如何在pom.xml文件中指定Java编译器版本?

我在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,编译器错误,但我得到了更多的错误。我从其他帖子中发现我应该修改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>

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

325697 次浏览
<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或Java 17。

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

我在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 >更新项目(选中强制更新)

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

希望对大家有所帮助

Thansk

一般来说,你不想只为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文档的编译器插件 因为编译器配置中的<source><target>元素在定义时使用属性maven.compiler.sourcemaven.compiler.target

source

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

target

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

关于sourcetarget的默认值,请注意 自maven编译器的__ABC0开始,默认值已从__ABC1更改为1.6 . < / p >

<release>标记-在maven-compiler-plugin 3.6中指定Java版本的新方法

你可以使用release参数:

<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标准选项JEP 247:编译旧平台版本

针对一个公共的、受支持的和文档化的API编译

.指定虚拟机版本
这种方式提供了一种标准的方法来为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使用相同版本的方法。

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