Maven 不使用 Java 7

2022-08-31 21:20:15

我想打包一个maven-(multi)模块,父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。例如,在调用时,我收到错误(使用-source 7或更高版本来启用菱形运算符)。你知道为什么它尝试使用1.6吗?mvn package

有效的 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>

答案 1

这可能不适用于 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>

答案 2

关于jdk 1.7与maven 2.2.1的兼容性问题的非常好的解释,由Mark Peters Maven“无法解析错误消息”(Java 7 + Maven 2)


推荐