如何使用maven获取依赖jar的路径

muh*_*hqu 5 java dependencies maven

给定一个简单的Maven项目,例如JUnit作为依赖项,如何将完整的文件路径放到junit.jar它将安装到的本地maven存储库中?

例如如何从神器junit:junit/Users/foobar/.m2/repository/junit/junit/4.11/junit-4.11.jar

Jim*_*ers 5

以下 SO 答案来看,看起来最简单的是使用 antrun 插件。

<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>
Run Code Online (Sandbox Code Playgroud)


Ter*_*ran 5

maven 依赖插件有一个目标“属性”。从文档

为每个项目依赖项设置指向工件文件的属性的目标。对于每个依赖项(直接和传递),将设置一个项目属性,该属性遵循 groupId:artifactId:type:[classifier] 形式并包含已解析工件的路径。

所以这样的事情应该可以解决问题:

<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>
Run Code Online (Sandbox Code Playgroud)

然后该属性${junit:junit:jar}应包含 jar 文件路径