Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the JDK version to be used by Maven compiler specified?

When I do not define something as follows in my pom.xml file, where on my system is it defined for Maven which version of Java JDK to use while compiling (I have several versions installed on my system, JAVA_HOME points to one of them)?

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
</build>
like image 318
rapt Avatar asked Mar 07 '12 14:03

rapt


People also ask

Which JDK is used by Maven?

The Maven tool uses JDK version 11.0.

Where is Maven Java version?

Method 1: Change the JAVA_HOME pathMaven uses the JAVA_HOME environment variable to find which Java version it is supposed to run. If a different version of Java is needed, set the JAVA_HOME to the path of the specific version of JDK.

Is JDK required for Maven?

Maven always uses the JDK specified by JAVA_HOME , no matter how many JDK installations are available on the system. This allows the user the flexibility to change JDKs as required or based on the project. Hence, it is important to ensure JAVA_HOME is defined.


2 Answers

Maven doc says

The Compiler Plugin is used to compile the sources of your project. The default compiler is javac and is used to compile Java sources. Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

ref: http://maven.apache.org/plugins/maven-compiler-plugin/index.html

There is this interesting thread on Maven's Jira Change default source level to 1.5


EDIT:
Update for Maven 3.0 and later:

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Source: http://maven.apache.org/plugins/maven-compiler-plugin/index.html

Thanks nachteil for pointing it out.

like image 87
Nishant Avatar answered Nov 12 '22 12:11

Nishant


simply use properties

<properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.test.skip>true</maven.test.skip>
</properties>
like image 22
Kalpesh Soni Avatar answered Nov 12 '22 10:11

Kalpesh Soni