【问题标题】:Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant在 Maven 中包装 Ant - JAVA_HOME 指向 JRE,但仅适用于 Ant
【发布时间】:2016-01-11 02:13:09
【问题描述】:

我有一个 Ant 项目,它可以自行构建。我现在正试图将它包装在一个 Maven 构建中,该构建将使用 maven-antrun-plugin 启动 Ant 构建。当我这样做时,构建失败并出现此错误,

[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"

有很多关于此错误的 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>

我的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>

source= 所在的行是第 170 行。

【问题讨论】:

    标签: maven ant java-home maven-antrun-plugin


    【解决方案1】:

    这是一个常见问题。试试这个配置:

    <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>
    

    Maven 使用 Java 的 系统属性 java.home,它环境变量 JAVA_HOME 相同,但它使用它通过添加jre 子目录来计算它的java.home,正如所见证的那样。因此,Ant 所需的东西在jre 目录中根本不可用。

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

    【讨论】:

      【解决方案2】:

      您需要指向 JDK 而不是 JRE。只需消除愤怒并尝试。

      It is currently set to "C:\Java\jdk1.8.0_65\jre"
      

      如果您的 JDK 已设置 - 另一种解决方法 - 您可以将 tools.jar 从 jdk lib 复制到 jre lib 并查看它是否有效。

      【讨论】:

      • 帖子明确指出我指的是 JDK 而不是 JRE。
      • 投反对票。?我认为您在我发表评论后编辑了您的帖子,或者可能是同一时间
      • 我的 JAVA_HOME 环境变量设置为 C:\Java\jdk1.8.0_65 在原始帖子中。我在您回答后做了笔记,因为您没有阅读整篇文章。
      • 复制 tools.jar 无效。
      猜你喜欢
      • 2023-03-15
      • 2011-09-11
      • 2020-04-30
      • 2019-04-19
      • 2013-06-19
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多