JDK tools.jar 作为 Maven 依赖项
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这就是配置文件的用途,提取属性的路径,为 Windows、OSX 等设置配置文件,并适当地定义属性值。
这是讨论操作系统配置文件的文档页面: Maven 本地设置模型
它最终应该看起来像这样:
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:
感谢您向我介绍 Maven 配置文件。
我已经使用了上面提到的配置文件,并根据所需文件的存在激活了配置文件:
我发布了这个答案以突出显示上一篇文章中的一个错误:属性部分只能在激活部分中使用才能激活配置文件基于指定属性的存在。为了定义属性,必须像上面一样使用属性部分。
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 :
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.
嗨,我知道你们都很聪明,但这让我花了几天时间才发现答案并不完整 - 配置文件和依赖项都是必要的。我希望没有人再在这上面浪费时间。请参阅下面我的完整代码:
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:
我在 Q 中找到了解决方案: 声明 maven 对 tools.jar 的依赖以在 JDK 9 上工作
I found a solution in Q: Declare maven dependency on tools.jar to work on JDK 9
不知何故,Windows 中的 Eclipse 无法获取 {java.home}。所以,我必须设置 JAVA_HOME 而不是 java.home。 JAVA_HOME 在“运行”->“运行配置”->“环境”中设置。这对我来说适用于标准 JDK(不是 Apple JDK)。
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
块之外的依赖项
。配置文件只是确定
${toolsjar}
将获得哪个值。The comment of Edward is correct.
You need the profile AND you need the
dependency
outside of theprofiles
block.The profile just determines which value
${toolsjar}
is gonna get.针对初学者的正确说明
首先将此配置文件添加到 Pom.xml 文件中标签上方或其他位置。
然后正确的 JRE 路径
转到:
选择已安装的 JRE 并双击它或从右侧菜单中单击“编辑”,然后确保 JRE 主路径位于 JDK 内部,如下所示:
如果您单独安装了 JRE,那么 eclipse 会选择独立的 JRE,如下所示:
因此将其更改为 JDK 自带的 JRE:
Proper instructions for beginners
First Add this profile to Pom.xml file above tag or somewhere else in it.
then Correct JRE path
Goto :
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:
if you have installed JRE seperatly then eclipse would have picked standalone JRE like:
so change it to JRE which come with JDK:
我的解决方案:
$JAVA_HOME/lib
中,$JAVA_HOME/..
中创建一个名为lib的符号链接,其中目标将是$JAVA_HOME /lib
my solution:
$JAVA_HOME/lib
$JAVA_HOME/..
named lib where target will be$JAVA_HOME/lib