首页 文章

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

提问于
浏览
224

我在netbeans上写了一个大约超过2000行的maven代码 . 当我在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>

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

4 回答

  • 315
    <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 1.7.x或1.8.x.

  • 286

    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

  • 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

  • 9

    通常,您不希望仅对 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是等效的,因为编译器配置中的 <source><target> 元素使用属性 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 .

    从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 9传递的new JVM standard option

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

    这种方式为 sourcetargetbootstrap JVM选项提供了标准方法来指定相同的版本 .
    请注意,指定 bootstrap 是交叉编译的好习惯,如果您不进行交叉编译也不会受到影响 .

    哪个是指定JDK版本的最佳方法?

    For Java 8 and below :

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

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

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

    From Java 9 :

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

相关问题