Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Maven dependencies printed to a file in a readable format?

I am working on a big projects with many pom.xml files and I need to specify all the libraries that I use. This means that I need to read pom.xml files recursively and get groupId, artifactId, scope and version. I checked out mvn dependency:tree but I can't find a way to print it to a file in a readable format. I saw appendOutput but I saw no example on how to use it in cmd. I saw some solutions done in Linux but I only have access to Windows XP.

like image 801
why_vincent Avatar asked Oct 31 '11 12:10

why_vincent


People also ask

How do you make a dependency tree?

How to get the Maven Dependency Tree of a Project. We can run mvn dependency:tree command in the terminal to print the project dependency tree. For our example, I will be using the Mockito Tutorial project. You can download the project from the GitHub repository.

How do you analyze a dependency tree in Maven?

Find it using maven infrastructure. Go to https://search.maven.org/ and enter your groupId and artifactId. Or you can go to https://repo1.maven.org/maven2/ and navigate first using plugins groupId, later using artifactId and finally using its version.

How do I download Maven dependencies to local?

You can use the Maven Dependency Plugin to download dependencies. Run mvn dependency:copy-dependencies , to download all your dependencies and save them in the target/dependency folder. You can change the target location by setting the property outputDirectory .


2 Answers

This can (at least now) be done with command line options to the dependency:tree plugin.

Try:

mvn dependency:tree -Doutput=/path/to/file 

Reference: Maven Dependency Plugin Page

You only asked about "readable" format, but you can also pass the -DoutputType parameter with various options. Also note that the version I have installed, I get the following warning:

[WARNING] The parameter output is deprecated. Use outputFile instead.  

So, consider trying it with -DoutputFile=/path/to/file

Also, I was unable to get the -DoutputType paramater to give me anything other than the default text, but didn't have a chance to play around with it. YMMV.

like image 159
AC Capehart Avatar answered Sep 23 '22 13:09

AC Capehart


If you have multiple modules under the same repo/project and want the dependencies of all the modules in one file, so as to be able to diff b/w one build and another to see if something changed somewhere, you can do

$project_dir> mvn dependency:tree -DoutputFile=<absolute_path_to_file> -DappendOutput=true 

e.g.

$project_dir> mvn dependency:tree -DoutputFile=`pwd`/mvn_dependency_tree.txt -DappendOutput=true 

See other options available at https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

like image 36
Anand Ganesh Avatar answered Sep 19 '22 13:09

Anand Ganesh