JAVA_HOME受到Maven的影响

我正在使用统一的Maven构build来改造大量现有的Java项目。 由于每个项目都很成熟,并且已经build立了基于Ant的构build,所以我使用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 ? 我search了我的批处理/构build文件,我无法find发生变化的地方

这是在一个被接受的答案外部链接的缺陷。 Codehausclosures,因此解决scheme不复存在。 这里是参考链接的内容 – 你基本上只需要将<dependencies>...</dependencies>块复制到你的antrun插件中。

即使整个运行的JAVA_HOME是一个JDK,maven-antrun-plugin也会运行ant并将JAVA_HOME设置为JDK的jre子目录。 在其他地方有关于如何在JDK的tools.jar的项目级别上创build依赖关系的文档,但是这并不能帮助antrun,它是一个插件。 以下configuration文件完成这项工作。 path中的“..”在“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> 

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

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