Maven不使用Java 7

我想打包一个maven(多)模块,父POM包括:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> 

我使用的是Java 1.7,属性如下所示:

 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <slf4j.version>1.6.1</slf4j.version> </properties> 

Maven版本是2.2.1:

 johannes@luna:~/workspace/treetank/bundles/treetank-core$ mvn -version Apache Maven 2.2.1 (rdebian-6) Java version: 1.7.0 Java home: /usr/lib/jvm/jdk1.7.0/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux" version: "3.0.0-14-generic" arch: "amd64" Family: "unix" 

我不知道为什么它不使用Java版本1.7。 当调用mvn package我得到错误(例如使用-source 7或更高来启用菱形运算符)。 你知道为什么使用1.6吗?

有效的POM是:

  <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <executions> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </execution> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </execution> </executions> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> 

这可能在maven 2.2.1中不起作用,但是对于Maven 3.0.4,只要将这两个属性添加到pom的属性中就可以实现Java 7:

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

关于jdk 1.7与Maven 2.2.1给出的兼容性问题的完美解释,Mark Peters Maven“无法parsing错误信息”(Java 7 + Maven 2)