javamavenmaven-3maven-dependency-plugin

How do I get an aggregated list of external dependencies of my maven project?


I have a large multimodule maven project that has a large number of dependencies. I would like to generate a complete, duplicate-filtered list of third-party dependencies (that is, all dependencies not using the group id of the project) this project has.

I have tried using mvn dependency:list -DexcludeGroupIds=org.example.projectx for this purpose, but it seems unable to aggregate the output into a single list. When I run the command from the project root command line, I get output as follows:

[...]
[INFO] ------------------------------------------------------------------------
[INFO] Building ProjectX: ModuleA - Datatypes 4.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ projectx-moda-datatypes ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    org.slf4j:slf4j-api:jar:1.7.10:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    junit:junit:jar:4.12:test
[INFO]    com.google.guava:guava:jar:18.0:compile
[INFO] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ProjectX: ModuleB - Binary 4.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ projectx-modb-binary ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    org.slf4j:slf4j-api:jar:1.7.10:compile
[INFO]    com.google.guava:guava:jar:18.0:compile

..etc, for every single submodule. Not only is this not a single list (but a separate list for each submodule), but as you can see it contains duplicates. Moreover, the actual output I'm interested in is buried in a torrent of other Maven output (download messages, etc).

The -DoutputFile=<file.txt> option does not offer a solution either. The result of running mvn dependency:list -DoutputFile=deps.txt from my project root is not a single file listing all dependencies, but multiple separate files, one in each submodule-directory.

I can of course redirect the maven console output to a file (mvn [options] > output.txt) and try some clever regexing in vi to filter it down to the list I want. However, I was hoping there was a way to get what I need using just Maven, either using the dependency plugin or some other reporting plugin that I'm not aware of.


Solution

  • You could create an additional module (or an external maven project) having pom packaging (that is, empty) and having as dependencies all of the modules/projects you are interested in. Then, run the site life cycle on it (that is, simply mvn site). As part of the generated site under target\site, you will have:

    Alternatively, you could also use this ad-hoc module/project to run other plugins on it, like the dependency:list you mentioned. This time you will get the full merged list you were looking for, although not alphabetically ordered and still difficult to read/re-use since it will be part of the build output. The site reports is instead much easier to read and handle (i.e. copy and paste of the dependencies table to an excel file).

    For reference, the Maven Project Info Reports Plugin will be used by the site phase. Official site, here.