指定 JDK 供 Maven 使用

新手上路,请多包涵

我正在尝试构建一个我修改过的 Hudson 插件,它需要 jdk1.6。这很好,但我不知道如何告诉 Maven 不同的 jdk 在哪里。我在互联网上发现很少提及,但它们似乎不适用于我。有人建议向 .m2/settings.xml 添加一些配置,但我没有 settings.xml 。另外,我不想对所有 Maven 构建使用 1.6。

一个问题是我在 cygwin 中使用 mvn ,如果这很重要的话。看来我应该能够在项目 pom 文件中进行规范,但现有的 pom 非常简单。

所以底线是,有没有办法为单次调用 maven 指定 jdk?

原文由 DanInDC 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 831
2 个回答

所以底线是,有没有办法为单次调用 maven 指定 jdk?

临时更改 JAVA_HOME 环境变量的值。

原文由 Pascal Thivent 发布,翻译遵循 CC BY-SA 4.0 许可协议

似乎 maven 现在在这里提供了一个解决方案: 使用不同的 JDK 编译源代码

假设您的 JAVA_HOME 指向 JDK7(它将运行 maven 进程)

你的 pom.xml 可能是:

 <build>
    <plugins>
        <!-- we want JDK 1.6 source and binary compatiblility -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- ... -->
        <!-- we want sources to be processed by a specific 1.6 javac -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable>${JAVA_1_6_HOME}/bin/javac</executable>
              <compilerVersion>1.3</compilerVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

如果您的开发人员只是在他们的 settings.xml 中添加(和自定义)以下行,您的 pom 将与平台无关:

 <settings>
  [...]
  <profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
          <JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
        </properties>
    </profile>
  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>compiler</activeProfile>
  </activeProfiles>
</settings>

原文由 Cerber 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题