【01】Maven依赖插件之maven-dependency-plugin

白君之
2023-12-01

一、插件目标(goal)

1、analyze:分析项目依赖,确定哪些是已使用已声明的,哪些是已使用未声明的,哪些是未使用已声明的

2、analyze-dep-mgt:分析项目依赖,列出已解析的依赖项与dependencyManagement中定义的依赖项不匹配的部分

3、analyze-report:生成项目报告

4、analyze-duplicate:分析pom.xml中的<dependencies/><dependencyManagement/>标记,确定重复声明的依赖项

5、build-classpath:告诉Maven以类路径格式从本地存储库输出依赖项的路径

6、copy:将配置在插件中的jar包复制到指定位置

7、copy-dependencies:将项目pom文件中定义的所有依赖及其传递依赖复制到指定位置

8、display-ancestors:显示项目所有祖先pom

8、get:从远程库中解析出一个构件

9、go-offline:让maven解决该项目所依赖的所有内容,以准备脱机

10、list:resolve的别名,列出项目的所有依赖项

11、list-repositores:显示项目所有依赖项目,然后列出使用的存储库

12、properties:包含文件系统上的工件的每个项目依赖项设置一个属性

13、purge-local-repository:清除本地存储库中的依赖

14、resolve:告诉Maven解析所有依赖项并显示版本。JAVA 9注意: 在使用Java 9运行时将显示模块名称。

15、resolve-plugins:告诉Maven解决插件及其依赖项

16、sources:告诉Maven解析所有依赖项及其源附件、并显示版本

17、tree:显示该项目的依赖关系树

18、unpack:解压缩

19、unpack-dependencies:与copy-dependencies功能一致,只是会解压

二、使用举例

1、复制特定的构件(jar包)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <type>jar</type>
                <overWrite>false</overWrite>
                <!-- jar包的存放位置 -->
                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                <!--给jar包取别名 -->
                <destFileName>optional-new-name.jar</destFileName>
            </artifactItem>
        </artifactItems>
        <outputDirectory>${project.build.directory}/wars</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>true</overWriteSnapshots>
    </configuration>
</plugin>

2、复制所有的依赖jar包到指定位置 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!-- jar包存放位置 -->
                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

 若是以命令行执行,则改为以下配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <type>jar</type>
                <overWrite>false</overWrite>
                <outputDirectory>target/sources</outputDirectory>
                <destFileName>34.jar</destFileName>
                <includes>**/*.class,**/*.xml</includes>
                <excludes>**/*test.class</excludes>
            </artifactItem>
        </artifactItems>
        <!-- other configurations here -->
    </configuration>
</plugin>

3、解压所有依赖项

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includes>**/*.class</includes>
                <excludes>**/*.properties</excludes>
                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>
</plugin>
 类似资料: