Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Adding the

<plugin>
    <groupId>org.apache.servicemix.tooling</groupId>
    <artifactId>depends-maven-plugin</artifactId>
</plugin>

plugin produces a classes/META-INF/maven/dependencies.properties file with the project dependencies easily parseable.

Example of the output produced:

# Project dependencies generated by the Apache ServiceMix Maven Plugin
# Generated at: Mon Oct 10 17:43:00 CEST 2011

groupId = my.group.name
artifactId = my.artifact.name
version = 0.0.1-SNAPSHOT
my.group.name/my.artifact.name/version = 0.0.1-SNAPSHOT

# dependencies

junit/junit/version = 4.8
junit/junit/type = jar
junit/junit/scope = test

org.easymock/easymock/version = 2.4
org.easymock/easymock/type = jar
org.easymock/easymock/scope = test

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

On GNU/Linux I would just do mvn dependency:tree > myFile. However, if you're restricted to Windows only, than I would look for Windows' syntax for streaming the output of a command.

According to this site (just a top-results from Google) it seems that Windows' console also use > sign to direct the output stream to i.e. a file. So would you mind trying this?

I have run the below command and got the file having all the maven dependency.

mvn dependency:tree -DoutputFile=temp/mvn_dependency_tree.txt

This command will create a folder named "temp" and inside this folder a file name mvn_dependency_tree.txt will be created with all the dependencies.

You can always install MinGW and MSYS and then use the Linux examples using dependency:tree in Windows

Perhaps effective-pom (in conjunction with some linux commands for saving the file) can be sufficient for your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top