tags:

views:

218

answers:

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?

+2  A: 

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] ------------------------------------------------------------------------
Pascal Thivent
I cannot prove this feature works in Maven. That only works for `maven-antrun-plugin` (see http://jira.codehaus.org/browse/MANTRUN-110). Please, provide a complete pom example, as I suppose, you refer not `<project><properties>` but some other properties.
dma_k
@dma_k The Jira issue you are mentioning doesn't show anything except that there was a bug in the antrun documentation. Now, feel free to test this solution yourself. And BTW, I always test my answers :)
Pascal Thivent
@Pascal Thanks for the update! I fully trust you, that it works at your site :) My question was: is it supposed to work in combination with `maven-antrun-plugin`. And you show this in your example, great! And from example I see that this is `maven-antrun-plugin`-specific feature, i.e. if I want to substitute `${my.lib}` variable for resources (without using any additional pugin) - I cannot do it, right?
dma_k
@dma_k Not sure it wouldn't work outside antrun (I'm just echoing ${my.lib} after all) but I would have to test resources filtering (sometimes, the way property resolution works is a bit obscure for me).
Pascal Thivent
@dma_k I did a test and the property is not available during filtering. I don't know if it's the same situation as in http://stackoverflow.com/questions/2246524/how-to-filter-resource-in-maven-replacing-with-a-dependencies-artifactid/2247645#2247645 i.e. if the expression is not available during filtering because filtering and interpolation don't share the same algorithm or if it's an antrun property.
Pascal Thivent
@Pascal Exactly, that what I meant. The link I've posted at my fisst comment also contains a link to `maven-antrun-plugin` sources, that do that trick. So it is antrun-specific feature, and bmargulies is also right, saying you can do the same in your custom plugin.
dma_k
Also found out, this works with `maven-antrun-plugin v1.3` and not with `maven-antrun-plugin v1.1`, so also checkout the latest version for this feature to work.
dma_k
A: 

You need to write a new maven plugin that sets a property value to the fully-resolved pathname of a dependency. The maven-dependency-plugin won't do that for you.

It will copy your dependency somewhere and then you can refer to it by that pathname.

bmargulies