【问题标题】:JDK tools.jar as maven dependencyJDK tools.jar 作为 Maven 依赖项
【发布时间】:2023-03-10 15:33:01
【问题描述】:

我想把 JDK tools.jar 作为编译依赖。我发现一些示例表明使用 systemPath 属性,如下所示:

<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 一样)。有人有想法吗?

【问题讨论】:

标签: java maven-2 tools.jar


【解决方案1】:

这就是配置文件的用途,提取属性的路径,为 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>

【讨论】:

  • 我运行的是 OS X 10.7 (Lion),这对我有用,但有趣的是我已经有一个用于 linux 机器 (unix) 的 *nix 配置文件。有了这两个配置文件,它就忽略了我的 mac 配置文件。因此,我可以更改 *nix 配置文件条目的路径,或者我必须注释掉该配置文件的配置文件,以便它可以看到我的 mac 配置文件
  • 如果您需要在 OS X 上同时支持 Apple Java 6 (Classes/classes.jar) 和 Oracle Java 7 (lib/tools.jar),这是行不通的,但 @Laurent 的回答会。
  • stackoverflow.com/a/29585979 对我来说似乎是 JDK 1.7、JDK 1.8 和 El Capitan 的更好答案。
  • 这个文件的路径和名称应该是什么?
【解决方案2】:

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

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

【讨论】:

  • classes.jar 存在性检查的好处是它回退到在 Mac 平台上使用 tools.jar。这对于将来在 Mac (link) 上发布的 OpenJDK 可能很重要,因为它可能有一个 tools.jar 而不是 classes.jar。
  • +1 因为这会测试我们所追求的实际文件并且不依赖于操作系统检测(无论如何这都不是必需的)
  • 更接近,但至少stackoverflow.com/a/29585979 最终是一个更好的解决方案(对于 JDK 1.7/1.8 和 El Capitan)。
【解决方案3】:

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

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

【讨论】:

  • 配置文件中的dependencies 部分不是必需的。在配置文件之外声明一次就足够了,然后让配置文件确定toolsjar 属性的正确值。
  • 这个依赖只有mac需要,最好只在这个profile里面声明
  • 不应该是&lt;toolsjar&gt;${java.home}/../Classes/classes.jar&lt;/toolsjar&gt; 而应该是&lt;toolsjar&gt;${java.home}/../lib/tools.jar&lt;/toolsjar&gt; 吗?
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    不知何故,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>
    

    【讨论】:

    • 这是一个在很多地方都被问到的老问题。因为这是 2016 年,我使用 El Capitan 并安装了 JDK 1.7 和 JDK 1.8,所以我认为这是在 STS(Eclipse)和 Maven(命令行)中解决此问题的最简单和最干净的方法,而无需创建符号链接或以某种方式修改已安装的系统。它第一次对我有用。 :) :) :)
    【解决方案6】:

    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>
    

    【讨论】:

      【解决方案7】:

      适合初学者的说明

      首先将此配置文件添加到标记上方的 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

      【讨论】:

        【解决方案8】:

        我的解决方案:

        1. 将 Sun 的 tools.jar 放到 $JAVA_HOME/lib
        2. 在名为 lib 的 $JAVA_HOME/.. 中创建符号链接,其中目标将是 $JAVA_HOME/lib

        【讨论】:

        • 这不是一个好的解决方案,因为它意味着在必须将 JAR 文件放在类路径中的每台机器上执行一些操作。
        • 我根据我在别处找到的解决方案尝试了这个和这个的变体,但没有得到我预期的结果。
        猜你喜欢
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多