文章目录
- 前言
- 一、准备工作
- 二、拷贝指定的jar
- 三、拷贝项目依赖的所有jar
- 四、解压依赖的jar文件
- 五、解压项目所有依赖的jar文件
- 六、下载所有依赖的jar的源代码
- 七、分析pom.xml中的依赖使用情况
- 八、列出pom.xml中的依赖树关系
前言
maven-dependency-plugin插件时一个依赖管理插件,大部分情况下我们使用它都是和maven-jar-plugin配合来使用,将项目的执行jar和依赖的jar进行分离处理,这样可以减少项目执行jar的大小。除了上面的功能,maven-dependency-plugin还提供了很多其他的操作,下面我们通过实例来看看如何使用这个插件。
一、准备工作
首先我们准备一个springboot的项目,在项目中添加如下依赖:
org.springframework.boot spring-boot-starter-web org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.2.Final commons-lang commons-lang 2.6 commons-io commons-io 2.4 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test 我们用springboot项目为例子,当然这也适用于其他普通的项目。
二、拷贝指定的jar
org.apache.maven.plugins maven-dependency-plugin 3.6.1 copy package copy com.google.guava guava 32.1.3-jre jar false ${project.build.directory}/guava optional-guava.jar 然后运行:
mvn clean dependency:copy
或者
mvn clean -DskipTests package
在target目录下会生成guava文件夹,下面生成optional-guava.jar文件,如果不使用destFileName就会默认生成guava-32.1.3-jre.jar文件
另外这里的artifactItem标签里的依赖可以时项目依赖的,也可以不是项目依赖的,如果是项目依赖的jar,就不需要version属性,如果不是项目依赖的就需要version属性
三、拷贝项目依赖的所有jar
org.apache.maven.plugins maven-dependency-plugin 3.6.1 copy-dependencies package copy-dependencies ${project.build.directory}/lib false false lombok,commons-io runtime 然后运行:
mvn clean -DskipTests package
这样会在target目录下生成lib文件夹,下面是所有的jar依赖
- excludeTransitive
排除传递依赖,如果设置为true,上面的项目中,spring-boot-starter-web中的所有依赖将不会被提前,一般情况需要设置为false
- stripVersion
提取的依赖jar文件是否去掉版本号
- excludeArtifactIds
排除指定的ArtifactId依赖
- includeScope
包含哪些scope的依赖
四、解压依赖的jar文件
org.apache.maven.plugins maven-dependency-plugin 3.6.1 unpack prepare-package unpack com.google.guava guava 32.1.3-jre jar false ${project.build.directory}/classes **/*.class,**/*.xml **/*test.class 运行打包命令:
mvn clean -DskipTests package
上面这段配置我们将guava的依赖解压,并将解压的class文件打包到我们自己的项目里来
五、解压项目所有依赖的jar文件
org.apache.maven.plugins maven-dependency-plugin 3.6.1 unpack-dependencies prepare-package unpack-dependencies **/*.class **/*.properties ${project.build.directory}/classes false true 上面这段配置,我们将项目所有的依赖解压,并打包到我们的项目中,这里其实就和maven-assembly-plugin和maven-shade-plugin插件打包功能一样
六、下载所有依赖的jar的源代码
org.apache.maven.plugins maven-dependency-plugin 3.6.1 src-dependencies package unpack-dependencies sources false ${project.build.directory}/sources commons-lang,commons-io 这样我们就把commons-lang和commons-io的源代码下载下来了
七、分析pom.xml中的依赖使用情况
org.apache.maven.plugins maven-dependency-plugin 3.6.1 analyze analyze-only true com.google.code.findbugs:jsr305 com.google.code.findbugs:annotations com.google.code.findbugs:annotations com.google.code.findbugs:annotations 运行命令:
mvn clean dependency:analyze
运行结果:
[WARNING] Used undeclared dependencies found: [WARNING] org.springframework.boot:spring-boot:jar:2.2.5.RELEASE:compile [WARNING] org.slf4j:slf4j-api:jar:1.7.30:compile [WARNING] org.springframework:spring-web:jar:5.2.4.RELEASE:compile [WARNING] org.springframework:spring-context:jar:5.2.4.RELEASE:compile [WARNING] org.springframework:spring-core:jar:5.2.4.RELEASE:compile [WARNING] org.springframework.boot:spring-boot-autoconfigure:jar:2.2.5.RELEASE:compile [WARNING] org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test [WARNING] org.springframework.boot:spring-boot-test:jar:2.2.5.RELEASE:test [WARNING] jakarta.validation:jakarta.validation-api:jar:2.0.2:compile [WARNING] org.springframework:spring-beans:jar:5.2.4.RELEASE:compile [WARNING] Unused declared dependencies found: [WARNING] org.springframework.boot:spring-boot-starter-web:jar:2.2.5.RELEASE:compile [WARNING] org.projectlombok:lombok:jar:1.18.12:compile [WARNING] org.springframework.boot:spring-boot-starter-test:jar:2.2.5.RELEASE:test
这里列出了jar包的使用情况,和申明情况
八、列出pom.xml中的依赖树关系
这里直接运行插件的goal即可:
mvn dependency:tree
运行结果:
[INFO] --- dependency:3.6.1:tree (default-cli) @ test-mvn-pkg-exe-springboot --- [INFO] org.example:test-mvn-pkg-exe-springboot:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.5.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.5.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:2.2.5.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.5.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.5.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile [INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.25:runtime [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.2.5.RELEASE:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.2:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.2:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.10.2:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.2:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.2:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.5.RELEASE:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.31:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.31:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.31:compile [INFO] | +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.5.RELEASE:compile [INFO] | | +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile [INFO] | | \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.5.1:compile [INFO] | +- org.springframework:spring-web:jar:5.2.4.RELEASE:compile [INFO] | | \- org.springframework:spring-beans:jar:5.2.4.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.2.4.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:5.2.4.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:5.2.4.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:5.2.4.RELEASE:compile [INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile [INFO] +- commons-lang:commons-lang:jar:2.6:compile [INFO] +- commons-io:commons-io:jar:2.4:compile [INFO] +- org.projectlombok:lombok:jar:1.18.12:compile [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.2.5.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-test:jar:2.2.5.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.5.RELEASE:test [INFO] +- com.jayway.jsonpath:json-path:jar:2.4.0:test [INFO] | +- net.minidev:json-smart:jar:2.3:test [INFO] | | \- net.minidev:accessors-smart:jar:1.2:test [INFO] | | \- org.ow2.asm:asm:jar:5.0.4:test [INFO] | \- org.slf4j:slf4j-api:jar:1.7.30:compile [INFO] +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.2:test [INFO] | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test [INFO] +- org.junit.jupiter:junit-jupiter:jar:5.5.2:test [INFO] | +- org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test [INFO] | | +- org.opentest4j:opentest4j:jar:1.2.0:test [INFO] | | \- org.junit.platform:junit-platform-commons:jar:1.5.2:test [INFO] | +- org.junit.jupiter:junit-jupiter-params:jar:5.5.2:test [INFO] | \- org.junit.jupiter:junit-jupiter-engine:jar:5.5.2:test [INFO] +- org.junit.vintage:junit-vintage-engine:jar:5.5.2:test [INFO] | +- org.apiguardian:apiguardian-api:jar:1.1.0:test [INFO] | +- org.junit.platform:junit-platform-engine:jar:1.5.2:test [INFO] | \- junit:junit:jar:4.12:test [INFO] +- org.mockito:mockito-junit-jupiter:jar:3.1.0:test [INFO] +- org.assertj:assertj-core:jar:3.13.2:test [INFO] +- org.hamcrest:hamcrest:jar:2.1:test [INFO] +- org.mockito:mockito-core:jar:3.1.0:test [INFO] | +- net.bytebuddy:byte-buddy:jar:1.10.8:test [INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.10.8:test [INFO] | \- org.objenesis:objenesis:jar:2.6:test [INFO] +- org.skyscreamer:jsonassert:jar:1.5.0:test [INFO] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test [INFO] +- org.springframework:spring-core:jar:5.2.4.RELEASE:compile [INFO] | \- org.springframework:spring-jcl:jar:5.2.4.RELEASE:compile [INFO] +- org.springframework:spring-test:jar:5.2.4.RELEASE:test [INFO] \- org.xmlunit:xmlunit-core:jar:2.6.3:test
这个功能应该很实用,有时我们要解决依赖冲突,通过这个插件能看到各个依赖的具体情况
如果项目依赖很多,我们可以将依赖关系树输出到文本,具体命令如下:
mvn dependency:tree -DoutputFile=tree.txt
如果我们只查看某个固定的依赖树关系可以使用includes,格式为:${groupId} : ${artifactId},多个使用逗号隔开
mvn dependency:tree -Dincludes=org.springframework.boot:spring-boot-starter-web,commons-lang:commons-lang
如果要排除某些依赖可以使用excludes,这样被excludes的依赖就不会列出来:
mvn dependency:tree -Dexcludes=org.apache.maven*,org.codehaus.plexus
- excludeTransitive
还没有评论,来说两句吧...