猿问

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


慕妹3242003
浏览 1021回答 3
3回答

有只小跳蛙

这就是配置文件的用途,提取属性的路径,为Windows,OSX等设置配置文件,并适当地定义属性值。这是讨论操作系统配置文件的文档页面:Maven本地设置模型它应该最终看起来像这样:&nbsp; <profiles>&nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; <id>windows_profile</id>&nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; <os>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <family>Windows</family>&nbsp; &nbsp; &nbsp; &nbsp; </os>&nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <toolsjar>${java.home}/../lib/tools.jar</toolsjar>&nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; </profile>&nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; <id>osx_profile</id>&nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; <os>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <family>mac</family>&nbsp; &nbsp; &nbsp; &nbsp; </os>&nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>&nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; </profile>&nbsp; </profiles>

胡子哥哥

感谢您向我介绍Maven个人资料。我已经使用了如上所述的配置文件,并根据所需文件的存在来激活配置文件:<profiles>&nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; &nbsp; <id>default-profile</id>&nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>true</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <file>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exists>${java.home}/../lib/tools.jar</exists>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </file>&nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <toolsjar>${java.home}/../lib/tools.jar</toolsjar>&nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; </profile>&nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; &nbsp; <id>mac-profile</id>&nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>false</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <file>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exists>${java.home}/../Classes/classes.jar</exists>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </file>&nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>&nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; </profile></profiles>我在前一篇文章中发布了此答案以强调一个错误:属性部分只能在激活部分中使用,以便基于指定属性的存在来激活配置文件。为了定义属性,必须像上面一样使用properties部分。

函数式编程

嗨,我知道你们都很聪明,但是这让我花了几天的时间才弄清楚答案是不完整的-配置文件和依赖项都是必需的。我希望没有人会再浪费时间在这上面。请在下面查看我的完整代码:<profiles>&nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; &nbsp; <id>osx_profile</id>&nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>false</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <os>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <family>mac</family>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </os>&nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>&nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; &nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.sun</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>tools</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.6.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>system</scope>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <systemPath>${toolsjar}</systemPath>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; </dependencies>&nbsp; &nbsp; </profile></profiles>
随时随地看视频慕课网APP

相关分类

Java
我要回答