概要
Maven提供了很多的插件,具體有哪些插件,可以在官網上查到:
本篇博客主要是總結下對maven dependency插件的使用心得。
maven dependency插件的主要作用:
它屬於工具類的插件。它提供了操作構件(artifact)的能力,可以從本地或者遠程倉庫 複製或者解壓特定構件到指定位置。
目標(goals)
Dependency插件支持許多目標(goals),可以參考下面鏈接:
這裏呢,主要介紹copy和copy-dependencies、unpack、get這幾個goals。
copy
通過在 pom.xml 文件中的插件配置處定義一系列構件,可以做到複製、重命名、指定版本等操作。它可以解決本地倉庫或者項目中缺少某個構件文件的問題,並把它們放到指定位置。
插件配置細節可以看官網介紹
在pom.xml中的配置參考如下:
<project>
[...]
<build>
<plugins>
<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>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
配置完,可以通過如下命令行執行:
mvn dependency:copy
copy-dependencies
作用:
從倉庫中複製項目依賴的構件到指定位置。
插件配置細節可以看官網介紹
在pom.xml中的配置參考如下:
<project>
[...]
<build>
<plugins>
<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>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
配置完,可以通過如下命令行執行:
mvn dependency:copy-dependencies
unpack
作用:
從倉庫中抓取一系列構件,然後解壓它們到指定位置。
插件配置細節可以看官網介紹
在pom.xml中的配置參考如下:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
<includes>**/*.class,**/*.xml</includes>
<excludes>**/*test.class</excludes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<excludes>**/*.properties</excludes>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
配置完,可以通過如下命令行執行:
mvn dependency:unpack
get
作用:
從指定的倉庫解析單個構件(artifact),包括可傳遞性。
插件配置細節可以看官網介紹
操作命令如下:舉例獲得spring-core
mvn dependency:get -DgroupId=org.springframework -DartifactId=spring-core -Dversion=5.1.5.RELEASE transitive=true
其中,transitive=true 代表同時還要抓取該構件的依賴構件。
總結
maven提供了強大的插件功能,遇到任何不清楚地,可以去官網查找資料,然後本地嘗試