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

时间:2013-05-23 20:42:06

标签: java maven pom.xml

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

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

5 个答案:

答案 0 :(得分:342)

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

答案 1 :(得分:297)

<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

答案 2 :(得分:10)

我在eclipse neon simple maven java project中面临同样的问题

但是我在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>

右键单击项目&gt; maven&gt;更新项目(已检查强制更新)

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

希望它会有所帮助

Thansk

答案 3 :(得分:10)

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

如何指定JDK版本?

使用javacmaven-compiler-plugin / maven.compiler.source属性来指定maven.compiler.targetsource是等效的。

target

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>
根据{{​​3}},

是等效的 因为编译器配置中的<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <source>元素使用属性<target>maven.compiler.source(如果已定义)。

  

Maven documentation of the compiler plugin

     

Java编译器的maven.compiler.target自变量。
  默认值为:-source
  用户属性是:1.6

     

source

     

Java编译器的maven.compiler.source自变量。
  默认值为:-target
  用户属性是:1.6

关于maven.compiler.targetsource的默认值,请注意  target

在maven-compiler-plugin 3.6中,您有了一种新的方式来指定Java版本

您可以使用since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6

target

您还可以仅声明用户属性<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 maven-compiler-plugin自变量传达release:我们可以从Java 9中传递的the release argument

  

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

这种方式提供了一种标准方式,可以为releasesourcetarget JVM选项指定相同的版本。
请注意,指定bootstrap是交叉编译的一种好习惯,并且即使您也不进行交叉编译也不会受到损害。

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

对于Java 8及以下版本:

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

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

maven.compiler.target

从Java 9开始:

maven-compiler-plugin参数(第三点)是一种强烈考虑是否要为<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> release使用相同版本的方法。

答案 4 :(得分:0)

<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> 
相关问题