nashorn-maven-plugin Maven Plugin

Execute Javascript code during build

License

License

Categories

Categories

JavaScript Languages Maven Build Tools
GroupId

GroupId

io.github.michaldo
ArtifactId

ArtifactId

nashorn-maven-plugin
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

nashorn-maven-plugin Maven Plugin
Execute Javascript code during build
Project URL

Project URL

https://github.com/michaldo/nashorn-maven-plugin
Source Code Management

Source Code Management

https://github.com/michaldo/nashorn-maven-plugin

Download nashorn-maven-plugin

How to add to project

<plugin>
    <groupId>io.github.michaldo</groupId>
    <artifactId>nashorn-maven-plugin</artifactId>
    <version>0.0.2</version>
</plugin>

Dependencies

provided (2)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.4
org.apache.maven : maven-core jar 3.1.0

Project Modules

There are no modules declared in this project.

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

Versions

Version
0.0.2
0.0.1