With Apache Maven, it is quite common to have to reference dependency location and not just its coordinates. The most common and known examples are:

  • For javaagents (-javaagent:/path/to/agent.jar)
  • For application specific usage (-Dapp.extensions=/path/config.jar)
  • For test environment (for instance in arquillian.xml you will point to a container zip)
  • Etc...

The solution I saw the most often was to use the local repository path. It ends up on configurations looking like:

-javaagent:${settings.localRepository}/my/groupId/artifactId/version/artifactId-version.jar

With very simple groupId and properties for the artifactId and version it is not that bad but often the groupIds are long and it ends up on a very long line not readable and with duplication which is quite error prone.

The alternative I like to use is to rely on the maven dependency plugin to give me the location:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>3.1.1</version>
  <executions>
    <execution>
      <goals>
        <goal>properties</goal>
      </goals>
    </execution>
  </executions>
</plugin>

This will be enough to let the plugin to register in the project properties all the dependencies location under the key groupId:artifactId:type[:classifier].

For instance if you want the javaagent for Aspectj, add as dependency (even in scope test):

<dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.5.4</version>
</dependency>

And then you can access the location of the weaver with the property ${aspectj:aspectjweaver:jar}. Back to our previous example we would now write:

-javaagent:${my.groupId:artifactId:jar}

Easier no? :)

 

From the same author:

In the same category: