Maven 中的依赖 jar 文件列表

使用 Maven2,有没有一种方法可以只列出文件名而不列出 jar 依赖项?

mvn dependency:build-classpath

可以列出 jar 文件,但是这将包含到它们在我的本地存储库中的位置的完整路径。我需要的基本上只是一个文件名的列表(或者复制依赖目标复制的文件名)。

所以我需要的清单大概是

activation-1.1.jar,antlr-2.7.6.jar,aopalliance-1.0.jar etc...

理想情况下,可以将其作为 maven 属性,但是我想,可以生成 build-classspath 之类的文件就可以了。

我试图实现的是为 OSGi 包将 Bundle-ClassPath写入一个手动维护的 MANIFEST.MF 文件。(要回答这个问题,你不需要理解这一点。)

澄清一下: 问题是 没有关于如何将清单头写入 jar 中的 MANIFEST.MF 文件(这很容易 google)。我正在询问如何获得我想要写的数据,即上面显示的列表。

123935 次浏览

Maven can build the classpath in your manifest automatically: http://maven.apache.org/guides/mini/guide-manifest.html

It's a configuration of the Maven archive plugin.

Have you looked at the Apache Felix project? It has a whole mess of plugins, including a bundle plugin that should do what you want.

Also, have you tried the <addClasspath> tag with <manifestFile>? That should have the desired effect of merging the classpath into your manifest.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<addClasspath>true</addClasspath>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
...
</plugin>

As best as I can tell, you can't get exactly that output, with the commas and no spaces. Both via the command line and via the pom.xml file, the maven-dependency-plugin or the CLI freaks out if you specify spaces or the '' (empty string) as a substitute with either pathSeparator or fileSeparator. So, you may be forced to reach something of a compromise. You can

mvn dependency:build-classpath -Dmdep.pathSeparator=":" -Dmdep.prefix='' -Dmdep.fileSeparator=":" -Dmdep.outputFile=classpath

However, that should get you a full list, separated by '::' instead of just ',', but it works. If you run:

mvn dependency:build-classpath -Dmdep.pathSeparator="@REPLACEWITHCOMMA" -Dmdep.prefix='' -Dmdep.fileSeparator="@" -Dmdep.outputFile=classpath

and attach this to the generate-resources phase and filter that resource later by setting the correct property in the process-resources phase of the lifecycle, you should be able to get just the comma.

You can see the full list of options at: http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html

I may be missing something here, but as you've already used copy-dependencies it sounds like what you're really after is just a list of files in a specified directory.

Ant can do this for you without any problems, as can a shell script.

This command will generate the dependencies tree of your maven project:

$ mvn dependency:tree

I am sure that you will like the result :-)

To add a notch to the existing answers, the current maven-dependency-plugin allows saving the classpath to a property with the outputProperty parameter.

Here's the command you're asking for

$ mvn dependency:tree

For large projects it can output a lot of text. I assume that you want to check that dependency tree contains a certain dependency, so you don't need a full list.

Here's how you can filter output on Windows:

$ mvn dependency:tree | findstr javax.persistence

And here's how you can do it on Linux:

$ mvn dependency:tree | grep javax.persistence

Maven way to filter the dependency tree (works in Windows cmd, MacOS and Linux shell):

$ mvn dependency:tree -Dincludes=javax.persistence:*

Maven way (Windows PowerShell):

$ mvn dependency:tree '-Dincludes=javax.persistence:*'

Actually, for just the final list of jars, simply use

mvn dependency:list

Which is lot more simple than dependency:tree which is an overkill to simply get the final list as it shows detailed transitive tree and conflict resolution (with verbose).

Here's the doc for additional parameters

Here is an awk script to pipe mvn dependency:list:

mvn dependency:list | awk -f mvnfmt.awk

You can | sort if you want to sort by name, or | tr '\n' ':' to format it to a classpath.

mvnfmt.awk is:

BEGIN {
found = 0
}


/The following files have been resolved/ {
found = 1
next
}


/^\[INFO\] \$/ {
print "Empty " found
if (found != 0) found = 0
}


{
if (!found) next
n = split($0, a, " ")
if (n != 2) {
found = 0
next
}
split(a[2], a, ":")
print a[2] "-" a[4] "." a[3]
}