Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK tools.jar as maven dependency

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 ?

like image 950
Laurent Avatar asked Jun 20 '10 18:06

Laurent


People also ask

Are Maven dependencies included in jar?

Apache Maven Shade Plugin provides the capability to package the artifact in an uber-jar, which consists of all dependencies required to run the project.

Is tools jar available in JDK 11?

I realised no tools. jar exists in the Windows build of OpenJDK 11.

What JDK is Maven?

The Maven tool uses JDK version 11.0. 10. The default JDK is set to 13.0.


2 Answers

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:

  <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> 
like image 139
sblundy Avatar answered Oct 13 '22 04:10

sblundy


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 :

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

like image 31
Laurent Avatar answered Oct 13 '22 06:10

Laurent