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.
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.
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With