目录
插件简介
Apache Maven Dependency Plugin是Apache Maven构建工具的一个插件,用于管理项目的依赖项。
该插件提供了一系列目标(goals),可用于解决和处理项目的依赖关系。以下是一些常用的目标及其功能:
- dependency:copy:将项目依赖的文件复制到指定的目录。
- dependency:tree:以树形结构打印项目依赖关系,显示依赖树。
- dependency:resolve:解析并下载项目的依赖项。
- dependency:list:列出项目的所有依赖项。
- dependency:analyze:分析项目依赖项,检查冲突和潜在的问题。
通过使用这些目标,开发人员可以更好地管理项目的依赖项,确保正确的依赖关系,并根据需要进行复制、解析和分析。
使用Apache Maven Dependency Plugin可以减少手动处理依赖项的工作量,并提供了一种标准化和自动化的方式来管理项目的依赖关系。它是Maven生态系统中非常重要和常用的插件之一。
使用示例
以使用 goals analyze 为例,用它来分析项目中的依赖是否使用了,可以通过它的结果来排除一些未使用的依赖,达到减少冲突和精简项目的效果:
配置依赖:
xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>analyze</id>
<goals>
<goal>analyze</goal>
</goals>
<configuration>
<failOnWarning>true</failOnWarning>
</configuration>
</execution>
</executions>
</plugin>
执行 mvn dependency:analyze
输出结果:
Used undeclared dependencies found:
Unused declared dependencies found:
bash
[INFO] <<< maven-dependency-plugin:3.1.1:analyze (default-cli) < test-compile @ test-app <<<
[INFO]
[INFO] --- maven-dependency-plugin:3.1.1:analyze (default-cli) @ test-app ---
[WARNING] Used undeclared dependencies found:
[WARNING] org.apache.httpcomponents:httpcore:jar:4.4.10:compile
[WARNING] org.springframework:spring-web:jar:5.0.10.RELEASE:compile
[WARNING] org.springframework:spring-test:jar:5.0.10.RELEASE:test
[WARNING] org.apache.poi:poi-ooxml:jar:3.14-beta1:compile
[WARNING] org.springframework:spring-jcl:jar:5.0.10.RELEASE:compile
[WARNING] org.springframework:spring-core:jar:5.0.10.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot-autoconfigure:jar:2.0.6.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot-test:jar:2.0.6.RELEASE:test
[WARNING] commons-lang:commons-lang:jar:2.3:compile
[WARNING] javax.annotation:javax.annotation-api:jar:1.3.2:compile
[WARNING] org.springframework:spring-aop:jar:5.0.10.RELEASE:compile
[WARNING] org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[WARNING] log4j:log4j:jar:1.2.14:compile
[WARNING] com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[WARNING] com.google.code.gson:gson:jar:2.8.5:compile
[WARNING] org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[WARNING] commons-codec:commons-codec:jar:1.11:compile
[WARNING] org.apache.commons:commons-pool2:jar:2.5.0:compile
[WARNING] org.apache.poi:poi:jar:3.14-beta1:compile
[WARNING] org.aspectj:aspectjweaver:jar:1.8.13:compile
[WARNING] org.springframework:spring-beans:jar:5.0.10.RELEASE:compile
[WARNING] org.apache.commons:commons-collections4:jar:4.1:compile
[WARNING] com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile
[WARNING] org.springframework:spring-tx:jar:5.0.10.RELEASE:compile
[WARNING] com.dianping.cat:cat-client:jar:3.0.0:compile
[WARNING] org.mybatis:mybatis:jar:3.5.1:compile
[WARNING] commons-collections:commons-collections:jar:3.2.2:compile
[WARNING] junit:junit:jar:4.12:compile
[WARNING] org.slf4j:slf4j-api:jar:1.7.25:compile
[WARNING] org.springframework:spring-jdbc:jar:5.0.10.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot:jar:2.0.6.RELEASE:compile
[WARNING] org.springframework:spring-context:jar:5.0.10.RELEASE:compile
[WARNING] org.springframework.data:spring-data-redis:jar:2.0.11.RELEASE:compile
[WARNING] org.springframework:spring-context-support:jar:5.0.10.RELEASE:compile
[WARNING] Unused declared dependencies found:
[WARNING] org.springframework.boot:spring-boot-starter-web:jar:2.0.6.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.6.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot-starter-data-redis:jar:2.0.6.RELEASE:compile
[WARNING] org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:2.0.1:compile
[WARNING] org.springframework.boot:spring-boot-starter-aop:jar:2.0.6.RELEASE:compile
[WARNING] org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.6.RELEASE:provided
[WARNING] org.springframework.boot:spring-boot-starter-test:jar:2.0.6.RELEASE:test
[WARNING] mysql:mysql-connector-java:jar:5.1.18:compile
[WARNING] com.jayway.jsonpath:json-path:jar:0.9.0:test
[WARNING] org.apache.commons:commons-dbcp2:jar:2.6.0:compile
[WARNING] org.apache.directory.studio:org.apache.commons.codec:jar:1.8:compile
[WARNING] org.elasticsearch:elasticsearch:jar:1.5.2:compile
[WARNING] com.101tec:zkclient:jar:0.4:compile
[WARNING] org.quartz-scheduler:quartz-jobs:jar:2.2.1:compile
[WARNING] com.squareup.okhttp3:okhttp:jar:3.7.0:compile
[WARNING] com.jcraft:jsch:jar:0.1.49:compile
[WARNING] org.apache.poi:poi-examples:jar:3.14-beta1:compile
[WARNING] biz.paluch.logging:logstash-gelf:jar:1.12.0:compile
[WARNING] com.google.code.findbugs:jsr305:jar:3.0.2:compile
结尾
本文只是以使用 analyze 使用为例做了介绍,有时也会使用到 copy 的场景,有些内容在 build 时需要将文件 copy 到指定目录,那么用它就非常方便了。
其它的使用,大家一起来探索吧,也欢迎大家一起分享你们的使用场景和使用方式。
更多的使用方式可以在官网探索:maven 插件 之 maven-dependency-plugin