JAVA_HOME 被 Maven 破坏

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

我正在使用统一的 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
3个回答
22
投票

这就是已接受答案中外部链接的缺点。 Codehaus 关闭了,因此解决方案消失了。作为参考,这里是链接后面的内容 - 你基本上只需将

<dependencies>...</dependencies>
块复制到你的 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>

21
投票

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

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

0
投票

太棒了,上面的配置文件解决了路径问题,我使用的方式是复制代码并使用chatgpt将其完美地制作为适合pom.xml文件中的内容。

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