Maven不使用Java 7

Joh*_*nes 49 java maven-2 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>
Run Code Online (Sandbox Code Playgroud)

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它不使用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>
Run Code Online (Sandbox Code Playgroud)

Sta*_*iel 120

这可能不适用于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>
Run Code Online (Sandbox Code Playgroud)

  • 我的项目编译得很好,但后来某个src文件夹被添加到pom.xml,明确地将这两个属性设置为1.6,仅针对该特定文件夹,这使得追踪更加困难. (2认同)

dem*_*sne 2

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