看起来可以将 path/to/a/dependency.jar
作为 Maven pom.xml
中的可扩展变量获取:参见 Can I use the path to a Maven dependency as a property?例如,您可以将表达式扩展为类似 /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
的字符串。
我想要的是 而不是我本地 Maven 存储库中依赖项 JAR 的完整路径只是 JAR 的裸名,例如 junit- 3.8.1.jar
.
例如,在我的 pom.xml
中,我希望能够使用像 ${maven.dependency.junit.junit.jar.name} 这样的值
code> 扩展为 junit-3.8.1.jar
。
我可以这样做吗?如何做?
最佳答案
您可以使用 maven-antrun-plugin 来获取依赖项的文件名。 Ant 有一个 <basename>
从路径中提取文件名的任务。如 Can I use the path to a Maven dependency as a property? 中所述依赖项的完整路径名在 ant 中可用为 ${maven.dependency.groupid.artifactid.type.path}
.这使我们能够像这样使用 ant 任务提取文件名:
<basename file="${maven.dependency.groupid.artifactid.type.path}" property="dependencyFileName" />
这会将文件名存储在名为 dependencyFileName
的属性中.
为了使这个属性在 pom 中可用,exportAntProperties
需要启用 maven-antrun-plugin 的配置选项。此选项仅在插件的 1.8 版中可用。
此示例显示用于检索 junit 依赖项的 Artifact 文件名的插件配置:
<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>
关于java - 如何获取 Maven 依赖项 JAR 的名称(非完整路径)作为 pom.xml 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864287/