【问题标题】:How do you specify the Java compiler version in a pom.xml file?如何在 pom.xml 文件中指定 Java 编译器版本?
【发布时间】:2013-05-19 09:07:06
【问题描述】:

我在 Netbeans 中编写了一些 Maven 代码,大约有 2000 多行。当我在 Netbeans 上编译它时,一切都很好,但是如果我想在命令行上运行它,我会得到这些错误:

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        HashSet<Double> resid_List = new HashSet<Double>(Arrays.asList(resid_val));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        List<Integer> ind_ovlpList = new ArrayList<Integer>(Arrays.asList(ind_ovlp));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
public class ColumnComparator implements Comparator<double[]> {

annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
@Override

我尝试使用Java 1.3.1, compiler errors,但出现更多错误。我从其他帖子中发现我应该修改pom.xml,但我不知道如何修改。这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany</groupId>
  <artifactId>mavenmain</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>mavenmain</name>
    <url>http://maven.apache.org</url>

   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

   <dependencies>
     <dependency>
       <groupId>junit</groupId>
        <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
     <dependency>
        <groupId>gov.nist.math</groupId>
        <artifactId>jama</artifactId>
        <version>1.0.2</version>
     </dependency>
   </dependencies>
 </project>

如果你能帮助我,那就太好了!

【问题讨论】:

标签: java maven pom.xml


【解决方案1】:

ma​​ven-compiler-plugin 它已经存在于 pom.xml 的插件层次结构依赖项中。签入有效的 POM。

简而言之,您可以使用如下属性:

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

我正在使用 Maven 3.2.5。

【讨论】:

  • 哪种方式是“最好的”?与所选答案相比,这个答案不那么冗长,但它似乎有点隐藏。甚至 Maven site documentation 也显示使用该插件。
  • @mkobit 我想这是个人选择。如果我需要配置除编译器和源版本之外的其他内容,我更喜欢在插件部分进行配置。
  • “配置其他东西”很好。如果我想使用google/error-prone 之类的东西,我将不得不使用插件部分的方式。
  • 这是@mkobit 提供的Maven 文档页面上的第一个方法。好多了,而且您不必固定编译器插件的版本。
  • 在 IntelliJ 中重新创建项目后,这对我有用。我不得不通过打开项目的 pom 文件作为新项目在 IntelliJ 中重新打开项目,我假设它重新创建了项目。
【解决方案2】:
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>(whatever version is current)</version>
        <configuration>
          <!-- or whatever version you use -->
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

查看 maven 编译器插件的配置页面:

http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

哦,还有:不要使用 Java 1.3.x,当前版本是 Java 11 或 17。

【讨论】:

  • 您可能还想检查您使用 Maven Enforcer Plugin 运行 Maven 的 JDK 版本:maven.apache.org/enforcer/maven-enforcer-plugin 我觉得这很方便,因为我必须支持不同的项目不同的JDK版本。例如,它避免了您在 Java 7 项目上使用 Java 8 进行编译。
  • 插件版本缺失。它不会引发错误,但强烈建议在此处设置版本。当前版本是 3.3
  • @LukasWerner 你当然是对的。但如果我添加一个版本,我必须每隔几个月编辑一次问题。妥协了:-)
  • 请澄清...这个标签 (无论是当前版本) 中的内容是什么?是 1.7 之类的吗?
  • @Elijah 不,那将是 java 语言版本。我说的是插件版本。你可以通过这个链接找到:mvnrepository.com/artifact/org.apache.maven.plugins/…
【解决方案3】:

通常,您不想只评估source 版本(例如javac -source 1.8),但您希望同时评估sourcetarget 版本(例如javac -source 1.8 -target 1.8)。
请注意,从 Java 9 开始,您可以同时传达信息并以更健壮的方式实现交叉编译兼容性 (javac -release 9)。
包装 javac 命令的 Maven 提供了多种方式来传达所有这些 JVM 标准选项。

如何指定JDK版本?

使用maven-compiler-pluginmaven.compiler.source/maven.compiler.target 属性来指定sourcetarget 是等效的。

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

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

根据Maven documentation of the compiler plugin等价 因为编译器配置中的&lt;source&gt;&lt;target&gt; 元素使用属性maven.compiler.sourcemaven.compiler.target(如果已定义)。

source

Java 编译器的 -source 参数。
默认值为:1.6.
用户属性为:maven.compiler.source

target

Java 编译器的 -target 参数。
默认值为:1.6
用户属性为:maven.compiler.target

关于sourcetarget 的默认值,请注意 since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6.

&lt;release&gt; 标签 — 在 maven-compiler-plugin 3.6 中指定 Java 版本的新方法

你可以使用the release argument

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>9</release>
    </configuration>
</plugin>

你也可以只声明用户属性maven.compiler.release

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

但此时最后一个还不够,因为您使用的 maven-compiler-plugin 默认版本不依赖于足够新的版本。

Maven 的release 参数将release 传递给Java 编译器以访问Java 9 中新添加的JVM standard optionJEP 247: Compile for Older Platform Versions

针对公共、受支持和记录在案的 API 编译 特定的 VM 版本。

这种方式提供了一种为sourcetargetbootstrap JVM 选项指定相同版本的标准方式。
请注意,指定bootstrap 是交叉编译的好习惯,如果您不进行交叉编译也不会有什么坏处。

指定 JDK 版本的最佳方法是什么?

Java 8 及以下

maven.compiler.source/maven.compiler.target 属性使用maven-compiler-plugin 都不是更好。 它没有改变任何事实,因为最终这两种方式依赖于相同的属性和相同的机制:maven 核心编译器插件。

好吧,如果您不需要在编译器插件中指定除 Java 版本之外的其他属性或行为,那么使用这种方式更有意义,因为这样更简洁:

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

Java 9 及更高版本

release 参数(第三点)是一种强烈考虑是否要对sourcetarget 使用相同版本的方法。

【讨论】:

    【解决方案4】:

    我在 eclipse neon simple maven java 项目中遇到了同样的问题

    但我在 pom.xml 文件中添加了以下详细信息

       <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    右键项目> maven>更新项目后(勾选强制更新)

    它解决了我在项目上显示错误

    希望对你有帮助

    谢谢

    【讨论】:

    • 当我尝试为 Microsoft JDBC Driver for SQL Server 构建 SqlServerSample 应用程序时,这对我有用。
    【解决方案5】:
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <fork>true</fork>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin> 
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 2011-04-07
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多