Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path to dependency jar with maven

Given a simple Maven project with for example JUnit as dependency, how do I get the full filepath to the junit.jar inside the local maven repository it will be installed into?!

e.g. How to get from artifact junit:junit to /Users/foobar/.m2/repository/junit/junit/4.11/junit-4.11.jar?

like image 576
muhqu Avatar asked Jan 23 '15 08:01

muhqu


3 Answers

The path is build as $repository_dir/groupId/artifactId/version/artifactId-version.jar

<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
like image 88
SubOptimal Avatar answered Sep 25 '22 03:09

SubOptimal


The maven dependency plugin has a goal 'properties'. From documentation:

Goal that sets a property pointing to the artifact file for each project dependency. For each dependency (direct and transitive) a project property will be set which follows the groupId:artifactId:type:[classifier] form and contains the path to the resolved artifact.

So something like this should do the trick:

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

Then the property ${junit:junit:jar} should contain the jar file path

like image 29
Terran Avatar answered Sep 24 '22 03:09

Terran


From the following SO answer, it looks like the easiest is to use the antrun plugin.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <configuration>
                <tasks>
                    <echo>${maven.dependency.junit.junit.jar.path}</echo>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
like image 43
Jim Sellers Avatar answered Sep 26 '22 03:09

Jim Sellers