nashorn-maven-plugin Maven Plugin

Execute Javascript code during build

Лицензия

Лицензия

Категории

Категории

JavaScript Языки программирования Maven Компиляция и сборка
Группа

Группа

io.github.michaldo
Идентификатор

Идентификатор

nashorn-maven-plugin
Последняя версия

Последняя версия

0.0.2
Дата

Дата

Тип

Тип

maven-plugin
Описание

Описание

nashorn-maven-plugin Maven Plugin
Execute Javascript code during build
Ссылка на сайт

Ссылка на сайт

https://github.com/michaldo/nashorn-maven-plugin
Система контроля версий

Система контроля версий

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

Скачать nashorn-maven-plugin

Как подключить последнюю версию

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

Зависимости

provided (2)

Идентификатор библиотеки Тип Версия
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.4
org.apache.maven : maven-core jar 3.1.0

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
0.0.2
0.0.1