Where is the JDK version to be used by Maven compi

2019-03-26 05:58发布

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>

4条回答
2楼-- · 2019-03-26 06:08

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.

查看更多
We Are One
3楼-- · 2019-03-26 06:16

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>
查看更多
走好不送
4楼-- · 2019-03-26 06:17

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.

查看更多
Viruses.
5楼-- · 2019-03-26 06:21

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>
查看更多
登录 后发表回答