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

Bre*_*ett 14 java maven

我刚刚在一个新的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
Run Code Online (Sandbox Code Playgroud)

我检查了maven-compiler-plugin-2.0.2.pom文件,plexus-compiler-javac.originalVersion等设置为1.5.3.

我知道我可以在每个项目的基础上通过在插件上下文中包含源/目标标签来设置它,但我想将maven-compiler配置为默认为jdk5或更高,而不必在大量的项目.

我怎样才能做到这一点?

Rob*_*t H 19

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

<properties>
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)

<project>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    ...
  </project>
Run Code Online (Sandbox Code Playgroud)

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

我之前遇到过与Maven类似的问题,这个问题已经解决了.基本上它的作用是将-source-target标志设置为指定的值并将其传递给编译器.较新的插件默认为1.5.

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

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

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


chi*_*nto 6

我尝试了maven-compiler-plugin方法,因为有像maven-surefire-pluginmaven-cobertura-plugin这样的插件仍然因为不兼容问题而失败,所以它很麻烦.

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

步骤1 创建/.m2/toolchains.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>
-->
Run Code Online (Sandbox Code Playgroud)

步骤2maven-toolchain-plugin添加到项目pom.xml中的plugins部分.

*如果使用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>
Run Code Online (Sandbox Code Playgroud)

所有其他插件都可以选择合适的JDK.希望能帮助到你.我今天在这个问题上花了差不多半天时间.


Tom*_*ome 1

在旧版本的 maven-compiler 插件(如 2.0.2-6)中,源和目标的默认值为 1.3。至少使用 3.0 版本的 Maven 编译器插件将其恢复为原始行为,或者仅配置该插件以将源和目标设置为适当的值。