如何计算和更改 JavaMaven 的哪个版本用于执行?

在运行我们的 Maven 构建时,我的团队的一个成员在执行时在一个依赖插件中获得 NoSuchMethodError。但是,当我们从她的命令行运行 java -version时,它指示 Java 1.6.0 _ 26。这个错误显然是在大声疾呼 Maven 正在使用 Java5。

我如何找出并更改 Maven 正在使用的 Java 版本?

3个潜在的重要说明:

  1. 这是在命令行中执行的,而不是使用 Eclipse 插件
  2. JAVA _ HOME 被设置为 JAVA 1.6 JDK
  3. 使用 Maven 2.2.1
92850 次浏览

mvn -version will output which java it's using. If JAVA_HOME is set to a valid JDK directory and Maven is using something else, then most likely someone has tampered with the way that Maven starts up.

You will need to configure maven-compiler-plugin to use 1.6 source and target parameters ( by default it is 1.5 ).

This is the best done in your project's parent pom in <pluginManagment> section ( but you can always configure it per individual projects of course ).

  <build>


<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>


</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

mvn -version tells you what compiler Maven is using, so this answers the question unless your POM specifies override values for the versions to be used.

Here's the easiest way to specify the overrides in pom.xml:

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

Alternatively, the maven-compiler-plugin can be specified more explicitly. In that case, look at the <source> and <target> values associated with the plugin.

Both of these options are described here.

If your POM has a <parent>, then you need to check that as well (recursively).

Note: When source and target are specified, these values are passed as command-line options to the compiler. Using this feature, you can compile or run against earlier Java versions than the compiler's nominal value.