Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the path to a Maven dependency as a property?

Tags:

maven-2

I have a maven dependency in my pom.xml as such:

<dependency>     <groupId>com.foo</groupId>     <artifactId>Bar</artifactId>     <version>1.2.3</version> </dependency> 

And I would like to use the system path to the binary as a property (so I can pass it to an external process that is kicked off by maven). I can do this in an awkward way:

<properties>     <my.lib>${settings.localRepository}/com/foo/Bar/1.2.3/Bar.jar</my.lib> </properties> 

But I would really like to use a more standard mechanism, such as:

<properties>     <my.lib>${com.foo:Bar:1.2.3}</my.lib> </properties> 

I something like that possible?

like image 529
codefinger Avatar asked Mar 01 '10 23:03

codefinger


People also ask

What is Maven property?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java. Now this property( java.

Where can I find Maven dependencies?

In your project's POM, press Ctrl and hover the mouse over the dependency. Click the dependency to open the dependency's POM. In the dependency POM, view the active dependency, its transitive dependencies and their versions. You can check the origin from which the dependency was pulled in.

Where do I put dependency in POM file?

Use the Dependencies tab of the POM editor to add the dependency. Click the Dependencies tab. Click Add to access the select dependency dialog. Enter the values for the module that you want to add as a dependency.


2 Answers

Here is a correct implementation, using the maven-dependency-plugin properties goal, which can be used anywhere in a pom:

<?xml version="1.0" encoding="UTF-8"?> <project>     <modelVersion>4.0.0</modelVersion>     <groupId>com.stackoverflow</groupId>     <artifactId>q2359872</artifactId>     <version>2.0-SNAPSHOT</version>     <name>q2359872</name>      <properties>         <!-- Must be listed in the dependencies section otherwise it will be null. -->         <my.lib>${org.jmockit:jmockit:jar}</my.lib>     </properties>     <dependencies>         <dependency>             <groupId>org.jmockit</groupId>             <artifactId>jmockit</artifactId>             <version>1.11</version>         </dependency>     </dependencies>     <build>         <defaultGoal>generate-sources</defaultGoal>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-dependency-plugin</artifactId>                 <version>2.3</version>                 <executions>                     <execution>                         <goals>                             <goal>properties</goal>                         </goals>                     </execution>                 </executions>             </plugin>             <!-- Example usage: -->             <plugin>                 <groupId>org.codehaus.mojo</groupId>                 <artifactId>exec-maven-plugin</artifactId>                 <version>1.2</version>                 <executions>                     <execution>                         <goals>                             <goal>exec</goal>                         </goals>                         <phase>generate-sources</phase>                     </execution>                 </executions>                 <configuration>                     <executable>echo</executable>                     <arguments>                         <argument>path to jar=</argument>                         <argument>${org.jmockit:jmockit:jar}</argument>                         <argument>my.lib=</argument>                         <argument>${my.lib}</argument>                     </arguments>                 </configuration>             </plugin>             <!-- end of Example usage -->         </plugins>     </build> </project> 

And the output is...

jpyeron@black /projects/wkspc/tmp/foo $ /cygdrive/c/programs.x86_64/apache-software-foundation/apache-maven-3.1.1/bin/mvn [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building q2359872 2.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.3:properties (default) @ q2359872 --- [INFO] [INFO] --- exec-maven-plugin:1.2:exec (default) @ q2359872 --- path to jar= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar my.lib= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.032s [INFO] Finished at: Wed Sep 17 12:07:18 EDT 2014 [INFO] Final Memory: 10M/153M [INFO] ------------------------------------------------------------------------ 
like image 107
Jason Pyeron Avatar answered Oct 08 '22 21:10

Jason Pyeron


Assuming that the com.foo:Bar:jar:1.2.3 artifact is declared as dependency in your POM, the following property returns the path to the jar in the local repository:

${maven.dependency.com.foo.Bar.jar.path} 

Update: Here is a simple POM demonstrating this:

<?xml version="1.0" encoding="UTF-8"?> <project>   <modelVersion>4.0.0</modelVersion>   <groupId>com.stackoverflow</groupId>   <artifactId>q2359872</artifactId>   <version>1.0-SNAPSHOT</version>   <name>q2359872</name>   <properties>     <my.lib>${maven.dependency.junit.junit.jar.path}</my.lib>   </properties>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>   </dependencies>   <build>     <plugins>       <plugin>         <artifactId>maven-antrun-plugin</artifactId>         <executions>           <execution>             <phase>process-resources</phase>             <configuration>               <tasks>                 <echo>${my.lib}</echo>               </tasks>             </configuration>             <goals>               <goal>run</goal>             </goals>           </execution>         </executions>       </plugin>     </plugins>   </build> </project> 

Running mvn process-resources produces the following output:

 $ mvn process-resources [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building q2359872 [INFO]    task-segment: [process-resources] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources {execution: default-resources}] [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/q2359872/src/main/resources [INFO] [antrun:run {execution: default}] [INFO] Executing tasks      [echo] /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7 seconds [INFO] Finished at: Tue Mar 02 14:41:32 CET 2010 [INFO] Final Memory: 7M/68M [INFO] ------------------------------------------------------------------------ 
like image 40
Pascal Thivent Avatar answered Oct 08 '22 21:10

Pascal Thivent