Java Maven中的依赖jar文件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/278596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:26:17  来源:igfitidea点击:

List of dependency jar files in Maven

javamaven-2dependenciesclasspath

提问by Sindri Traustason

Using Maven 2, is there a way I can list out the jar dependencies as just the file names?

使用 Maven 2,有没有一种方法可以将 jar 依赖项作为文件名列出?

mvn dependency:build-classpath 

can list the jar files, but that will include the full path to their location in my local repository. What I need is essentially just a list of the file names (or the file names that the copy-dependencies goal copied).

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

So the list I need would be something like

所以我需要的清单类似于

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

ideally as a maven property, but I guess, a file such as build-classpath can generate will do.

理想情况下作为 maven 属性,但我想,可以生成诸如 build-classpath 之类的文件就可以了。

What I am trying to achieve is writing a Bundle-ClassPathto an otherwise manually maintained MANIFEST.MF file for a OSGi bundle. (You shouldn't need to understand this bit to answer the question.)

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

To clarify: The question is notabout how to write manifest headers into the MANIFEST.MF file in a jar (that is easily googleble). I am asking about how to get the data I want to write, namely the list shown above.

澄清一下:问题在于如何将清单标头写入 jar 中的 MANIFEST.MF 文件(这很容易用 googleble 搜索)。我在问如何获取我要写入的数据,即上面显示的列表。

回答by Davide Gualano

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

Maven 可以自动在您的清单中构建类路径:http: //maven.apache.org/guides/mini/guide-manifest.html

It's a configuration of the Maven archive plugin.

它是 Maven 归档插件的配置。

回答by sblundy

Have you looked at the Apache Felixproject? It has a whole mess of plugins, including a bundle pluginthat should do what you want.

你看过Apache Felix项目吗?它有一大堆插件,包括一个可以做你想做的插件

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

另外,你有没有试过<addClasspath>标签<manifestFile>?这应该具有将类路径合并到清单中的预期效果。

<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>

回答by Gabe Mc

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 pathSeparatoror fileSeparator. So, you may be forced to reach something of a compromise. You can

尽我所知,您无法获得准确的输出,其中包含逗号且没有空格。无论是通过命令行还是通过 pom.xml 文件,如果您指定空格或 ''(空字符串)作为pathSeparator或的替代品,maven-dependency-plugin 或 CLI 都会出问题fileSeparator。因此,您可能会被迫达成某种妥协。你可以

    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

andattach this to the generate-resourcesphase andfilter that resource later by setting the correct property in the process-resourcesphase of the lifecycle, you shouldbe able to get just the comma.

并将其附加到generate-resources阶段稍后通过process-resources在生命周期的阶段设置正确的属性来过滤该资源,您应该能够获得逗号。

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

您可以在以下位置查看完整的选项列表: http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html

回答by Kevin Wright

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.

Ant 可以毫无问题地为您执行此操作,shell 脚本也可以。

回答by Anis

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

此命令将生成 Maven 项目的依赖关系树:

$ mvn dependency:tree

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

我相信你会喜欢这个结果:-)

回答by Andrew

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

要为现有答案添加一个缺口,当前的 maven-dependency-plugin 允许将类路径保存到带有outputProperty参数的属性。

回答by naXa

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:

以下是在 Windows 上过滤输出的方法:

$ mvn dependency:tree | findstr javax.persistence

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

以下是在 Linux 上执行此操作的方法:

$ mvn dependency:tree | grep javax.persistence

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

Maven过滤依赖树的方法(适用于 Windows cmd、MacOS 和 Linux shell):

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

Maven way (Windows PowerShell):

Maven 方式(Windows PowerShell):

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

回答by kisna

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).

这比dependency:tree简单得多,因为它显示了详细的传递树和冲突解决方案(详细说明),所以简单地获取最终列表是一种矫枉过正。

回答by Matthieu

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

这是一个用于管道的 awk 脚本mvn dependency:list

mvn dependency:list | awk -f mvnfmt.awk

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

你可以| sort,如果你想以按名称排序,或| tr '\n' ':'将其格式化到类路径。

mvnfmt.awkis:

mvnfmt.awk是:

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(##代码##, a, " ")
    if (n != 2) {
        found = 0
        next
    }
    split(a[2], a, ":")
    print a[2] "-" a[4] "." a[3]
}