maven插件maven-dependency-plugin使用详解

文章目录


前言

maven-dependency-plugin插件时一个依赖管理插件,大部分情况下我们使用它都是和maven-jar-plugin配合来使用,将项目的执行jar和依赖的jar进行分离处理,这样可以减少项目执行jar的大小。除了上面的功能,maven-dependency-plugin还提供了很多其他的操作,下面我们通过实例来看看如何使用这个插件。


一、准备工作

首先我们准备一个springboot的项目,在项目中添加如下依赖:

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

我们用springboot项目为例子,当然这也适用于其他普通的项目。

二、拷贝指定的jar

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>32.1.3-jre</version>
                <type>jar</type>
                <overWrite>false</overWrite>
                <outputDirectory>${project.build.directory}/guava</outputDirectory>
                <destFileName>optional-guava.jar</destFileName>
            </artifactItem>
        </artifactItems>
    </configuration>
</plugin>

然后运行:

bash 复制代码
mvn clean dependency:copy

或者

bash 复制代码
mvn clean -DskipTests package

在target目录下会生成guava文件夹,下面生成optional-guava.jar文件,如果不使用destFileName就会默认生成guava-32.1.3-jre.jar文件

另外这里的artifactItem标签里的依赖可以时项目依赖的,也可以不是项目依赖的,如果是项目依赖的jar,就不需要version属性,如果不是项目依赖的就需要version属性

三、拷贝项目依赖的所有jar

xml 复制代码
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <excludeArtifactIds>lombok,commons-io</excludeArtifactIds>
                <includeScope>runtime</includeScope>
<!--                            <excludeScope>runtime</excludeScope>-->
            </configuration>
        </execution>
    </executions>
</plugin>

然后运行:

bash 复制代码
mvn clean -DskipTests package

这样会在target目录下生成lib文件夹,下面是所有的jar依赖

  • excludeTransitive
    排除传递依赖,如果设置为true,上面的项目中,spring-boot-starter-web中的所有依赖将不会被提前,一般情况需要设置为false
  • stripVersion
    提取的依赖jar文件是否去掉版本号
  • excludeArtifactIds
    排除指定的ArtifactId依赖
  • includeScope
    包含哪些scope的依赖

四、解压依赖的jar文件

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>32.1.3-jre</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <includes>**/*.class,**/*.xml</includes>
                        <excludes>**/*test.class</excludes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

运行打包命令:

bash 复制代码
mvn clean -DskipTests package

上面这段配置我们将guava的依赖解压,并将解压的class文件打包到我们自己的项目里来

五、解压项目所有依赖的jar文件

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includes>**/*.class</includes>
                <excludes>**/*.properties</excludes>
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>

</plugin>

上面这段配置,我们将项目所有的依赖解压,并打包到我们的项目中,这里其实就和maven-assembly-plugin和maven-shade-plugin插件打包功能一样

六、下载所有依赖的jar的源代码

xml 复制代码
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>src-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <classifier>sources</classifier>
                <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
                <outputDirectory>${project.build.directory}/sources</outputDirectory>
                <includeArtifactIds>commons-lang,commons-io</includeArtifactIds>
            </configuration>
        </execution>
    </executions>
</plugin>

这样我们就把commons-lang和commons-io的源代码下载下来了

七、分析pom.xml中的依赖使用情况

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>analyze</id>
            <goals>
                <goal>analyze-only</goal>
            </goals>
            <configuration>
                <failOnWarning>true</failOnWarning>
                <ignoredDependencies>
                    <ignoredDependency>com.google.code.findbugs:jsr305</ignoredDependency>
                </ignoredDependencies>
                <ignoredUsedUndeclaredDependencies>
                    <ignoredUsedUndeclaredDependency>com.google.code.findbugs:annotations</ignoredUsedUndeclaredDependency>
                </ignoredUsedUndeclaredDependencies>
                <ignoredUnusedDeclaredDependencies>
                    <ignoredUnusedDeclaredDependency>com.google.code.findbugs:annotations</ignoredUnusedDeclaredDependency>
                </ignoredUnusedDeclaredDependencies>
                <ignoredNonTestScopedDependencies>
                    <ignoredNonTestScopedDependency>com.google.code.findbugs:annotations</ignoredNonTestScopedDependency>
                </ignoredNonTestScopedDependencies>
            </configuration>
        </execution>
    </executions>
</plugin>

运行命令:

bash 复制代码
mvn clean dependency:analyze

运行结果:

bash 复制代码
[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即可:

bash 复制代码
mvn dependency:tree

运行结果:

bash 复制代码
[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

这个功能应该很实用,有时我们要解决依赖冲突,通过这个插件能看到各个依赖的具体情况

如果项目依赖很多,我们可以将依赖关系树输出到文本,具体命令如下:

bash 复制代码
mvn dependency:tree -DoutputFile=tree.txt

如果我们只查看某个固定的依赖树关系可以使用includes,格式为:${groupId} : ${artifactId},多个使用逗号隔开

bash 复制代码
mvn dependency:tree -Dincludes=org.springframework.boot:spring-boot-starter-web,commons-lang:commons-lang

如果要排除某些依赖可以使用excludes,这样被excludes的依赖就不会列出来:

bash 复制代码
mvn dependency:tree -Dexcludes=org.apache.maven*,org.codehaus.plexus

相关推荐
捂月7 分钟前
如何在 Spring Boot 中配置数据库?
数据库·spring boot·后端
代码小鑫12 分钟前
【开源】A060-基于Spring Boot的游戏交易系统的设计与实现
java·开发语言·数据库·spring boot·后端
如来神掌十八式13 分钟前
Semaphore控制接口并发频率
java·semaphore
云初@17 分钟前
IDEA实现javaweb用户登录(增删改查)
java·ide·intellij-idea
代码小鑫20 分钟前
【开源】A059-基于SpringBoot的社区养老服务系统的设计与实现
java·开发语言·数据库·spring boot·后端
HEX9CF37 分钟前
【Anaconda】Anaconda3 下载与安装教程(Ubuntu 22.04)
linux·运维·ubuntu
paopaokaka_luck1 小时前
[384]基于springboot的药品管理系统
java·spring boot·后端
老马啸西风1 小时前
Neo4j APOC-01-图数据库 apoc 插件介绍
java
次次皮1 小时前
【方案三】JAVA中使用ocr(Umi-OCR)
java·ocr