为 maven-compiler-plugin 设置默认的 jdk

2022-09-03 18:28:33

我刚刚在一个新的ubuntu系统上安装了maven,其中包括maven-compiler-plugin。我有一个Java项目,以前构建得很好,默认为javac源代码和目标5(jdk 1.5)。但是,该项目现在正试图在新系统上使用jdk1.3进行编译。有没有一种简单的方法可以将系统配置为使用>=jdk5?

以下是系统的一些配置详细信息:

$ java -version
java version "1.6.0_45"

$ dpkg -s maven
Package: maven
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 1489
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Version: 3.0.4-2

$ dpkg -s libmaven-compiler-plugin-java
Package: libmaven-compiler-plugin-java
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 75
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Source: maven-compiler-plugin
Version: 2.0.2-6

我已经检查了maven-compiler-plugin-2.0.2.pom文件,plexus-compiler-javac.originalVersion和其他文件设置为1.5.3。

我知道我可以通过在插件上下文中包含源/目标标签来为每个项目设置此参数,但我想将maven-compiler配置为默认为jdk5或更高版本,而不必在大量项目中执行此操作。

我该怎么做?


答案 1

在 pom 中,指定以下内容以将编译器设置为 JDK5:

<properties>
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
</properties>

<project>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    ...
  </project>

我在依赖项之前指定了我的,尽管只要它是项目元素的一部分,您应该能够将其放置在pom中的任何位置。

我以前在Maven上遇到了类似的问题,这为我解决了这个问题。从本质上讲,这样做是将 和 标志设置为指定的值,并将其传递给编译器。较新的插件默认为 1.5。-source-target

为了在不指定属性的情况下使用默认方法,您需要运行更高版本的 Maven。

我想你也可以通过你的IDE设置一个模板,把它包含在所有新的pom文件中。当然,实际的实现将取决于您的IDE...

有关更多详细信息,请参阅 apache maven 编译器插件文档以及设置源代码和编译器示例


答案 2

我尝试了maven-compiler-plugin方法,它被证明很麻烦,因为有像maven-surefire-pluginmaven-cobertura-plugin这样的插件,由于不兼容问题仍然失败。

更好的方法是使用maven-toolchain-plugin

步骤1创建 /.m2/工具链.xml

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.8</version>
        <vendor>sun</vendor>
    </provides>
    <configuration>
          <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.7</version>
        <vendor>sun</vendor>
    </provides>
    <configuration>
        <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>
<toolchain>
    <type>jdk</type>
    <provides>
        <version>1.6</version>
        <vendor>apple</vendor>
    </provides>
    <configuration>
        <jdkHome>/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home</jdkHome>
    </configuration>
</toolchain>

<!-- other toolchains -->
<!--
<toolchain>
    <type>netbeans</type>
    <provides>
        <version>5.5</version>
    </provides>
    <configuration>
        <installDir>/path/to/netbeans/5.5</installDir>
    </configuration>
</toolchain>
-->

第 2 步maven-toolchain-plugin 添加到项目 pom.xml 中的插件部分。

*如果使用 maven 3,请确保这也进入插件管理 *

   <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-toolchains-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>1.7</version>
                        <vendor>sun</vendor>
                    </jdk>
                </toolchains>
            </configuration>
        </plugin>

瞧,您的所有其他插件都选择了正确的JDK。希望它有帮助。我今天花了将近半天的时间讨论这个问题。


推荐