Saturday, December 22, 2018

Several useful cases for maven plugin “dependency”

The maven dependency plugin is very handy when trying to deal with following questions:

  • What are the external artifacts involved in a maven project. (dependency:resolve)
  • Find out how an artifact is introduced into a project. (dependency:tree)
  • In a multi modules project, how these modules denpend on each other? (dependency:tree –Dincludes=com.mypackage.*)
  • Find out unnecessary dependencies in a maven project. (dependency:analyze)


To solve these puzzles, we need to know some typical usage of maven dependency plugin.

1. goal ‘resolve’

1
2
#list all dependencis
mvn dependency:resolve

Output looks like below.

image

2. goal ‘tree’

1
2
3
4
5
6
# list dependencies tree.
# can use –Dincludes=[groupId]:[artifactId]:[type]:[version], can use wildcard *
 
# e.g  -Dincludes=org.springframework*,org.apache*    # list only package from spring and apache.
 
mvn dependency:tree  -Dincludes=org.springframework*

Output looks like below.

image


3. goal ‘analyze’

1
2
3
4
5
6
# List possible inmproper dependency.
# 1.avoid ‘Used undeclared dependencies`
# 2.'Unused declared dependencies` are not 100% reliable, cause of <provide> or <optional>
#    need to check manually
 
mvn dependency:analyze

Output looks like below.

image

In above screenshot there are several suspicious dependencis may not be used at all. Then use “dependency:tree –Dincludes= org.springframework.boot:spring-boot-starter-web” to see how this dependency is introduced to the project.

0 comments:

Post a Comment

Powered by Blogger.

About The Author

My Photo
Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.