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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19571165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:25:55  来源:igfitidea点击:

set the default jdk for maven-compiler-plugin

javamaven

提问by Brett

I have just installed maven on an new ubuntu system, which includes the maven-compiler-plugin. I have a java project that was previously building fine, defaulting to a javac source and target of 5 (jdk 1.5). However, the project is now trying to compile using jdk1.3 on the new system. Is there an easy way to configure the system to use >=jdk5 ?

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

Here's some of the configuration details of the system:

下面是系统的一些配置细节:

$ 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 <[email protected]>
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 <[email protected]>
Architecture: all
Source: maven-compiler-plugin
Version: 2.0.2-6

I've checked the maven-compiler-plugin-2.0.2.pom file, and plexus-compiler-javac.originalVersion and others are set to 1.5.3.

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

I know I can set this on a per-project basis by including a source/target tag in a plugin context, but I'd like to configure maven-compiler to default to jdk5 or higher without having to do this across a large number of projects.

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

how can i do this?

我怎样才能做到这一点?

采纳答案by Tome

Default value for source and target was 1.3 in older versions of maven-compiler plugin (like 2.0.2-6). Use at least a 3.0 version of the Maven compiler plugin to get this back to the original behaviour, or just configure that plugin to get source and target to appropriate values.

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

回答by Reji

Suggestion: Use the latest maven compiler plugin.

建议:使用最新的maven编译器插件。

In order to change the defaults, you should set source and target.

为了更改默认值,您应该设置源和目标。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>

More Info: maven-compiler-plugin

更多信息:maven-compiler-plugin

回答by khmarbaise

The simplest solution to such things is using an up-to-date version of Maven (3.1.1) and in particular create a parent pom.xml file for all your projects where you define the configuration and version of your maven-compiler-plugin via pluginManagement or better all of your plugins.

解决此类问题的最简单的解决方案是使用最新版本的 Maven (3.1.1),特别是为您定义 maven-compiler-plugin 的配置和版本的所有项目创建一个父 pom.xml 文件通过 pluginManagement 或更好的所有插件。

回答by Robert H

In your pom specify the following to set the compiler to JDK5:

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

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

i.e.

IE

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

I specify mine prior to the dependencies, although so long as its part of the project element you should be able to place it anywhere inside the pom.

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

I ran into a similar issue with Maven previously, this fixed it for me. Essentially what this does is set the -sourceand -targetflags to the value specified and passes it to the compiler. Newer plugins default to 1.5.

我之前在 Maven 中遇到了类似的问题,这为我修复了它。本质上,这是将-source-target标志设置为指定的值并将其传递给编译器。较新的插件默认为 1.5。

In order to use the default approach withoutspecifying the properties, you will need to be running a later version of Maven.

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

I suppose you could also set up a template via your IDE to include this in all new pom files. Of course the actual implementation would depend upon your IDE...

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

See The apache maven compiler plugin documentationas well as Setting the source and compiler examplesfor more details.

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

回答by chinto

I tried the maven-compiler-pluginapproach and it proved cumbersome as there are plugins like maven-surefire-pluginand maven-cobertura-pluginwhich still fail due to incompatibility issues.

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

The better approach was to use maven-toolchain-plugin.

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

Step 1Create /.m2/toolchains.xml

步骤 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>
-->

Step 2Add maven-toolchain-pluginto pluginssection in your project pom.xml.

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

*If using maven 3, ensure this goes into pluginManagementas well *

*如果使用 maven 3,请确保这也进入pluginManagement*

   <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>

Voila all your other plugins pick up the right JDK. Hope it helps. I spent almost half day on this exact issue today.

瞧,您的所有其他插件都选择了正确的JDK。希望能帮助到你。我今天在这个确切的问题上花了将近半天的时间。

回答by frank

I used following settings to set maven default java compiler version.

我使用以下设置来设置 maven 默认的 java 编译器版本。

first, modify maven settings.xml:

首先,修改maven settings.xml:

<profile>
  <id>jdk-1.8</id>
  <activation>
      <activeByDefault>true</activeByDefault>
      <jdk>1.8</jdk>
  </activation>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  </properties>
</profile>

second, in eclipse preferences, make the java home point to jdk home

二、在eclipse的preferences中,让java home指向jdk home