Maven Ant BuildException 与 maven-antrun-plugin ...无法找到 javac 编译器

问题描述 投票:0回答:2

我正在尝试让 Maven 为某些遗留代码调用 ANT 构建。 ant 构建通过 ant 正确构建。但是,当我使用 Maven ant 插件调用它时,它失败并出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run      (default) on project CoreServices: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:158: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:62: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:33: The following error occurred while executing this line:
[ERROR] C:\dev\projects\ods\build.xml:41: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\bea\jdk150_11\jre"

我的 javac 存在于 C: ea\jdk150_11 中,这适用于所有其他事情。我不确定 Maven 从哪里获取此版本的 JAVA_HOME。 windows环境变量中的JAVA_HOME设置为C:ea\jdk150_11\,应该是这样。

我用来调用 build.xml 的 Maven 代码是

<build>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.6</version>

     <executions>
          <execution>
            <phase>install</phase>
            <configuration>

              <target>
    <ant antfile="../build/build.xml" target="deliver" >
    </ant>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
    </plugin>
   </plugins>
  </build>
java maven maven-plugin
2个回答
22
投票

第一件事:为什么在

install
阶段而不是在
compile
阶段执行 ANT 脚本?

第二件事:你的问题可能是由于Maven执行JRE而不是JDK而引起的,尽管你的JAVA_HOME指向JDK。要解决此问题,您必须手动调整 maven-antrun-plugin 的依赖关系。这只是一个例子:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <dependencies>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.5.0</version>
            <scope>system</scope>
            <systemPath>${java.home}/../lib/tools.jar</systemPath>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>compile</phase>
            <configuration><target><ant/></target></configuration>
            <goals><goal>run</goal></goals>
        </execution>
    </executions>
</plugin>

0
投票

我和OP有同样的问题!

  1. 通过Eclipse IDE执行ant目标编译=成功
  2. 通过命令控制台执行相同的ant目标=成功
  3. 执行maven ant插件来执行此ant目标=失败并出现相同的错误

解决方案:在ant

<javac>
命令中,设置其标志:
fork=yes

<target name="compileJava">
    <javac destdir="${dir.build}"
           fork="yes" 
           includeantruntime="false"
           debug="true"
           failonerror="true">
        //...
    </javac>
</target>

参考:链接

© www.soinside.com 2019 - 2024. All rights reserved.