Java Maven:POM.xml 中缺少工件 com.sun:tools:jar:1.6.0 编译时异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26422259/
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
Maven : Missing artifact com.sun:tools:jar:1.6.0 compile time exception in POM.xml
提问by sTg
I am getting one weird issue and getting a compile time exception in my pom.xml when i am trying to add dependancy for tools. jar displayed as below(Missing artifact com.sun:tools:jar:1.6.0)
当我尝试为工具添加依赖项时,我遇到了一个奇怪的问题,并且在我的 pom.xml 中出现了编译时异常。jar 显示如下(缺少工件 com.sun:tools:jar:1.6.0)
I have set my JAVA_HOME variable as below:
我已经将我的 JAVA_HOME 变量设置如下:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_34
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_34
When i hardcode it to the actual path of JDK1.6 i dont find any error as below.
当我将它硬编码到 JDK1.6 的实际路径时,我没有发现任何错误,如下所示。
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>C:\Program Files\Java\jdk1.6.0_34\lib\tools.jar</systemPath>
</dependency>
but i know its not good practise. Request guidance in resolving this error.
但我知道这不是一个好习惯。请求指导以解决此错误。
采纳答案by coderplus
java.home
is a System property which generally points to the jre directory and you are getting an error as you have pointed to a jar which doesn't exist.
java.home
是一个 System 属性,它通常指向 jre 目录,并且您会收到错误,因为您指向了一个不存在的 jar。
In case you want to refer to an environment variable within your pom file, use the below syntax.
如果要在 pom 文件中引用环境变量,请使用以下语法。
${env.variable_name}
In your case, it should be ${env.JAVA_HOME}
as seen below
在你的情况下,它应该${env.JAVA_HOME}
如下所示
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
Update:As lexicore has mentioned, this wont work with MAC as the MAC JDK has a different file structure.
更新:正如 lexicore 所提到的,这不适用于 MAC,因为 MAC JDK 具有不同的文件结构。
回答by lexicore
In Maven, ${java.home} points to the JRE directory used by the JDK, not JDK itself. Please see this question:
在 Maven 中,${java.home} 指向 JDK 使用的 JRE 目录,而不是 JDK 本身。请看这个问题:
So instead of
所以代替
${java.home}/lib/tools.jar
which assumes the JDK directory you should have used
假设您应该使用的 JDK 目录
${java.home}/../lib/tools.jar
However, this is only a half of the solution. The problem is that under Mac, the directory structure is different. You have to user profiles in order to make your build relibaly work cross-platform.
然而,这只是解决方案的一半。问题是在Mac下,目录结构不同。您必须使用用户配置文件才能使您的构建 relibaly 跨平台工作。
Please see this question:
请看这个问题:
JDK tools.jar as maven dependency
And, specificaly, this answer(which IS the correct answer and not the one accepted by the OP there).
而且,特别是这个答案(这是正确的答案,而不是那里的 OP 接受的答案)。
This is how Oracle handles it in one of their POMs:
这是 Oracle 在其 POM 之一中处理它的方式:
<!-- JDK dependencies -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${tools.jar}</systemPath>
</dependency>
And then in profiles
:
然后在profiles
:
<profile>
<id>default-tools.jar</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../lib/tools.jar</tools.jar>
</properties>
</profile>
<profile>
<id>default-tools.jar-mac</id>
<activation>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../Classes/classes.jar</tools.jar>
</properties>
</profile>
On Mac JDK has a different file structure. This is why you have to define these profiles.
在 Mac 上,JDK 具有不同的文件结构。这就是您必须定义这些配置文件的原因。
See also the following posts:
另请参阅以下帖子: