JAVA_HOME被Maven破坏了

Bos*_*one 38 ant maven-2 maven

我正在使用统一的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>
Run Code Online (Sandbox Code Playgroud)

当我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"
Run Code Online (Sandbox Code Playgroud)

令我困惑的是

  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?我搜索了我的批处理/构建文件,我无法找到发生更改的位置

Lon*_*zak 21

在一个公认的答案中,这是外部链接的缺点.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>
Run Code Online (Sandbox Code Playgroud)

  • 由于tools.jar 已被删除,您如何在Java 9 中解决此问题? (2认同)

dvt*_*ver 16

我能够通过在ant build.xml文件中放入以下属性定义来解决这个问题:

<property name="build.compiler" value="extJavac"/>
Run Code Online (Sandbox Code Playgroud)