maven-2 - JAVA_HOME 被 Maven 破坏

标签 maven-2 ant 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>

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

[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. 如果我运行 antcompile 一切都会编译得很好

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

最佳答案

这就是已接受答案中外部链接的缺点。 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>

关于maven-2 - JAVA_HOME 被 Maven 破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2022622/

相关文章:

Android maven jarsigner,jarsigner : attempt to rename X but failed

java - 从 Emma 代码覆盖率报告中过滤 junit 测试类

java - Gradle 项目中的 Maven 依赖项

java - 如何在安装时在maven中自动运行findbugs

maven-2 - 将大型发布部署到 Maven Central

java - Java 文件上的 Dos2unix

java - 使用文件集和路径元素在 build.xml 中设置类路径的区别

java - 编译成 WAR 时丢失文件

java - Android项目 "error: package R does not exist "的Maven编译错误

maven-2 - maven- assembly-plugin 不添加与系统范围的依赖关系