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

我的未来我决定 提交于 2020-01-01 04:41:07

问题


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>

回答1:


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.




回答2:


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>



回答3:


From maven compiler plugin doucemntation:

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.

I found this post via search engine and I think it is worth updating. Also: the -target and -source options do not affect the compiler itself, but rather the way it handles source code and produces output byte code.




回答4:


You must define a property in your maven setting.xml file. The property is your second javac path.(D:\dev\java\ibm\java1.6.0\bin\javac)After use this property for maven-compiler-plugin in your pom file.

setting.xml

 <settings>
    <profiles>
      <profile>
          <id>IBM_JAVA</id>
            <properties>
              <IBM_JAVA_1_6_JAVAC>D:\dev\java\ibm\java1.6.0\bin\javac</IBM_JAVA_1_6_JAVAC>
            </properties>
      </profile>
    </profiles>
    <activeProfiles>     
        <activeProfile>IBM_JAVA</activeProfile>   
    </activeProfiles>
    </settings> 

pom.xml

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
            <fork>true</fork>
            <executable>${IBM_JAVA_1_6_JAVAC}</executable>
            <encoding>UTF-8</encoding>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>


来源:https://stackoverflow.com/questions/9603591/where-is-the-jdk-version-to-be-used-by-maven-compiler-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!