Jdk options for Maven compiler for source and target

I have the following configuration in my pom.xml for maven-compiler-plugin.

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> 

What should be the source and target version of jdk? How does this depend on the version of jdk installed on my computer? How can they be different? for example, the installed jdk is 1.8, in the initial parameter is 1.6, target is 1.7.

+8
java maven
source share
1 answer

With source / target, you only define javac switches, which means creating compatible code. For example, if you installed jdk 8 and want to create java 7 runable classes. But it does not check if JDK 8 is installed at all. This will also work if you have JDK 7 installed.

If you really need to check the version of the JDK that is installed, you need to go through the maven-enforcer-plugin and check the installed JDK ...

 <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>enforce-java</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireJavaVersion> <version>1.8.0</version> </requireJavaVersion> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build> [...] </project> 

With the above, you can no longer build JDK 7 ... only with JDK 8 ...

BTW: Why are you using such an old version of maven-compiler-plugin ?

+10
source share

All Articles