我有一个使用Ant构建文件的Maven项目:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-test-app</artifactId>
<groupId>my-test-group</groupId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="build.xml" inheritRefs="true">
<target name="all"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
Ant构建文件使用脚本:
<?xml version='1.0' encoding='UTF-8'?>
<project name="scriptdef-test-build">
<scriptdef name="test-script" language="javascript">
<![CDATA[
var System = Java.type('java.lang.System');
System.out.println("Working!");
]]>
</scriptdef>
<target name="all">
<test-script/>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
在Java 8上它可以工作,但在Java 9(9-ea + 162)上它找不到脚本引擎:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (compile) on project my-test-app: An Ant BuildException has occured: …Run Code Online (Sandbox Code Playgroud) 我们将 Apache Ant 与 Nashorn JavaScript Engine 结合使用,该引擎在 jdk 15 及更高版本中已被弃用并删除。我试图找到如何从 Nashorn 切换到 Graal VM,但即使在 Apache 网站上也没有找到任何可用的信息。请告知我需要哪些罐子以及我应该放置它们。我们的代码需要改变什么。如果有人已经这样做了,请分享您的经验。我有一个针对 jdk1.8.0_311 运行的示例:
<?xml version="1.0" ?>
<project name="test" default="test">
<property environment="env"/>
<target name="test" >
<script language="javascript">
<![CDATA[
load("nashorn:mozilla_compat.js");
importPackage(java.time);
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
self.log("This script is for Test Of Nashorn Javascript Engine");
print ("Today is: " + date );
]]>
</script>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
结果看起来像:
test:
[script] Warning: Nashorn engine is planned to be removed from a future JDK release …Run Code Online (Sandbox Code Playgroud)