JDK tools.jar作为maven依赖

Lau*_*ent 73 java maven-2 tools.jar

我想把JDK tools.jar作为编译依赖项.我找到了一些示例,表明要使用systemPath属性,如下所示:

<dependency>
  <groupId>com.sun</groupId>
  <artifactId>tools</artifactId>
  <scope>system</scope>
  <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

问题是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一样).有人有想法吗?

sbl*_*ndy 45

这就是配置文件的用途,提取属性的路径,设置窗口的配置文件,OSX等,并适当地定义属性值.

下面是讨论型材操作系统的文档页面:Maven的本地设置模式

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

  <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>
Run Code Online (Sandbox Code Playgroud)

  • 如果你需要在OS X上支持Apple Java 6(`Classes/classes.jar`)和Oracle Java 7(`lib/tools.jar`),这将不起作用,但@Laurent的答案将会有效. (3认同)

Lau*_*ent 39

感谢您介绍我的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>
Run Code Online (Sandbox Code Playgroud)

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


Jia*_*nyu 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>
Run Code Online (Sandbox Code Playgroud)

  • 配置文件中的`dependencies`部分不是必需的.只需在配置文件之外声明一次,然后让配置文件确定`toolsjar`属性的正确值即可. (2认同)

Ani*_*rma 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>
Run Code Online (Sandbox Code Playgroud)


use*_*610 7

我在 Q: Declare maven dependency on tools.jar 中找到了一个解决方案,以在 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>
Run Code Online (Sandbox Code Playgroud)