Bazinga!
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project…: Fatal error compiling: invalid target release: 1.8.0_11 -> [Help 1]
To summarize, I want to compile a maven project using Java 8, that is possible thanks to the JAVA_HOME configuration:
1 2 3 4 5 6 7 |
$ mvn -version Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00) Maven home: /home/jbeneito/Applications/apache-maven-3.2.1 Java version: 1.8.0_11, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-oracle/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.13.0-32-generic", arch: "amd64", family: "unix" |
But I copied the maven compiler plugin configuration from another maven project without putting special attention:
1 2 3 4 5 6 7 8 9 10 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> |
In programming the error usually contains the solution. I’m trying to use the current Java version (1.8.0_11) as the option for the javac command, that is not the expected one.
${java.version} is a environment variable whose value is currently 1.8.0_11, but the Maven Compiler Plugin expects the standard options of the javac command for Setting the -source and -target of the Java Compiler.
That is:
- 1.3 for Java SE 1.3
- 1.4 for Java SE 1.4
- 1.5 for Java SE 5
- 5 synonym for 1.5
- 1.6 for Java SE 6
- 6 synonym for 1.6
- 1.7 the default value. The compiler accepts code with features introduced in Java SE 7. (Yes, as it appears in the Java 8 official documentation)
- 7 Synonym for 1.7
Actually I only use the values with the format 1.X, that includes 1.8, that I assume is the default value for Java 8 SE.
Why did I use this variable?
Because in my previous project it wasn’t an environment variable, but a custom property in the pom.xml:
1 2 3 4 5 |
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> ... </properties> |
But this time I have another two custom properties:
1 2 3 4 |
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> |
Thus the solution is easy:
- Try to not copy-paste
- Pay attention in any case
- Just use the appropriate variables
1 2 3 4 5 6 7 8 9 10 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${maven.compiler.target}</source> <target>${maven.compiler.source}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> |