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

如何在pom.xml文件中指定Java编译器版本?,java,maven,pom.xml,Java,Maven,Pom.xml,我用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

我用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
如果你能帮助我,那就太好了


[...]
[...]
org.apache.maven.plugins


哦,还有:不要使用Java 1.3.x,当前版本是Java 1.7.x或1.8.x

maven编译器插件它已经存在于pom.xml中的插件层次依赖关系中。签入有效的POM

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

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

1.8
1.8

我正在使用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>

org.apache.maven.plugins
maven编译器插件
3.6.1
1.8
1.8
右键单击project>maven>update项目后(选中强制更新)

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

希望这会有帮助


Thansk

通常,您不希望只对
源代码
版本(例如
javac-source 1.8
)进行估价,但您希望同时对
源代码
目标代码
版本(例如
javac-source 1.8-target 1.8
进行估价)。
请注意,从Java9中,您可以通过一种更健壮的方式传递信息,以实现跨编译兼容性(
javac-release9
)。
封装
javac
命令的Maven提供了多种方式来传递所有这些JVM标准选项

如何指定JDK版本? 使用
maven编译器插件
maven.compiler.source
/
maven.compiler.target
属性指定
目标
是等效的

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>
您还可以只声明用户属性
maven.compiler.release

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>
Java9及更高版本 <> > <代码>发行版>代码>参数(第三点)是一种强烈的考虑,如果您想使用相同版本的“代码>源代码/代码>和<代码>目标< /代码> .

代码>
<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> 
org.apache.maven.plugins maven编译器插件 3.3 真的 1.8 1.8
升级java版本的时间到了。java 5支持泛型。在Java版本之前不可能让它们工作。OpenJDK javac的默认值是1.3,而Oracle JDK的默认值是1.5,这两个值在我成为程序员之前就已经存在了!相关:您可能还需要对使用Maven Enforcer插件运行Maven所使用的JDK版本进行检查:我发现这非常方便,因为我必须支持不同JDK版本上的不同项目。例如,它避免了在Java 7项目上使用Java 8进行编译。插件版本丢失。它不会抛出错误,但强烈建议在那里设置版本。目前的版本是3。3@LukasWerner你当然是对的。但是如果我添加一个版本,我必须每隔几个月编辑一次这个问题。寻求妥协:-)请澄清。。。这个标签中有什么内容(当前的版本)?是类似于1.7吗?@Elijah不,那将是java语言版本。我说的是插件版本。你可以通过这个链接找到:哪种方式是“最好的”?与所选答案相比,这一条没有那么冗长,但似乎有点隐藏。甚至Maven也会使用插件进行演示。@mkobit我想这是个人的选择。如果我需要配置编译器和源代码版本之外的其他东西,我更喜欢在插件部分进行配置。如果我想使用类似的东西,我就必须使用plugins部分,这是@mkobit给出的Maven文档页面上的第一个方法。更棒的是,你不必固定编译器插件的版本。在我用IntelliJ重新创建项目后,这对我来说很有效。我必须在IntelliJ中重新打开该项目,方法是将该项目的pom文件作为一个新项目打开,我假设该项目已重新创建。当我尝试为Microsoft JDBC Driver for SQL Server构建SqlServerSample应用程序时,这对我很有效。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>9</release>
    </configuration>
</plugin>
<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<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>