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!
-
1On the Maven Central you can do a search via search.maven.org/#advancedsearch%7Cgav or are you talking about your local cache $HOME/.m2/repository or within a repository manager ?– khmarbaiseApr 17, 2017 at 10:38
-
@khmarbaise I'm talking about the case when you have a multi-module Maven project and you would like to search for all possible locations in all jars the project depends on for a specific class file. So it should load all jars I guess and do a search if it is possible– Dmitry SenkovichApr 17, 2017 at 12:36
-
I would use my IDE for such things?– khmarbaiseApr 17, 2017 at 17:18
-
@khmarbaise I guess my question was formulated too poor, sorry. But yes, Intellij IDEA can do this kind of things, thanks.– Dmitry SenkovichApr 18, 2017 at 8:16
1 Answer
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.
-
-
@PhilipRego -- The error message tells you all you need to know. The commands above are Maven commands and expect a Maven project; in other words, the existence of a
pom.xml
. If you do have a Maven project, make sure that you run said commands in the project directory..– FrellingFeb 26, 2019 at 8:24 -