出于某种原因,我的 Ubuntu 13.10 Maven 安装使用旧插件。最明显的是,编译器插件是版本 2.0.2,默认为 Java 1.3:
$ mvn --show-version compile
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-14-generic", arch: "amd64", family: "unix"
[INFO] Scanning for projects...
[...]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project simple: Compilation failure
[ERROR] [...]/simple/src/main/java/org/sonatype/mavenbook/App.java:[11,17] error: generics are not supported in -source 1.3
编译失败,因为 ArrayList<Integer> a = new ArrayList<Integer>();
它使用泛型。 现在我知道我可以强制maven使用当前的编译器插件(参见 generics are not supported in -source 1.3 )但我宁愿解决根本原因,也不愿将我使用的每个插件添加到我的pom文件中。如果没有其他解决方案,我会将插件规范添加到父pom中,但我仍然对理性maven选择插件版本感兴趣。
我的环境:我的本地 m2 存储库(我的(库存 Ubuntu))/etc/maven/m2.conf
中没有 settings.xml看起来像这样:
main is org.apache.maven.cli.MavenCli from plexus.core
set maven.home default ${user.home}/m2
[plexus.core]
optionally ${maven.home}/lib/ext/*.jar
load ${maven.home}/lib/*.jar
还有我的/etc/maven/settings.xml
除了一些注释和空的 xml 标签之外,基本上是空的。项目pom默认由mvn archetype:generate -DgroupId=org.sonatype.mavenbook -DartifactId=simple -Dpackage=org.sonatype.mavenbook -Dversion=1.0-SNAPSHOT
生成:
<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>org.sonatype.mavenbook</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>simple</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>
</dependencies>
</project>
有什么建议吗?
最佳答案
您可以(并且应该)在 pom.xml
中手动指定 Maven 编译器插件
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
我不知道为什么 Maven 3.0.4 使用如此早期的 Maven 编译器插件版本,但我不认为这是 Ubuntu 特有的问题。我不知道通用解决方案是否需要本地配置。您希望避免这种情况,因为它会使您的构建平台依赖。在 pom 中显式指定这一点的优点是,您可以在任何其他计算机上构建项目,而不会再次遇到相同的问题。
如果不想在每个项目中都这样做,您可以使用父 pom.xml。请参阅default maven compiler setting
关于java - Maven 在 Ubuntu 安装中使用旧插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20706530/