在 Maven 中包装 Ant - JAVA_HOME 指向 JRE,但仅适用于 Ant

Kyl*_*ine 2 ant java-home maven maven-antrun-plugin

我有一个 Ant 项目,它可以自行构建得很好。我现在正尝试将其包装在 Maven 构建中,该构建将使用maven-antrun-plugin. 当我这样做时,构建失败并收到此错误,

[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: 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:\Java\jdk1.8.0_65\jre"
Run Code Online (Sandbox Code Playgroud)

有很多关于此错误的 SOF 帖子,但我觉得我的帖子是独一无二的,因为它仅在我将 Ant 构建包装在 Maven 中时才会发生,即,当我只是说 时,我不会在同一个项目上收到此错误$ ant build

这是我的pom.xml文件的一部分

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <tasks>
                            <ant antfile="build.xml" target="build" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>add-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>build/bin/myWarFile.war</file>
                                <type>war</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

我的JAVA_HOME环境变量设置为C:\Java\jdk1.8.0_65.

错误中提到的文件来自我的工作维护的库,我们保存所有罐子。在该文件中,第 170 行的内容如下

<target name="compile-src">
    <!-- Compile source -->
    <javac srcdir="${java.src.dir}"
        destdir="${class.dir}"
        debug="${debug.flag}"
        deprecation="${deprecation.flag}"
        nowarn="${warnings.flag}"
        optimize="off"
        source="${source.value}">
        <classpath refid="compile.classpath"/>
    </javac>
</target>
Run Code Online (Sandbox Code Playgroud)

带有source= 的行是第 170 行。

Dan*_*iel 5

这是一个常见问题。尝试使用此配置:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    ...
    <!-- Add this dependency to your ant-run configuration -->
    <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>
    ...
</plugin>
Run Code Online (Sandbox Code Playgroud)

Maven 使用 Java 的系统属性 java.home,它与环境变量不同,但它通过附加子目录来使用它来计算它,如所见。因此,Ant 所需的内容在该目录中根本不可用。 JAVA_HOMEjava.homejrejre

但是,此配置可确保正确满足 Ant 的插件依赖关系。