3

It looks like it is possible to get the path/to/a/dependency.jar as an expandable variable within a Maven pom.xml: see Can I use the path to a Maven dependency as a property? You can expand, e.g., an expression into a string like /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar.

What I want instead of the full path to the dependency JAR within my local Maven repository is just the bare name of the JAR, for example junit-3.8.1.jar.

So for example, within my pom.xml, I would like to be able to use a value like ${maven.dependency.junit.junit.jar.name} to expand to junit-3.8.1.jar.

Can I do this, and how?

Community
  • 1
  • 1
0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
  • 1
    Why? And what do you want to do with it? What kind of application did you had in mind? – Robert Scholte Aug 06 '15 at 20:36
  • @RobertScholte, the **background** is I'm working on an application that depends on BouncyCastle, which is a JCE provider. I'm bundling the app as an executable jar using the Maven assembly plugin, but I can't bundle BouncyCastle in the same way because JCE providers have to be digitally signed and bundling it kills the digital signature. I have a couple of use cases in mind for having the JAR name, but one of them is to put the BouncyCastle provider JAR in my executable JAR's manifest classpath so Java knows to load it. – 0xbe5077ed Aug 06 '15 at 20:48
  • Consider to use maven-shade-plugin instead of maven-assembly-plugin, which is the preferred solution for your case. Read http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html which shows how to filter specific files (second example). – Robert Scholte Aug 06 '15 at 20:56
  • It's still unclear on why you would need a property to hold the dependency name. If you know the dependency name, why not just use it? Why would you need a property for that? – mystarrocks Aug 06 '15 at 20:57
  • @RobertScholte, I can see how this will let me build an Uber-jar (which is 90% of what I'm trying to do) but not how I can put the excluded dependency JARs (BouncyCastle in my example) in my executable Uber-jar's manifest classpath... – 0xbe5077ed Aug 06 '15 at 20:59
  • @mystarrocks, because I would rather avoid hard-coding `my-dependency-1.2.3.jar` when I could just put in a symbolic reference and hopefully make my `pom.xml` more flexible in the face of dependency version change. – 0xbe5077ed Aug 06 '15 at 21:00
  • How about adding it to your pom and giving it the runtime scope? – Robert Scholte Aug 06 '15 at 21:07

2 Answers2

3

You can use the maven-antrun-plugin to get the file name of a dependency. Ant has a <basename> task which extracts the file name from a path. As described in Can I use the path to a Maven dependency as a property? the full path name of a dependency is available in ant as ${maven.dependency.groupid.artifactid.type.path}. This enables us to extract the file name with the ant task like this:

<basename file="${maven.dependency.groupid.artifactid.type.path}" property="dependencyFileName" />

This stores the file name in a property named dependencyFileName.

In order to make this property availbable in the pom, the exportAntProperties configuration option of the maven-antrun-plugin needs to be enabled. This option is only available as of version 1.8 of the plugin.

This example shows the plugin configuration for retrieving the artifact file name of the junit dependency:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <tasks>
                    <basename file="${maven.dependency.junit.junit.jar.path}"
                                      property="junitArtifactFile"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Christoph Böhme
  • 3,766
  • 1
  • 19
  • 29
1

No, I'm sorry to say that it isn't possible. So, you have two options before you. 1) modify the maven source code and contribute the modification. 2) write your own plug-in. I recommend the second option. Writing plug-ins is not that hard. As a philosophical principal, select a frequently-used plug-in which has functionality close to what you want to accomplish. Read and understand the code, and then modify it to do what you desire.

So for your example, you might look at the filter plugin. There's also some interesting syntax going on in the Ant plugin. It allows you to name dependencies and get those jar filenames into the embedded Ant script.

Good luck. :-)

As a more practical alternative, you might just break down and manually code the property value with the exact version number you're using. You're not going to switch the version number that often, right? And this is only one jar you're dealing with, right?

johnstosh
  • 335
  • 1
  • 5