簡體   English   中英

指定Maven編譯器使用的JDK版本在哪里?

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

當我沒有在我的pom.xml文件中定義如下內容時,在我的系統中為Maven定義了在編譯時使用哪個版本的Java JDK(我的系統上安裝了多個版本, JAVA_HOME指向其中一個版本) ?

<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>

Maven醫生說

Compiler Plugin用於編譯項目的源代碼。 默認編譯器是javac,用於編譯Java源代碼。 另請注意, 目前默認的源設置為1.5,默認目標設置為1.5,與您運行Maven的JDK無關。 如果要更改這些默認值,則應設置源和目標,如設置Java編譯器的-source-target中所述。

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

Maven的Jira Change默認源級別為1.5,這個有趣的線程


編輯:
Maven 3.0及更高版本的更新:

Compiler Plugin用於編譯項目的源代碼。 從3.0開始,默認編譯器是javax.tools.JavaCompiler (如果您使用的是java 1.6)並且用於編譯Java源代碼。 如果要使用javac強制插件,則必須配置插件選項forceJavacCompilerUse。

資料來源: http//maven.apache.org/plugins/maven-compiler-plugin/index.html

感謝nachteil指出它。

只需使用屬性

<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>

從maven編譯器插件doucemntation:

從3.0開始,默認編譯器是javax.tools.JavaCompiler(如果您使用的是java 1.6)並且用於編譯Java源代碼。 如果要使用javac強制插件,則必須配置插件選項forceJavacCompilerUse。

我通過搜索引擎找到了這篇文章,我覺得值得更新。 另外: -target-source選項不會影響編譯器本身,而是影響源代碼處理和生成輸出字節代碼的方式。

您必須在maven setting.xml文件中定義屬性。 該屬性是您的第二個javac路徑。(D:\\ dev \\ java \\ ibm \\ java1.6.0 \\ bin \\ javac)在pom文件中將此屬性用於maven-compiler-plugin之后。

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM