Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which compiler version maven is using here?

I am using below maven compiler plugin to compile my java code:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <inherited>true</inherited>
    <version>2.5.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

My java_home points to Java 7.

My first question, will my classes be compiled with java 6 or java 7?

If with java 6 (because of <source>1.6</source> ), how maven will know the path java 1.6 as java home points to 1.7?

If I need to compile the source code with java 1.8, do I need to set source and target as 1.8?

But then, how maven will know that jdk 1.8 is in the path?

Do i need to change java_home to point to java 8?

like image 788
emilly Avatar asked May 02 '16 17:05

emilly


People also ask

Which compiler does Maven use?

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.

What is Maven compiler version?

Apache Maven Compiler Plugin/ compiler:compile. | Last Published: 2022-03-08. Version: 3.10.1.

How do I know what version of Java Maven is using?

mvn -version will output which java it's using. If JAVA_HOME is set to a valid JDK directory and Maven is using something else, then most likely someone has tampered with the way that Maven starts up. You can define the Java to be used with maven.

What is the use of Maven compiler source?

The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle: compile – compile main source files. testCompile – compile test source files.


1 Answers

Short answers:

  1. Since JAVA_HOME points to Java 1.7, the javac program (the compiler) from Java 1.7 will be used. However, since the source and target are both 1.6, the command mvn compile will generate classes that are runnable on JRE 1.6. This would be evident if you saw the major and minor version number associated with the generated class file using the javap utility. For Java 1.8, these values are 52 and 0, for Java 1.7, they are 51 and 0 and for Java 1.6 they are 50 and 0. You might wonder why you would ever want a Java 1.7 compiler to generate classes with target = 1.6. The reasons are based on the runtime (JRE) that you want to run the classes on. If your compiler and runtime always agree on the version, you may not use these, but these upgrades need to be coordinated in large teams and in the meanwhile, you should always try to remain as close to the latest versions of the software (one major reason: you want to get the bug fixes). The other thing to keep in mind is that source release 1.n requires target release 1.n.

  2. Yes, change those values <source> and <target> to 1.8 (there's perhaps a shortcut, but let's do that later ;)). Make sure that JAVA_HOME (and PATH) points to JDK 1.8, since the maven-compiler-plugin ultimately delegates to the javac program in the PATH with -source and -target arguments that come from the plugin's <configuration>. See the output of mvn -X compile, you will get something like:


[DEBUG]   (f) source = 1.8 
[DEBUG]   (f) staleMillis = 0 
[DEBUG]   (f) target = 1.8 
[DEBUG]   (f) useIncrementalCompilation = true 
[DEBUG]   (f) verbose = false 
[DEBUG] -- end configuration -- 
[DEBUG] Using compiler 'javac'.

Beware the following warning from Maven docs:

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs.

like image 121
Kedar Mhaswade Avatar answered Oct 02 '22 01:10

Kedar Mhaswade