AngryPanda
AngryPanda

Reputation: 1281

List maven dependencies in Java / Scala

I want to expose the list of maven dependency artifacts (with version) over an HTTP endpoint. This blogpost solves my problem but unfortunately it's in bash and not Java/Scala.

mvn -o dependency:list \
| grep ":.*:.*:.*" \
| cut -d] -f2- \
| sed 's/:[a-z]*$//g' \
| sort -u 

Output

com.amazonaws:amazon-sqs-java-extended-client-lib:jar:1.0.0
com.amazonaws:aws-java-sdk-acm:jar:1.10.64
com.amazonaws:aws-java-sdk-api-gateway:jar:1.10.64
com.amazonaws:aws-java-sdk-autoscaling:jar:1.10.64
com.amazonaws:aws-java-sdk-cloudformation:jar:1.10.64
com.amazonaws:aws-java-sdk-cloudfront:jar:1.10.64
com.amazonaws:aws-java-sdk-cloudhsm:jar:1.10.64
com.amazonaws:aws-java-sdk-cloudsearch:jar:1.10.64

Can someone share some pointers on how to achieve this in code?

Upvotes: 1

Views: 136

Answers (1)

yw3410
yw3410

Reputation: 228

The easiest way to do this would be to create a maven task to print the dependencies to a resource directory at the time of compilation.

An alternative is that you can inspect the classpath of the ClassLoader, get the jars and work it out using the artifacts but that's much, much more fragile.

Upvotes: 1

Related Questions