Maven : Missing artifact com.sun:tools:jar:1.6.0 compile time exception in POM.xml [duplicate]

被刻印的时光 ゝ 提交于 2019-12-06 03:52:19

问题


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)

I have set my JAVA_HOME variable as below:

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.

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


回答1:


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.

In case you want to refer to an environment variable within your pom file, use the below syntax.

${env.variable_name}

In your case, it should be ${env.JAVA_HOME} as seen below

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




回答2:


In Maven, ${java.home} points to the JRE directory used by the JDK, not JDK itself. Please see this question:

Java_home in Maven

So instead of

${java.home}/lib/tools.jar

which assumes the JDK directory you should have used

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

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

This is how Oracle handles it in one of their POMs:

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

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

See also the following posts:

  • JDK tools.jar as maven dependency
  • Build error: missing artifact com.sun:tools:jar:1.6
  • missing artifact sun.jdk:tools:jar:1.6.0:system
  • Missing artifact com.sun:tools:jar:1.5.0 maven


来源:https://stackoverflow.com/questions/26422259/maven-missing-artifact-com-suntoolsjar1-6-0-compile-time-exception-in-pom-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!