Skip to content

michaldo/nashorn-maven-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nashorn-maven-plugin

Purpose

nashorn-maven-plugin allows execute little piece of Javascript code during build. It is lightweight alternative to implement regular plugin

Example

Let say you want touch file 'x' during build. Classic Ant way:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.6</version>
      <executions>
        <execution>
          <phase>process-resources</phase>
            <configuration>
              <target>
                <touch file="${basedir}/x" />
              </target>
            </configuration>
            <goals><goal>run</goal></goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Nashorn way:

<build>
  <plugins>
    <plugin>
      <groupId>io.github.michaldo</groupId>
      <artifactId>nashorn-maven-plugin</artifactId>
      <version>0.0.2</version>
      <executions>
        <execution>
          <phase>process-resources</phase>
          <goals><goal>eval</goal></goals>
          <configuration>
            <script>
              var path = java.nio.file.Paths.get($basedir, 'x');
              var Files = Java.type('java.nio.file.Files');
              if (!Files.exists(path)) {
                Files.createFile(path);
              } else {
                var FileTime = Java.type('java.nio.file.attribute.FileTime');
                Files.setLastModifiedTime(path, FileTime.from(java.time.Instant.now()))
              }
            </script>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

No benefit of Nashorn way. But let say you have multi-module project with many jar and war module and you want to touch 'x' only for war module. For Ant it's impossible, huh? For Nashorn it is pretty easy

<build>
  <plugins>
    <plugin>
      <groupId>io.github.michaldo</groupId>
      <artifactId>nashorn-maven-plugin</artifactId>
      <version>0.0.2</version>
      <executions>
        <execution>
          <phase>process-resources</phase>
          <goals><goal>eval</goal></goals>
          <configuration>
            <script>
              
              if ($project.packaging != 'war') return;
              
              var path = java.nio.file.Paths.get($basedir, 'x');
              var Files = Java.type('java.nio.file.Files');
              if (!Files.exists(path)) {
                Files.createFile(path);
              } else {
                var FileTime = Java.type('java.nio.file.attribute.FileTime');
                Files.setLastModifiedTime(path, FileTime.from(java.time.Instant.now()))
              }
            </script>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Usage

<plugin>
  <groupId>io.github.michaldo</groupId>
  <artifactId>nashorn-maven-plugin</artifactId>
  <version>0.0.2</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals><goal>eval</goal></goals>
        <configuration>
        <script>
          print('hello world');
        </script>
      </configuration>
    </execution>
  </executions>
</plugin>

Goals

nashorn:eval: executes javascript code

Parameters

script : required script code

Maven API

The following Maven objects are injected in Javascript space

Variable Class
$project actual MavenProject
$session MavenSession
$mojo MojoExecution
$plugin MojoExecution.getMojoDescriptor().getPluginDescriptor()
$settings MavenSession.getSettings()
$localRepository MavenSession.getLocalRepository()()
$reactorProjects MavenSession.getProjects()
$repositorySystemSession MavenSession.getRepositorySession()
$executedProject MavenProject.getExecutionProject()
$basedir MavenProject.getBasedir()

Requirements

Maven 3.1

Java 8

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published