maven-dependency-plugin插件的使用
maven-dependency-plugin是 处理与依赖相关的插件。它有很多可用的goal,大部分是和依赖构建、分析和解决相关的goal,这部分goal可以直接用maven的命令操作,例 如:mvn dependency:tree、mvn dependency:analyze;这类操作在平时的maven应用中很少会用到。这里主要介绍除此之外的、用得最多的几个操作:copy, copy-dependencies和它们对应的unpack, unpack-dependencies.
首先声明插件:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.8</version></plugin></plugins> </build>
copy 和 unpack
copy操作可以用来将某个(些)maven artifact(s)拷贝到某个目录下。添加phase和goal如下:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.8</version><executions><execution><phase>package</phase><goals><goal>copy</goal></goals></execution></executions></plugin></plugins> </build>
然后就是配置,copy可以的配置的项比较多,详细的请参考:copy配置。下面是一些常用项说明:
Name Type Since Description
artifactItems | List | 1.0 | Collection of ArtifactItems to work on. (ArtifactItem contains groupId, artifactId, version, type, classifier, outputDirectory, destFileName and overWrite.) See Usage for details. |
outputDirectory | File | 1.0 |
Default output location used for mojo, unless overridden in ArtifactItem. Default value is: ${project.build.directory}/dependency. User property is: outputDirectory. |
prependGroupId | boolean | 2.7 |
Prepend artifact groupId during copy Default value is: false. User property is: mdep.prependGroupId. |
- prependGroupId: 用来指示拷出来的library名字需要不需要加上groupId,默认是不加
- outputDirectory: 用来指定拷出后Libraries的存放地
这里除了artifactItems没有默认值,需要指定外,所有其他的选项都可以被忽略:
<configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></artifactItem></artifactItems> </configuration>
以上配置会将junit包拷到target/dependency目录下,文件名为:junit-4.11.jar。
如果想把它拷到lib目录下,可以如下配置:
<configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></artifactItem></artifactItems><outputDirectory>lib</outputDirectory> </configuration>
或者:
<configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><outputDirectory>lib</outputDirectory></artifactItem></artifactItems> </configuration>
根据上面的说明,artifactItem里可以有以下几个参数:
- groupId
- artifactId
- version
- type
- classifier
- outputDirectory
- destFileName
- overWrite
同样的参数,artifactItem里的优先级更高,例如:
<configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></artifactItem><artifactItem><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><outputDirectory>lib2</outputDirectory></artifactItem></artifactItems><outputDirectory>lib</outputDirectory> </configuration>
其中junit会拷到lib目录下,因为它没有定义自己的outputDirectory;slf4j-log4j12会拷到lib2下,因为它定义了自己的outputDirectory。
unpack和copy类似,只不过它会把拷来的包解开,例如:
<executions><execution><phase>package</phase><goals><goal>unpack</goal></goals></execution> </executions> <configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></artifactItem><artifactItem><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><outputDirectory>lib2</outputDirectory></artifactItem></artifactItems><outputDirectory>lib</outputDirectory> </configuration>
则junit和slf4j-log4j12拷完以后,放到lib和lib2下的不再是Jar包,还是Jar包里的内容。
copy-dependencies 和 unpack-dependencies
上面介绍的copy 和 unpack操作是由要拷某个包,这个包需要具体指定要拷哪个包,与当前工程的依赖没有关系。copy-dependencies和它有点类似,但是它是 用来拷当前工程的依赖包的,典型的,例如我们有一个web应用,当打成war包的时候,它所有的依赖也需要被打到应用中。
copy-dependencies的参数有很多,详细的可以参考:copy-dependencies Doc,但是几乎所有都有默认值。所以一个最简单的定义如下:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.8</version><executions><execution><phase>package</phase><goals><goal>copy-dependencies</goal></goals></execution></executions> </plugin>
这里没有指定任何配置,所有的参数都用默认值,则当前工程的所有依赖(直接、间接的)都会被拷到target/dependency目录下。
也可以使用outputDirectory指定存放在。另外,以下几个参数可以控制哪些依赖将被拷出(或排除):
Name Type Since Description
excludeArtifactIds | String | 2.0 |
Comma separated list of Artifact names to exclude. User property is: excludeArtifactIds. |
excludeClassifiers | String | 2.0 |
Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default). User property is: excludeClassifiers. |
excludeGroupIds | String | 2.0 |
Comma separated list of GroupId Names to exclude. User property is: excludeGroupIds. |
excludeScope | String | 2.0 |
Scope to exclude. An Empty string indicates no scopes (default). User property is: excludeScope. |
excludeTransitive | boolean | 2.0 |
If we should exclude transitive dependencies Default value is: false. User property is: excludeTransitive. |
excludeTypes | String | 2.0 |
Comma Separated list of Types to exclude. Empty String indicates don't exclude anything (default). User property is: excludeTypes. |
includeArtifactIds | String | 2.0 |
Comma separated list of Artifact names to include. User property is: includeArtifactIds. |
includeClassifiers | String | 2.0 |
Comma Separated list of Classifiers to include. Empty String indicates include everything (default). User property is: includeClassifiers. |
includeGroupIds | String | 2.0 |
Comma separated list of GroupIds to include. User property is: includeGroupIds. |
includeScope | String | 2.0 |
Scope to include. An Empty string indicates all scopes (default). The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom. In summary:
User property is: includeScope. |
includeTypes | String | 2.0 |
Comma Separated list of Types to include. Empty String indicates include everything (default). User property is: includeTypes. |
例如当前工程有以下依赖:
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><scope>test</scope></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-script</artifactId><version>2.13.2</version></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-spring</artifactId><version>2.13.2</version></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-xstream</artifactId><version>2.13.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>3.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>3.2.4.RELEASE</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.10.0</version></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.7</version></dependency><dependency><groupId>org.ogce</groupId><artifactId>xpp3</artifactId><version>1.1.6</version></dependency> </dependencies>
要排除所有scope为test的依赖:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.8</version><executions><execution><phase>package</phase><goals><goal>copy-dependencies</goal></goals></execution></executions><configuration><includeScope>compile</includeScope></configuration> </plugin>
注意:这里不能<excludeScope>test</excludeScope>,这样会把所有compile级别的也排除。看下图:
Copied From: Dependencies Scopes
scope/phase-> | compile | test | run | assembly |
compile | U | U | U | U |
provided | U | ! | ! | ! |
runtime | ! | U | U | U |
test | ! | U | ! | ! |
说明:最左侧是表示dependency的scope级别,顶行表示maven的阶段,可以看出:compile级别的dependency会在所有阶段都被使用。
要排除所有camel的依赖,如下:
<configuration><excludeGroupIds>org.apache.camel</excludeGroupIds> </configuration>
要排除除camel-spring外的所有其他依赖如下:
<configuration><includeArtifactIds>camel-spring</includeArtifactIds> </configuration>
转载:http://liugang594.iteye.com/blog/2093082
转载于:https://www.cnblogs.com/a198720/articles/4206173.html
maven-dependency-plugin插件的使用相关推荐
- 关于maven的plugin插件问题(踩坑)
关于maven的plugin插件问题(踩坑) 今天几乎花了一整天的时间来弄插件的问题 特此来复盘一下,没想到不起眼的plugin竟然困扰了我一天 文章目录 关于maven的plugin插件问题(踩坑) ...
- Maven 的 Maven Compiler Plugin 插件
当前 Maven Compiler Plugin 这个插件的版本为 3.10.1 你可以使用最新的版本. 我们今天主要说一下这个插件中 --release 参数 这个插件的配置方法如下: <?x ...
- Maven版本管理-Maven Release Plugin插件
一.什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基 ...
- 用Maven Archetype Plugin快速创建项目
还在用mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.ryanote -Dart ...
- 【Maven】自定义插件(新建项目开始):AbstractMojo,Mojo,pluginGroups,build,plugin,execution,phase,goals
一.前言 实际开发中很少会有开发插件的需求 只是通过此次开发更好地理解插件目标与生命周期阶段的关系 二.插件开发 2.1 创建项目 2.2 环境准备 根据个人情况修改项目的Maven配置 修改工程打包 ...
- eclipse maven plugin 插件 安装 和 配置
本文转自:http://www.blogjava.net/fancydeepin/archive/2012/07/13/eclipse_maven3_plugin.html maven3 安装: 安装 ...
- 学习Maven之Maven Enforcer Plugin
1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比 ...
- Maven的单元测试插件maven-surefire-plugin详解
文章目录 pom.xml 的配置(注意事项,非常重要) 测试案例 执行测试命令 surefire 插件配置 pom.xml 的配置(注意事项,非常重要) 1.必须引入 maven-surefire-p ...
- 使用Maven的assembly插件实现自定义打包
一.背景 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...
- maven scala plugin 实现jvmArgs,执行过程原理解析笔记
scala-maven-plugin 构建scala,springboot工程 我们在使用maven构建scala工程的时候, 要使用scala-maven-plugin插件,常规配置如下 : < ...
最新文章
- Linux启动界面切换:图形界面-字符界面(转)
- django rest-framework 1.序列化 一
- xbox acc驱动win7_Xbox老大:希望第一方工作室能推出更多单机游戏_电竞
- Py之wxPython:利用wxPython设计GUI界面(图片背景+简单按钮)
- windows10 下Python3.5.4安装
- Linux系统下UDP发送和接收广播消息小样例
- HTML5废除元素,HTML5 与 HTML4 的区别(2) - 新增的元素和废除的元素
- gridview 实现排序 (在不是使用sqlDataSource控件,而在后台编码绑定gridview时,指定那个字段排序时使用。本例用了单层结构,可修改后应用于多层)
- 清理清理火狐历史记录
- ExceptionError
- python树莓派编程 沃尔弗拉姆_《 Python树莓派编程》——3.3 Python入门-阿里云开发者社区...
- automake 安装及使用
- Cadence Orcad Capture定时保存功能介绍图文视频教程
- 电子教室的功能有哪些-极域电子教室
- Python数据分析学习系列 六 数据加载、存储与文件格式
- PS不能直接拖入图片的解决办法
- 双十一自动领喵币工具
- 耳机不怕丢,Belkin最新降噪耳机支持苹果 Find My
- python求两数最大公因数_Python求两个数的最大公约数
- 【STM32 HAL】用旋钮电位器进行PID调参