说明
本文是对maven-dependency-plugin
常用配置的介绍,更详细的学习请参照官网文档地址: Apache Maven Dependency Plugin
maven-dependency-plugin
作用:依赖插件提供了操作构件的能力。它可以从本地或远程存储库复制或解压构件到指定位置。它有很多可用的goal,但是我们最常用到的是 dependency:copy
,dependency:copy-dependencies
及dependency:unpack
,dependency:unpack-dependencies
这四个
1. 使用演示
此处演示常用的dependency:copy
、dependency:copy-dependencies
和dependency:unpack
1.1 插件声明
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</build>
1.2 dependency:copy
它将从存储库中解析artifact,获取在插件配置部分定义的一系列构件,并将它们复制到指定的位置。如果需要,还可以重命名它们或去除版本号。如果远程仓库存在本地仓库不存在这些构件,该目标还可以从远程仓库解析这些构件。添加phase和goal如下
com.alibaba.fastjson拷到libs目录下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
1.3 dependency:copy-dependencies
与dependency:copy
类似,只不过是将项目所有的依赖copy到指定的地方
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</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>
<!--覆盖发布工件-->
<overWriteReleases>false</overWriteReleases>
<!--覆盖快照项目-->
<overWriteSnapshots>false</overWriteSnapshots>
<!--覆盖不存在或比源文件更旧的构建产物-->
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
1.4 dependency:unpack
官方释义:类似于复制,但会解包。简单理解就是把依赖jar解包复制,我们可以利用解包效果分离出一些依赖jar已经写好的脚本
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<outputDirectory>${project.build.directory}/unpack/slf4j</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
<outputDirectory>${project.build.directory}/unpack/fastjson</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>