Java 如何列出 JAR 的依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4417820/
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
How to list dependencies of a JAR
提问by lisak
is there a tool that can list third party "packages" that contain (third party) classes referenced in the JAR ? Let say that it would recognize what is "home" package from JAR file definition and it would print out a list of fully qualified names of third party classes up to 3rd level that were referenced within the JAR.
是否有工具可以列出包含 JAR 中引用的(第三方)类的第三方“包”?假设它会从 JAR 文件定义中识别出什么是“home”包,并且它会打印出在 JAR 中引用的第三方类的完全限定名称列表,最高可达 3 级。
org.apache.commons
org.apache.maven
javax.servlet.jsp
org.eclipse.persistence
org.apache.Hymanrabbit
the purpose is that I need to find maven dependencies for that JAR file and deploy it as a maven artifact.
目的是我需要找到该 JAR 文件的 maven 依赖项并将其部署为 maven 工件。
采纳答案by MJB
罐分析器:
a dependency management utility for jar files. It's primary purpose is to traverse through a directory, parse each of the jar files in that directory, and identify the dependencies between the jar files. The output is an xml file representing the PhysicalDependenciesbetween the jar files.
For more information on PhysicalDependencies, including a variety of design patterns, check out Extensible Java...
jar 文件的依赖管理实用程序。它的主要目的是遍历一个目录,解析该目录中的每个 jar 文件,并确定 jar 文件之间的依赖关系。输出是一个 xml 文件,表示jar 文件之间的PhysicalDependencies。
有关PhysicalDependencies 的更多信息,包括各种设计模式,请查看Extensible Java...
回答by lisak
This might be an alternative for this tool. But it doesn't list referenced classes. The last number determines "package level".
这可能是此工具的替代方案。但它没有列出引用的类。最后一个数字决定了“包裹级别”。
jar tvf yourjar.jar | perl -nle'BEGIN{$depth=(shift)-1}print join(".", (split(/\//, (split)[-1]))[0..$depth])' 3
Problem is, that this way it is impossible to get referenced classes out of class definitions. So the only way possible seems to be do it in JAVA.
问题是,这样就不可能从类定义中获取引用的类。所以唯一可能的方法似乎是在 JAVA 中进行。
So the question is: Is there any Java tool that can do it ?
所以问题是:是否有任何 Java 工具可以做到这一点?
EDIT:
编辑:
I found what I needed : M2eclipse has a feature called Class search
我找到了我需要的东西:M2eclipse 有一个叫做 类搜索的功能
In the maven repositories view, right click a repository and enable full index
在 maven 存储库视图中,右键单击存储库并启用完整索引
Then Navigate > Open type from maven - there you can look through all artifacts available based on java package convention
然后 Navigate > Open type from maven - 在那里你可以查看所有基于 java 包约定的可用工件
回答by srinannapa
If yours is a maven based project
如果您的项目是基于 Maven 的项目
mvn dependency:build-classpath -Dmdep.pathSeparator=":" -Dmdep.prefix='' -Dmdep.fileSeparator=":" -Dmdep.outputFile=classpath
Look at the below link http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html
看看下面的链接 http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html
May be in your case
可能是你的情况
mvn dependency:build-classpath -Dmdep.pathSeparator=":" -Dmdep.prefix='' -Dmdep.fileSeparator=":" -Dmdep.outputFile=classpath -f pathtopom.xml of third party
回答by drekka
My first thought was that you could do this by using a class loader to iterate over all the class files in the jar and use reflection to analyse each one for their dependences. However the Class class does not have a method which tells you this information. So the next thought would be to use some sort of bytecode analyser (asmfor example) to pull out all the referenced classes from a compile class.
我的第一个想法是,您可以通过使用类加载器遍历 jar 中的所有类文件并使用反射来分析每个类文件的依赖关系来实现此目的。但是 Class 类没有告诉您这些信息的方法。所以下一个想法是使用某种字节码分析器(例如asm)从编译类中提取所有引用的类。
Presuming that you could get this information the next issue would be to back trace the classes to jars. In a sense this would be the easy part because all you would need to do is create a classloader for each jar in your maven repo, or directory or wherever the jars are, and then ask each one in turn if it contained the specific class.
假设您可以获得此信息,下一个问题是将类回溯到 jars。从某种意义上说,这将是一个简单的部分,因为您需要做的就是为您的 Maven 存储库、目录或 jar 所在的任何位置的每个 jar 创建一个类加载器,然后依次询问每个人它是否包含特定的类。
The flaw in that thinking is that a java class (raw source or compiled) does not detail where to get the imported class from. So if you have two classes with the same package and name (happens more often than you might think), then you would be unable to tell which to use.
这种想法的缺陷在于 Java 类(原始源代码或编译的)没有详细说明从何处获取导入的类。因此,如果您有两个具有相同包和名称的类(发生的频率比您想象的要多),那么您将无法分辨该使用哪个。
Even java just assumes that the first one it finds in the class path is the correct one and throws an exception if it turns out to be incorrect (MethodNotFoundException). So unless you are going to further analyse the bytecode to figure out what methods on each class are called and then compare those with the classes in your class path, you still won't be able to be correct.
甚至 java 也只是假设它在类路径中找到的第一个是正确的,如果结果不正确则抛出异常(MethodNotFoundException)。因此,除非您打算进一步分析字节码以找出每个类上调用了哪些方法,然后将它们与类路径中的类进行比较,否则您仍然无法正确。
IN short, it's probably possible to do what you want, but likely to be very difficult and time consuming.
简而言之,您可能可以做您想做的事,但可能非常困难且耗时。
The way I normally deal with this is to simply fire up the class in test code and keep adding dependencies until I can get it to execute every method I'm interested in.
我通常处理这个问题的方法是简单地在测试代码中启动类并继续添加依赖项,直到我可以让它执行我感兴趣的每个方法。
回答by obe6
If you use maven (as I understood), you can simply use Maven Dependency plugin.
如果您使用 maven(据我所知),您可以简单地使用 Maven Dependency 插件。
First you need to write a basic pom.xml with all your jars, you can copy this example (i use hibernate as example), and substitute your jars :
首先,您需要使用所有 jar 编写一个基本的 pom.xml,您可以复制此示例(我使用 hibernate 作为示例),并替换您的 jar:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.0-Final</version>
</dependency>
<!-- add here other dependencies -->
</dependencies>
</project>
then open terminal, go into the containing folder of the pom.xml above and run this command :
然后打开终端,进入上面 pom.xml 的包含文件夹并运行以下命令:
mvn dependency:tree
mvn 依赖:树
this will print a list of dependencies...
这将打印依赖项列表...
or if you want download and copy in a folder all dependencies run the following command :
或者如果你想下载并复制一个文件夹中的所有依赖项,请运行以下命令:
mvn dependency:copy-dependencies
mvn 依赖:复制依赖
you'll find all your dependencies in folder ./target/dependencies
您将在文件夹 ./target/dependencies 中找到所有依赖项
回答by user2492456
Tattleltale is a tool from JBoss which does this
Tattleltale 是 JBoss 的一个工具,它可以做到这一点
tattletale.jboss.org/
tattletale.jboss.org/
回答by karthik manchala
You can do the following (if its a maven project):
您可以执行以下操作(如果它是 Maven 项目):
mvn dependency:tree
It shows transitive dependencies as well, making it very useful to debug dependency conflicts.
它也显示了可传递的依赖关系,这对于调试依赖关系冲突非常有用。
回答by Barry Kelly
There is a new tool since JDK 8: jdeps
自 JDK 8 以来有一个新工具: jdeps
https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool
https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool
jdeps is a new command-line tool added since JDK 8 for developers to use to understand the static dependencies of their applications and libraries. jdeps is a static analysis tool on the given class files
jdeps 是自 JDK 8 以来添加的新命令行工具,供开发人员用来了解其应用程序和库的静态依赖项。jdeps 是对给定类文件的静态分析工具