Java JDK tools.jar 作为 maven 依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3080437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:16:43  来源:igfitidea点击:

JDK tools.jar as maven dependency

javamaven-2tools.jar

提问by Laurent

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

我想将 JDK tools.jar 作为编译依赖项。我发现了一些指示使用systemPath属性的示例,如下所示:

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

问题是路径对于 Mac Os X 不正确(但对于 Windows 和 Linux 是正确的)。对于它,正确的路径是${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 ?

我正在寻找一种方法来定义一个 maven 属性,如果系统被检测为 Mac Os X,则值设置为${java.home}/../Classes/classes.jar,否则设置为 $ {java.home}/../lib/tools.jar(就像使用 ANT 一样)。有人有想法吗?

采纳答案by Laurent

Thank you for introducing me maven profiles.

感谢您向我介绍 Maven 配置文件。

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.

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

回答by sblundy

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

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

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

这是讨论操作系统配置文件的文档页面: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>

回答by toTem

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
  1. 将 Sun 的 tools.jar 放到 $JAVA_HOME/lib
  2. $JAVA_HOME/..在目标将在的命名库中创建符号链接$JAVA_HOME/lib

回答by Jianyu

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>

回答by Animesh Sharma

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

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

回答by Marty

The comment of Edwardis correct.

爱德华的评论是正确的。

You need the profile AND you need the dependencyoutside of the profilesblock. The profile just determines which value ${toolsjar}is gonna get.

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

回答by user7610

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

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

由于实际的 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>

回答by user889030

Proper instructions for beginners

给初学者的正确说明

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

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

then Correct JRE path

然后更正JRE路径

Goto :

去 :

Windows > Preferecnes > Installed JREs

Windows > Preferecnes > 已安装的 JRE

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:

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

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

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

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

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

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

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

so change it to JRE which come with JDK:

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

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

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