Maven-antrun-plugin运行的Ant在Java 9上找不到脚本引擎

8

我有一个使用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>

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>

在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: The following error occurred while executing this line:
/home/dan/scriptdef-test/build.xml:10: Unable to create javax script engine for javascript
around Ant part ...<ant antfile="build.xml" inheritRefs="true">... @ 4:47 in /home/dan/scriptdef-test/target/antrun/build-main.xml
...    
Caused by: /home/dan/scriptdef-test/build.xml:10: Unable to create javax script engine for javascript
    at org.apache.tools.ant.util.optional.JavaxScriptRunner.evaluateScript(JavaxScriptRunner.java:84)
    at org.apache.tools.ant.util.optional.JavaxScriptRunner.executeScript(JavaxScriptRunner.java:67)
    at org.apache.tools.ant.taskdefs.optional.script.ScriptDef.executeScript(ScriptDef.java:350)
    at org.apache.tools.ant.taskdefs.optional.script.ScriptDefBase.execute(ScriptDefBase.java:50)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:547)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:441)
    ... 34 more

我已经连接了一个调试器并观察到由JavaxScriptRunner使用的ScriptEngineManager没有任何脚本引擎,因为它使用的服务加载器没有找到任何ScriptEngineFactory实现。我该如何让服务加载器正常工作?
更新:我还为此创建了一个问题:https://issues.apache.org/jira/browse/MANTRUN-200

有点奇怪,我同意,但是我对这些构建文件中发生的事情一无所知,所以我不太想改变它们。 - Dan Berindei
我找到的唯一提供ScriptEngineFactory的模块是_jdk.scripting.nashorn_。尝试将--add-modules jdk.scripting.nashorn添加到最终抱怨缺少引擎的java命令中。 - Nicolai Parlog
1
在scriptdef标签中添加classpathref="maven.plugin.classpath"属性。 - Cristian Florescu
@myset 没有起作用。 - Dan Berindei
虽然MANTRUN-200已经标记为关闭,但问题仍然存在(请参见链接问题MNG-6275)。我有一个不同但相关的用例 - 因此可以直接使用jdk.nashorn.api.scripting.NashornScriptEngineFactory - 但似乎通过在插件类路径上包含META-INF/services/javax.script.ScriptEngineFactory的东西来黑客SPI是行不通的(由于MNG-6275上的更广泛问题)。 - earcam
显示剩余4条评论
2个回答

3

我的解决方法是将我们的脚本改成Groovy,但我收到了有关此问题的通知,于是回来看是否已修复该问题。尽管问题没有解决,但一条评论为我提供了一个可行的解决方案:

<?xml version='1.0' encoding='UTF-8'?>
<project name="scriptdef-test-build">
   <scriptdef name="test-javascript" language="javascript"
              classpath="${plugin_classpath}">
      <![CDATA[
           var System = Java.type('java.lang.System');
           System.out.println("Working from Javascript!");
        ]]>
   </scriptdef>

   <target name="all">
      <property name="plugin_classpath" refid="maven.plugin.classpath"/>
      <test-groovy/>
      <test-javascript/>
   </target>
</project>

plugin_classpath 属性是重要的部分:定义并在 scriptdef 中使用它。 - bebbo

0

Dan Berindei的解决方案适用于JDK 11,并且配置嵌入在插件中:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <!-- id/phase/goals here -->
      <configuration>
        <target>
          <property name="plugin_classpath" refid="maven.plugin.classpath"/>

          <script language="javascript" classpath="${plugin_classpath}"><![CDATA[
            project.setProperty('resultCode', 
                parseInt(project.getProperty('copyResult')));
          ]]></script>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

也适用于Ant的scriptcondition

<scriptcondition language="javascript" classpath="${plugin_classpath}"><![CDATA[
   <!-- your script here... -->
]]></scriptcondition>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接