80

I would like to put JDK tools.jar as compile dependency. I found some examples that indicate to use the systemPath property like the following:

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

The problem is that the path is not correct for Mac Os X (however it is correct for Windows and Linux). For it, the correct path is ${java.home}/../Classes/classes.jar.

I am looking for a way in order to define a maven property such that if system is detected as Mac Os X, value is set to ${java.home}/../Classes/classes.jar, otherwise it is set to ${java.home}/../lib/tools.jar (like it is possible to do with ANT). Does someone has an idea ?

4

8 回答 8

47

这就是配置文件的用途,提取属性的路径,为 Windows、OSX 等设置配置文件,并适当地定义属性值。

这是讨论操作系统配置文件的文档页面:Maven Local Settings Model

它最终应该看起来像这样:

  <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>
于 2010-06-20T18:43:00.567 回答
40

感谢您向我介绍 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>

我发布了这个答案以突出上一篇文章中的一个错误:属性部分只能在激活部分中使用,以便根据指定属性的存在来激活配置文件。为了定义一个属性,必须像上面一样使用属性部分。

于 2010-06-20T19:42:18.297 回答
10

嗨,我知道你们都很聪明,但这让我花了几天时间才发现答案并不完整——配置文件和依赖项都是必要的。我希望没有人会再在这件事上浪费时间。请在下面查看我的完整代码:

<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>
于 2012-08-19T07:37:29.250 回答
8

我在 Q 中找到了一个解决方案:Declare maven dependency on tools.jar to work on JDK 9

由于实际的 maven 巫术非常复杂,令新手感到惊讶并且是未来改进的主题,因此最好不要复制粘贴它。因此,此模块存在,因此您不必了解或关心细节。~~ https://github.com/olivergondza/maven-jdk-tools-wrapper

<dependency>
  <groupId>com.github.olivergondza</groupId>
  <artifactId>maven-jdk-tools-wrapper</artifactId>
  <version>0.1</version>
</dependency>
于 2018-09-10T20:38:51.720 回答
7

不知何故,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>
于 2015-04-12T04:32:27.663 回答
1

爱德华的评论是正确的。

您需要配置文件并且您需要块的dependency外部profiles。配置文件只是确定${toolsjar}将获得哪个值。

<dependencies>
    <dependency>
        <groupId>jdk.tools</groupId>
        <artifactId>jdk.tools</artifactId>
        <version>jdk1.8.0</version>
        <scope>system</scope>
        <systemPath>${toolsjar}</systemPath>
    </dependency>
</dependencies>
于 2017-12-14T14:47:05.173 回答
0

对初学者的正确指导

首先将此配置文件添加到标记上方的 Pom.xml 文件或其中的其他位置。

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

然后更正 JRE 路径

去 :

Windows > Preferecnes > 已安装的 JRE

选择安装的 JRE 并双击它或从右键菜单中单击编辑,然后确保 JRE 主路径位于 JDK 内部,例如:

C:\Program Files\Java\jdk1.8.0_181\jre

如果您已经单独安装了 JRE,那么 eclipse 会选择独立的 JRE,例如:

C:\Program Files\Java\jre1.8.0_181\

所以把它改成JDK自带的JRE:

C:\Program Files\Java\jdk1.8.0_181\jre

于 2018-10-14T09:46:23.117 回答
-11

我的解决方案:

  1. 把 Sun 的 tools.jar 放到$JAVA_HOME/lib
  2. $JAVA_HOME/..命名的库中创建一个符号链接,其中目标将是$JAVA_HOME/lib
于 2010-08-24T17:53:23.903 回答