2010-06-20 48 views
69

我想將JDK tools.jar作爲編譯依賴項。我發現了一些例子,指示使用Systempath下屬性如下所示:作爲maven dependency的JDK tools.jar

<dependency> 
    <groupId>com.sun</groupId> 
    <artifactId>tools</artifactId> 
    <scope>system</scope> 
    <systemPath>${java.home}/../lib/tools.jar</systemPath> 
</dependency> 

的問題是,路徑不正確的Mac OS X(但是它是Windows和Linux是正確的)。對於它,正確的路徑是$ {java.home} /../ Classes/classes.jar

我正在尋找一種方法來定義一個maven屬性,例如,如果系統被檢測爲Mac Os X,則值設置爲$ {java.home} /../ Classes/classes.jar,否則它被設置爲爲$ {java.home} /../ lib/tools.jar(就像ANT可以做的那樣)。有人有想法嗎?

回答

43

這就是配置文件的用途,提取屬性的路徑,爲windows,OSX等設置配置文件,並適當地定義屬性值。

下面是討論型材操作系統的文檔頁面:Maven Local Settings Model

應該endup看起來像這樣:

<profiles> 
    <profile> 
     <id>windows_profile</id> 
     <activation> 
     <os> 
      <family>Windows</family> 
     </os> 
     </activation> 
     <properties> 
     <toolsjar>${java.home}/../lib/tools.jar</toolsjar> 
     </properties> 
    </profile> 
    <profile> 
     <id>osx_profile</id> 
     <activation> 
     <os> 
      <family>mac</family> 
     </os> 
     </activation> 
     <properties> 
     <toolsjar>${java.home}/../Classes/classes.jar</toolsjar> 
     </properties> 
    </profile> 
    </profiles> 
+0

我運行OS X 10.7(獅子),這工作對我來說,除了有趣的是我已經爲Linux盒( UNIX)* nix的輪廓。在這兩個配置文件中,它忽略了我的配置文件 mac。因此,我可以更改* nix配置文件條目的路徑,或者必須註釋掉該配置文件的配置文件,以便它可以看到我的配置文件 mac 2011-10-17 20:23:03

+3

如果您需要同時支持Apple Java 6('Classes/classes。 jar')和Oracle Java 7('lib/tools.jar')在OS X上,這將不起作用,但@ Laurent的答案會。 – 2012-12-04 21:48:45

+1

http://stackoverflow.com/a/29585979似乎是對我來說JDK 1.7,JDK 1.8和El Capitan更好的答案。 – 2016-02-05 16:25:11

36

謝謝你把我介紹Maven的配置文件。只有地產板塊能夠:

我用輪廓上方,並通過激活基於所需文件的存在的輪廓中提到:

<profiles> 
    <profile> 
     <id>default-profile</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
      <file> 
       <exists>${java.home}/../lib/tools.jar</exists> 
      </file> 
     </activation> 
     <properties> 
      <toolsjar>${java.home}/../lib/tools.jar</toolsjar> 
     </properties> 
    </profile> 
    <profile> 
     <id>mac-profile</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <file> 
       <exists>${java.home}/../Classes/classes.jar</exists> 
      </file> 
     </activation> 
     <properties> 
      <toolsjar>${java.home}/../Classes/classes.jar</toolsjar> 
     </properties> 
    </profile> 
</profiles> 

我張貼了這個答案要突出在以前的帖子一個錯誤在激活部分使用,以便根據指定屬性的存在激活配置文件。爲了定義屬性,必須像上面那樣使用屬性部分。

+0

