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

MTT*_*MTT 249 java pom.xml maven

我在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
Run Code Online (Sandbox Code Playgroud)

我试图使用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>
Run Code Online (Sandbox Code Playgroud)

如果你可以帮助我会很棒.

leo*_*ges 350

maven-compiler-plugin它已经存在于pom.xml中的插件层次结构依赖项中.入住有效的POM.

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

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

我正在使用maven 3.2.5

  • 这是@mkobit提供的Maven文档页面上的第一个方法.更好,你不必固定编译器插件的版本. (5认同)
  • @mkobit我想这是个人选择.如果我需要配置除编译器和源版本之外的其他东西,我更喜欢在插件部分中这样做. (2认同)

Sea*_*oyd 302

<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>
Run Code Online (Sandbox Code Playgroud)

请参阅maven编译器插件的配置页面:

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

哦,并且:不使用Java 1.3.x,当前版本是Java 1.7.x或1.8.x.

  • 您还可能想要使用Maven Enforcer插件添加对用于运行Maven的JDK版本的检查:http://maven.apache.org/enforcer/maven-enforcer-plugin/我发现这非常方便,因为我有支持不同JDK版本的不同项目.例如,它避免了您在Java 7项目上使用Java 8进行编译. (4认同)
  • 插件版本丢失了.它不会引发错误,但强烈建议在那里设置版本.目前的版本是3.3 (2认同)
  • @Elijah不,那将是java语言版本.我在谈论插件版本.你可以通过以下链接找到:http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin (2认同)

dav*_*xxx 12

通常,您不希望仅对source版本进行值(javac -source 1.8例如),但您希望同时对值sourcetarget版本进行值(javac -source 1.8 -target 1.8例如).
请注意,从Java 9开始,您可以通过两种方式传递信息,并以更强大的方式进行交叉编译兼容性(javac -release 9).
包装javac命令的Maven 提供了多种方式来传达所有这些JVM标准选项.

如何指定JDK版本?

使用maven-compiler-pluginor maven.compiler.source/ maven.compiler.targetproperties指定sourcetarget等效.

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

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

根据编译器插件Maven文档是等价的, 因为编译器配置中<source><target>元素和属性maven.compiler.source以及maven.compiler.target它们是否已定义.

资源

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

目标

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

关于默认值source,并 target请注意, 由于3.8.0Maven的编译器,默认值已经从改变1.51.6.

从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>
Run Code Online (Sandbox Code Playgroud)

您还可以声明用户属性maven.compiler.release:

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>
Run Code Online (Sandbox Code Playgroud)

但是目前最后一个maven-compiler-plugin版本还不够,因为您使用的默认版本不依赖于最近的版本.

Maven release参数传达release:我们可以从Java 9 传递 的新JVM标准选项:

针对特定VM版本的公共,支持和记录的API进行编译.

通过这种方式提供一种标准方法来指定相同的版本source,在targetbootstrapJVM选项.
请注意,指定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>
Run Code Online (Sandbox Code Playgroud)

从Java 9开始:

release参数(第三点),如果你想使用相同版本的强烈考虑的方式sourcetarget.


小智 10

我在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>
Run Code Online (Sandbox Code Playgroud)

右键单击项目> maven>更新项目(已检查强制更新)后

它决定我在项目上显示错误

希望它会有所帮助

Thansk