adelphus
adelphus

Reputation: 10328

How to output a simple list of Maven dependencies

I'm trying to get a simple, machine-parsable list of dependencies from my POM. If I do:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom

I get a load of pointless [INFO] output:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - my-group:my-project:jar:1.0
[INFO]    task-segment: [org.apache.maven.plugins:maven-dependency-plugin:2.1:list]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:list {execution: default-cli}]
[INFO] 
[INFO] The following files have been resolved:
[INFO]    com.squareup.picasso:picasso:jar:2.5.2:compile
[INFO]    commons-io:commons-io:jar:1.3.2:compile
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Jan 11 14:06:05 GMT 2016
[INFO] Final Memory: 17M/325M
[INFO] ------------------------------------------------------------

which I then have to manually scrape to get the info. If I add the -q switch

mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom

I get a download progress message or nothing if it's already local (unless there's an error of course).

Is there really no way to execute the dependency list command, so it just outputs a simple list of downloaded dependencies? Something like:

> mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom
    com.squareup.picasso:picasso:jar:2.5.2:compile
    commons-io:commons-io:jar:1.3.2:compile
> 

Upvotes: 9

Views: 10474

Answers (3)

Mike R
Mike R

Reputation: 879

A slight improvement to Faron's comment above

This will sort remove empty lines and leading whitespace indents

mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.10:list -DoutputFile=>(tail -n +3|sort|awk 'NF'|sed "s/^[ \t]*//")

Example

❯ mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.10:list -DoutputFile=>(tail -n +3|sort|awk 'NF'|sed "s/^[ \t]*//")
com.amazonaws:aws-java-sdk-accessanalyzer:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-account:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-acm:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-acmpca:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-alexaforbusiness:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-amplifybackend:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-amplify:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-amplifyuibuilder:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-api-gateway:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-apigatewaymanagementapi:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-apigatewayv2:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-appconfigdata:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-appconfig:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-appflow:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-appintegrations:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-applicationautoscaling:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-applicationcostprofiler:jar:1.12.261:compile
com.amazonaws:aws-java-sdk-applicationinsights:jar:1.12.261:compile

Without the sed and awk to remove the empty lines and leading whitespace it would look like this.

❯ mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.10:list -DoutputFile=>(tail -n +3|sort)

   com.amazonaws:aws-java-sdk-accessanalyzer:jar:1.12.261:compile
   com.amazonaws:aws-java-sdk-account:jar:1.12.261:compile
   com.amazonaws:aws-java-sdk-acm:jar:1.12.261:compile
   com.amazonaws:aws-java-sdk-acmpca:jar:1.12.261:compile
   com.amazonaws:aws-java-sdk-alexaforbusiness:jar:1.12.261:compile
   com.amazonaws:aws-java-sdk-amplifybackend:jar:1.12.261:compile

Reference for sed command: https://stackoverflow.com/a/2310620
Reference for awk command: https://stackoverflow.com/a/29549497

Upvotes: 1

adelphus
adelphus

Reputation: 10328

Building on the answer from Tunaki and combining with this answer to use stdout as a file, I get this:

> mvn -q org.apache.maven.plugins:maven-dependency-plugin:2.10:list \
  -f sample.pom -DoutputFile=>(cat)

The following files have been resolved:
   commons-io:commons-io:jar:1.3.2:compile
   com.squareup.picasso:picasso:jar:2.5.2:compile

>

Not perfect - but a significant improvement for my purposes.

Upvotes: 4

Tunaki
Tunaki

Reputation: 137064

It is possible to redirect the output of the maven-dependency-plugin to a file with the help of the outputFile attribute:

If specified, this parameter will cause the dependencies to be written to the path specified, instead of writing to the console.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:list -f sample.pom -DoutputFile="..."

The plugin will create the file if it does not exist. If it already exists, the content will be overwritten (but this can be controlled by the appendOutput attribute).

This should give you a simple and parsable list of dependencies.


As a side-note, I notice you are using a very old version of the maven-dependency-plugin (2.1 is dated January 2009). The latest is 2.10 at this time.

Upvotes: 7

Related Questions