關於classes.jar的存在檢查的好處是它可以回到在Mac平臺上使用tools.jar。這可能對Mac上發佈的未來OpenJDK很重要([link](http://openjdk.java.net/projects/macosx-port/)),因爲它可能有一個tools.jar而不是classes.jar。 – prunge 2011-04-12 04:48:08

+0

+1,因爲這測試了我們之後的實際文件,並且不依賴OS檢測(反正這不是必需的) – 2013-09-09 15:42:25

+0

更接近,但http://stackoverflow.com/a/29585979最終是更好的解決方案(對於JDK至少1.7/1.8和El Capitan)。 – 2016-02-05 16:26:34

-11

我的解決方案:

  1. 把太陽的tools.jar到$JAVA_HOME/lib
  2. 使在$JAVA_HOME/..一個符號鏈接名爲lib目錄下的目標將是$JAVA_HOME/lib
+8

這不是一個好的解決方案,因爲它意味着在JAR文件必須放在類路徑中的每臺機器上執行一些操作。 – Laurent 2010-08-24 18:09:41

+0

我嘗試過這種方式,並根據我在其他地方找到的解決方案進行了變化,結果沒有得到我期望的結果。 – 2016-02-05 16:29:14

9

您好我知道你們是都很聰明,但是這讓我花了幾天的時間才發現答案並不完整 - 配置文件和依賴項都是必需的。我希望沒有人會再次浪費時間。請參閱下面的完整代碼:

<profiles> 
    <profile> 
     <id>osx_profile</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <os> 
       <family>mac</family> 
      </os> 
     </activation> 
     <properties> 
      <toolsjar>${java.home}/../Classes/classes.jar</toolsjar> 
     </properties> 
     <dependencies> 
      <dependency> 
       <groupId>com.sun</groupId> 
       <artifactId>tools</artifactId> 
       <version>1.6.0</version> 
       <scope>system</scope> 
       <systemPath>${toolsjar}</systemPath> 
      </dependency> 
     </dependencies> 
    </profile> 
</profiles> 
+2

配置文件中的「依賴關係」部分不是必需的。在配置文件之外聲明一次就足夠了,然後讓配置文件確定'toolsjar'屬性的正確值。 – 2012-10-24 01:54:06

+1

此依賴關係僅適用於mac,最好只在此配置文件中聲明 – Jianyu 2012-12-01 07:20:02

+0

不應該是行' $ {java.home} /../ Classes/classes.jar'而不是' $ {java.home } /../ lib/tools.jar'? – Jens 2013-06-21 08:28:53

8

不知何故,Windows中的eclipse未能拿起{java.home}。所以,我必須設置JAVA_HOME而不是java.home。 JAVA_HOME在Run-> Run Configurations-> Environment中設置。這對我來說是標準的JDK(不是Apple JDK)。

<profiles> 
     <profile> 
      <id>windows-profile</id> 
      <activation> 
       <activeByDefault>true</activeByDefault> 
       <file> 
        <exists>${JAVA_HOME}/lib/tools.jar</exists> 
       </file> 
      </activation> 
      <properties> 
       <toolsjar>${JAVA_HOME}/lib/tools.jar</toolsjar> 
      </properties> 
     </profile> 
     <profile> 
      <id>mac-profile</id> 
      <activation> 
       <activeByDefault>false</activeByDefault> 
       <file> 
        <exists>${java.home}/../lib/tools.jar</exists> 
       </file> 
      </activation> 
      <properties> 
       <toolsjar>${java.home}/../lib/tools.jar</toolsjar> 
      </properties> 
     </profile> 
    </profiles> 


    <dependencies> 
     <dependency> 
       <groupId>jdk.tools</groupId> 
       <artifactId>jdk.tools</artifactId> 
       <version>jdk1.8.0</version> 
       <scope>system</scope> 
       <systemPath>${toolsjar}</systemPath> 
      </dependency> 
     </dependencies> 
+1

這是一個在很多地方問過的老問題。因爲這是2016年,我使用El Capitan和JDK 1.7和JDK 1.8,我認爲這是在STS(Eclipse)和Maven(命令行)中解決這個問題最簡單,最簡單的方法,無需創建符號鏈接或以某種方式處理安裝的系統。它第一次爲我工作。 :) :) :) – 2016-02-05 16:21:08

1

Edward的評論是正確的。

您需要該配置文件,並且您需要profiles塊之外的dependency。 配置文件只是確定哪個值${toolsjar}會得到。

<dependencies> 
    <dependency> 
     <groupId>jdk.tools</groupId> 
     <artifactId>jdk.tools</artifactId> 
     <version>jdk1.8.0</version> 
     <scope>system</scope> 
     <systemPath>${toolsjar}</systemPath> 
    </dependency> 
</dependencies> 
相關問題