49

I want to package a maven-(multi)module, the parent POM includes:

<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>

I'm using Java 1.7 and the properties are specified as follows:

<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>

The Maven version is 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"

I have no clue why it doesn't use Java version 1.7. When invoking mvn package I get the error (use -source 7 or higher to enable diamond operator) for instance. Do you know why it tries using 1.6?

The effective POM is:

  <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
  • Works for me on a simple maven project on RHEL5. The error text I get when compiling 1.7 source with 1.6 is also different - diamond operator is not supported in -source 1.6
    – Raghuram
    Jan 6, 2012 at 13:50

2 Answers 2

120

This may not work in maven 2.2.1, but with Maven 3.0.4, simply adding the two properties to the pom's properties enables Java 7 for me:

<properties>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
</properties>
2
  • 2
    My project was compiling fine, but then somehow a src folder got added to the pom.xml explicitly setting those two properties to 1.6 just for that particular folder, which made it harder to track down.
    – Steven P
    Mar 24, 2014 at 3:21
  • 1
    Seems people are saying it's not working, so I tried fresh w/maven 3.3.9: Generate project: mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.example -DartifactId=maven-sample then add List<String> list = new ArrayList<>(); in AppTest.java and verify source error on mvn test. Add properties to pom.xml. That fixed mvn test. Generated eclipse project: mvn eclipse:eclipse From eclipse 4.4.1: file->import->existing project. I'm then able to run the unit test from eclipse =) Seems best to ask a new question with what you are doing that's not working? May 3, 2016 at 18:50
2

perfectly nice explanation on the compatibility issue of jdk 1.7 with maven 2.2.1 given by Mark Peters Maven "could not parse error message" (Java 7 + Maven 2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.