【问题标题】:JAVA_HOME gets mangled by MavenJAVA_HOME 被 Maven 破坏
【发布时间】:2011-01-02 14:49:40
【问题描述】:

我正在使用统一的 Maven 构建来改造一堆现有的 Java 项目。由于每个项目都很成熟并且已经建立了基于 Ant 的构建,所以我使用maven-antrun-plugin 来执行现有的build.xml,如下所示:

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <ant antfile="build.xml" target="compile" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

当我运行 mvn compile 时,构建失败并显示以下消息:

[INFO] An Ant BuildException has occured: The following error occurred 
       while executing  this line:
build.xml:175: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Java\jdk1.6.0_13\jre"

让我困惑的是

  1. 我将JAVA_HOME=C:\Java\jdk1.6.0_13 作为我的环境设置的一部分,当mvn.bat 执行时,这正是我得到的值,但是正如您在错误消息中看到的那样,它显示为C:\Java\jdk1.6.0_13\jre
  2. 如果我运行 ant compile 一切都编译得很好

这是否意味着maven-antrun-plugin 可能会做类似set JAVA_HOME=%JAVA_HOME%\jre 的事情?我搜索了我的批处理/构建文件,但找不到更改发生的位置

【问题讨论】:

    标签: maven-2 ant maven


    【解决方案1】:

    我可以通过将以下属性定义放入我的 ant build.xml 文件中来解决此问题:

    <property name="build.compiler" value="extJavac"/>
    

    【讨论】:

      【解决方案2】:

      这是已接受答案中外部链接的缺点。 Codehaus 关闭,因此解决方案消失了。作为参考,这里是链接后面的内容 - 您基本上只需要将 &lt;dependencies&gt;...&lt;/dependencies&gt; 块复制到您的 antrun 插件...

      maven-antrun-plugin 运行 ant 时将 JAVA_HOME 设置为 JDK 的 jre 子目录,即使整个运行的 JAVA_HOME 是 JDK。 其他地方有关于如何在项目级别为 JDK 的 tools.jar 创建依赖项的文档,但这无助于 antrun,它是一个插件。 以下配置文件完成了这项工作。路径中的 '..' 经过 'jre' 目录到达 lib 目录。

      <profiles>
            <profile>
                <id>tools.jar</id>
                <build>
                  <plugins>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <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>
                  </plugins>
                </build>
            </profile>
      

      【讨论】:

      • 感谢您扩展损坏的链接!
      • 你先生,是互联网英雄,谢谢,我找了好几个小时。有用。我刚刚更改为 java 版本 1.8.0。这应该是现在公认的答案!
      • 在 Java 9 中如何解决这个问题,因为 tools.jar 已被删除?
      猜你喜欢
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2012-09-02
      • 2018-10-07
      • 2021-07-16
      • 2021-10-19
      • 2020-02-15
      • 2010-12-25
      相关资源
      最近更新 更多