Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find jars containing a class file in Maven project

As the header says I wonder if there is such an opportunity in Maven to know the jar a class file gets loaded in a module. Just like dependency:tree, but I would like to see jars with a specific class file. Thanks!

like image 807
Dmitry Senkovich Avatar asked Apr 17 '17 09:04

Dmitry Senkovich


People also ask

How do you check if a JAR file has a class in it?

To find the . jar files that contain a class, you can use the FindClass.sh script. First go to a UNIX installation of Sterling Platform/MCF. If the FindClass.sh script already exists it should be in your $YFS_HOME directory or your $YFS_HOME/lib directory.

Does JAR file contain class files?

The JAR file contains the TicTacToe class file and the audio and images directory, as expected. The output also shows that the JAR file contains a default manifest file, META-INF/MANIFEST. MF, which was automatically placed in the archive by the JAR tool.

How do I know if a jar is in classpath?

A pragmatic way: Class. forName("com. myclass") where com. myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException , then the jar is not on you current classpath.


1 Answers

As far as I know, there is no specific Maven plugin (3.0+) that will search dependencies for class declarations. However, I believe I understand your need and offer the following solutions:

Finding duplicate declarations

mvn dependency:analyze-duplicate -DcheckDuplicateClasses

Find containing JAR within Eclipse

Use CTRL+SHIFT+T to bring up the Open Type dialog. Entering part or the whole class name presents a list of containing JARs on the build classpath.

Find containing JAR without IDE

If more programatic control is required for checking on systems without an IDE, say a CI server, the following snippets can be used to list JAR files containing a specific class or even a specific name pattern. This approach uses Maven's dependency plugin to collect all dependencies in a temporary directory such that they may be easily searched.

For Unix or Git Bash systems

mvn clean dependency:copy-dependencies -DoutputDirectory=target/temp
for j in target/temp/*.jar; do jar -tf $j | grep SomeClass && echo $j; done

For Windows via cmd shell

mvn clean dependency:copy-dependencies -DoutputDirectory=target/temp
for /R %G in (target\temp\*.jar) do @jar -tf "%G" | find "SomeClass" && echo %G

In either case, a matching entry's full package and class name will be displayed followed by the containing JAR file name. grep and find search parameters can be further refined to restrict matches as needed, such as SomeClass.class.

Hope this helps.

like image 127
Frelling Avatar answered Oct 19 '22 20:10

Frelling