本文介绍了用Maven生成类路径文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从pom.xml依赖项生成一个类路径文件.我需要它,因此在测试过程中,我具有所有依赖项的类路径(以后将它们打包成一个包)
I'd like to generate a classpath file from pom.xml dependencies. I need it so during tests I have the classpath of all dependencies (that are later packaged into a bundle)
maven-dependency-plugin
不适合我,原因有两个:
maven-dependency-plugin
does not suit me for two reasons:
- 它会生成存储库中文件的路径,因此要使用其他模块,它们首先需要为其运行
install
阶段(我想使用类似/some/root/othermodule/target/classes
的路径) - 它不包含工件自身的路径(
target/classes
),这意味着我需要稍后在代码中添加它,这很尴尬
- it generates paths to files in the repository, so to use other modules they first need to run
install
phase for them (I'd like to have paths like/some/root/othermodule/target/classes
) - it doesn't include the artifact's own path (
target/classes
), which means I need to add it later in code, which is awkward
所以我正在寻找另一个插件(或如何正确运行maven-dependency-plugin
)
So I'm looking for another plugin (or how to properly run maven-dependency-plugin
)
推荐答案
我最终使用了GMaven:
I ended up using GMaven:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def all = project.runtimeArtifacts.collect{
def aid = "${it.groupId}:${it.artifactId}:${it.version}"
def p = project.projectReferences[aid]
p?.build?.outputDirectory ?: it.file.path
} + project.build.outputDirectory
def file = new File(project.build.directory, ".classpath")
file.write(all.join(File.pathSeparator))
</source>
</configuration>
</execution>
</executions>
</plugin>
代码有点复杂,因为我想尽可能地找到目标/类的路径.如果这不是必需的,则可以执行以下操作:
The code is a bit complex since I wanted paths to target/classes when possible. If this is not required, one can do :
file.write(project.runtimeClasspathElements.join(File.pathSeparator))
这篇关于用Maven生成类路径文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!