57

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.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
why_vincent
  • 2,172
  • 9
  • 33
  • 49

8 Answers8

92

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.

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
AC Capehart
  • 1,260
  • 10
  • 12
18

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

Anand Ganesh
  • 331
  • 2
  • 6
9

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
Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
5

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?

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
2

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.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html#outputFile

If the project has multi modules and you want to output the dependency to a single file, use absolute path and append options.

mvn dependency:tree -DoutputFile=/abs_path/deps.txt -DappendOutput=true

noanswer
  • 171
  • 1
  • 7
0

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

Wilhelm Kleu
  • 10,821
  • 4
  • 36
  • 48
0

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

smp7d
  • 4,947
  • 2
  • 26
  • 48