JDK tools.jar 作为 Maven 依赖项

发布于 2024-09-06 12:37:10 字数 631 浏览 5 评论 0原文

我想将 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 一样)。有人有主意吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

粉红×色少女 2024-09-13 12:37:10

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

That's what profiles are for, extract the path to a property, setup profiles for windows, OSX, etc, and define the property values appropriately.

Here's the doc page that discussing profiles for OSes: Maven Local Settings Model

It should endup looking something like this:

  <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>
隔纱相望 2024-09-13 12:37:10

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

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

Thank you for introducing me maven profiles.

I have used profile as mentioned above and by activating a profile based on the presence of the desired file :

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

I posted this answer to highlight a mistake in the previous post : the property section can only be used in activation section in order to activate a profile based on the existence of the specified property. In order to define a property, the properties section must be used like above.

a√萤火虫的光℡ 2024-09-13 12:37: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>

Hi I know you guys are all smart, but it caused me couple of days to figure out the answer is not complete - both the profile and the dependency is necessary. I hope no one will waste time on this again. Please see my complete code below:

<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>
寄离 2024-09-13 12:37:10

我在 Q 中找到了解决方案: 声明 maven 对 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>

I found a solution in Q: Declare maven dependency on tools.jar to work on JDK 9

As the actual maven wizardry is quite elaborate, surprising to newcomers and a subject of future improvements, it is better not co copy-paste it around. Hence this module exists so you do not have to know or care about the details. ~~ 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>
§普罗旺斯的薰衣草 2024-09-13 12:37:10

不知何故,Windows 中的 Eclipse 无法获取 {java.home}。所以,我必须设置 JAVA_HOME 而不是 java.home。 JAVA_HOME 在“运行”->“运行配置”->“环境”中设置。这对我来说适用于标准 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>

Somehow, the eclipse in windows fails to pick up {java.home}. So, I had to set JAVA_HOME instead of java.home. JAVA_HOME was set in Run->Run Configurations->Environment. This worked for me with standard JDK(not 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>
雪花飘飘的天空 2024-09-13 12:37:10

爱德华的评论是正确的。

您需要配置文件,并且需要 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>

The comment of Edward is correct.

You need the profile AND you need the dependency outside of the profiles block.
The profile just determines which value ${toolsjar} is gonna get.

<dependencies>
    <dependency>
        <groupId>jdk.tools</groupId>
        <artifactId>jdk.tools</artifactId>
        <version>jdk1.8.0</version>
        <scope>system</scope>
        <systemPath>${toolsjar}</systemPath>
    </dependency>
</dependencies>
只有影子陪我不离不弃 2024-09-13 12:37:10

针对初学者的正确说明

首先将此配置文件添加到 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 >偏好>安装的 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

Proper instructions for beginners

First Add this profile to Pom.xml file above tag or somewhere else in it.

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

then Correct JRE path

Goto :

Windows > Preferecnes > Installed JREs

selected intalled JRE and double click on it or from right menu click edit and then make sure JRE Home path is inside JDK something like:

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

if you have installed JRE seperatly then eclipse would have picked standalone JRE like:

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

so change it to JRE which come with JDK:

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

东北女汉子 2024-09-13 12:37:10

我的解决方案:

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

my solution:

  1. put the Sun's tools.jar to the $JAVA_HOME/lib
  2. make a symlink in the $JAVA_HOME/.. named lib where target will be $JAVA_HOME/lib
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文