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

4

2 回答 2

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>
于 2015-08-12T15:37:47.303 回答
21

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

<property name="build.compiler" value="extJavac"/>
于 2013-03-14T12:18:30.677 回答