2012-03-07 117 views
9

當我在我的pom.xml文件中,在我的系統上是Maven的定義而編制要使用哪個Java JDK版本如下沒有定義的東西(我已經安裝在系統上的多個版本,JAVA_HOME分之一他們)?指定Maven編譯器使用的JDK版本在哪裏?

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

我仍然看到提出的解決方案的問題。就像在我的用例中,我安裝了JDK1.8並克隆了一個GIT倉庫,我(覺得)不應該改變maven-compiler-plugin設置。我已經配置了使用1.7和1.8 JDK的。我如何在不更改提供的源代碼的情況下通知maven在其上使用? – jwilleke 2016-09-29 16:13:54

回答

9

Maven的醫生說

編譯器插件是用來編譯項目的源代碼。默認編譯器是javac,用於編譯Java源代碼。另請注意,目前的默認源設置爲1.5,默認目標設置爲1.5,與運行Maven的JDK無關。如果要更改這些默認值,你應該在設置Java編譯器的-source-target描述設置源和目標。

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

上有Maven的吉拉這個有趣的線程Change default source level to 1.5


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

編譯器插件用於編譯你的項目的來源。 3.0以來,默認的編譯器是javax.tools.JavaCompiler(如果你使用的是Java 1.6)和用於編譯Java源代碼。如果要使用javac強制插件,則必須配置插件選項forceJavacCompilerUse。

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

感謝nachteil指點出來。

+0

非常感謝您,請稍等。 – rapt 2012-03-07 15:07:32

+0

從版本3.0開始,這不是真的 - 請參閱下面的答案。 – nachteil 2015-03-09 07:38:06

+0

@nachteil更新,謝謝。 – Nishant 2015-03-09 08:06:02

1

您必須定義在你的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> 
5

簡單的使用性能

<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

從Maven的編譯器插件doucemntation:

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

我發現這篇文章通過搜索引擎,我認爲這是值得更新。 另外:-target-source選項不影響編譯器本身,而是它處理源代碼併產生輸出字節代碼的方式。