Reputation: 1664
I am trying to test cross compilation between modules. I have a module which uses java 1.8 and another module uses Java 11. I am trying to use Java 11 module as dependency on Java 1.8 project. When I try to compile Java 11 module, maven shows an error.
<project>
<dependencies>
<dependency>
<groupId>com.berkin</groupId>
<artifactId>java</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.berkin</groupId>
<artifactId>java</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
public class Test {
public void test(){
System.out.println(" ".isEmpty()); //true
}
}
In theory I should able to compile secondary module with java 11 compiler and use it in min Java 8 JRE. Thus I can also use it in Java 8 project as a dependency. Where am I missing?
ERROR
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project java: Fatal error compiling: invalid source release: 11 -> [Help 1]
Thanks.
Upvotes: 0
Views: 1204
Reputation: 1664
I get it thanks to @Elliott Frisch
There are several terms;
Java Compiler : It is defined by JAVA_HOME or if you override by selecting another Java version for Maven thats it. You can compile Java 6 codes with Java 11 compiler. Because it has backward compatibility.
Source : Minimum Java version which you want to use java features. For example, if I want to use lambdas, at least I have to use source 8. If I want to use Java 11 features, I need to set it to 11. Its all about Java Features that you want to use
Target : Minimum JRE version which can run my features. E.g. if I set Java 8, I need at least JRE 8 to use lambdas. I can't use JRE 7 or lower because they don't know what lambda is.
Also
If java compiler version is 8, it can't compile source 11 classes. Because compiler don't know how to compile Java 11. I failed at the beginning because I had lower than source. When I changed into 11 & 11, I failed again because this time compiler was older(It was Java 8)
Thanks
Upvotes: 0
Reputation: 12335
The main reason is that you cannot use a source
value that is lower than the target
value. If you want the code to be compatible with Java 1.8, lower the source to 1.8
Upvotes: 1