使用maven 找到依赖的JAR包

1.业务场景

有些时候,我需要知道某个jar包依赖了哪些包,这个时候可以通过maven 依赖插件将依赖的包copy出来。

2.具体做法

我们可以创建一个空的项目,增加 pom.xml 文件,增加我们需要的依赖包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.redxun</groupId>
        <artifactId>jpaas-cloud</artifactId>
        <version>6.8.5-SNAPSHOT</version>
    </parent>

    <groupId>com.study</groupId>
    <artifactId>ActivitiDep</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7.1.0.2.M7</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-image-generator</artifactId>
            <version>7.1.0.2.M7</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.1.0.2.M7</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <!-- 复制依赖 -->
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 依赖包 输出目录 -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

添加这个后,我们执行 maven 的 package打包命令,maven就会将我们需要的JARcopy到指定的目录下面。

3.输出依赖树

有些时候,我需要输出依赖的结构,那么可以使用下面的命令进行输出

mvn  dependency:tree > dep.txt

输出的内容如下:

[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ ActivitiDep ---
[INFO] com.study:ActivitiDep:jar:6.8.5-SNAPSHOT
[INFO] +- org.activiti:activiti-engine:jar:7.1.0.2.M7:compile
[INFO] |  +- org.activiti.api:activiti-api-runtime-shared:jar:7.1.0.2.M7:compile
[INFO] |  +- org.activiti.core.common:activiti-project-model:jar:7.1.0.2.M7:compile
[INFO] |  +- org.activiti:activiti-bpmn-converter:jar:7.1.0.2.M7:compile
[INFO] |  +- org.activiti:activiti-bpmn-model:jar:7.1.0.2.M7:compile
[INFO] |  +- org.activiti:activiti-process-validation:jar:7.1.0.2.M7:compile
[INFO] |  +- org.apache.commons:commons-email:jar:1.5:compile
[INFO] |  |  \- com.sun.mail:javax.mail:jar:1.5.6:compile
[INFO] |  |     \- javax.activation:activation:jar:1.1:compile
[INFO] |  +- org.mybatis:mybatis:jar:3.5.1:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.15.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:5.2.15.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.2.15.RELEASE:compile
[INFO] |  +- de.odysseus.juel:juel-api:jar:2.2.7:compile
[INFO] |  +- de.odysseus.juel:juel-impl:jar:2.2.7:compile
[INFO] |  +- de.odysseus.juel:juel-spi:jar:2.2.7:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.11.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.4:compile
[INFO] |  +- joda-time:joda-time:jar:2.10.5:compile