maven-dependency-plugin插件的使用
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
maven-dependency-plugin插件的使用的更多相关文章
- maven release plugin插件
1.打包版本区别 SNAPSHOT 快照版本(开发阶段,不稳定,容易出现bug)RELEASE 正式版本(外部依赖使用阶段,稳定,很少出现bug)Tag :标记每次代码提交的版本(比较稳定,类似分支) ...
- Maven版本管理-Maven Release Plugin插件
一.什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基 ...
- 记录一次maven打包时将test目录下的类打包到jar中,Maven Assembly Plugin的使用
今天有人问我打包后找不到主类,运行的类写在test中.按照常规,test目录下的文件不会打包到jar包中.(但是我测试一个springboot工程就可以,这里之后再研究) 具体解决如下 第一步:在po ...
- 学习Maven之Maven Enforcer Plugin
1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比 ...
- Maven集成Tomcat插件
目录 类似插件及版本区别: 本地运行,启动嵌入式tomcat: 错误一: 错误二: Idea运行调试: vscode运行调试: 远程部署: 项目中的pom.xml配置: Tomcat中的tomcat- ...
- maven surefire plugin介绍
示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- Maven中常用插件的配置
在Maven项目的pom.xml文件中配置插件信息,使用<build></build>标签 1.配置JDK版本插件和Tomcat版本插件 <build> <! ...
- Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告
最近在做基于jenkins ant junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...
- [Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes
链接地址:[Selecting Contents for Uber JAR](http://maven.apache.org/plugins/maven-shade-plugin/examples/i ...
- 施用 maven shade plugin 解决 jar 或类的多版本冲突
施用 maven shade plugin 解决 jar 或类的多版本冲突 使用 maven shade plugin 解决 jar 或类的多版本冲突java 应用经常会碰到的依赖的三方库出现版本 ...
随机推荐
- js日期时间比较函数
转自:http://www.cnblogs.com/zxjyuan/archive/2010/09/07/1820708.html js日期比较(yyyy-mm-dd) function duibi( ...
- Python初学者需要注意的问题
一.注意你的Python版本 Python官方网站为http://www.python.org/,当前最新版本为3.4.0 alpha,稳定版本为3.3.2,在3.0版本时,Python的语法改动较大 ...
- shell来start、stop、restart应用程序模板
这里使用shell中的case语法: case分支语句格式如下: case $变量名 in 模式1) 命令列表 ;; 模式2) 命令列表 ;; *) ;; esac case行尾必须为单词“in”,每 ...
- led显字风扇原理?
神奇的是上面的图案居然会变,十分好奇,求告知原理?? 其实就是依靠转速计算出LED灯变化的频率.这点和老式CRT的显示原理差不多.比如说风扇的转速时60rpm就是每分钟60圈,每秒1圈(当然实际转速快 ...
- C 语言函数参数只能传指针,不能传数组
今天被要求编写一个C/C++冒泡算法的程序,心想这还不是手到擒来的事儿,虽然最近都是用Javascript程序,很少写C/C++程序,但是好歹也用过那么多年的C语言: 首先想的是怎么让自己的代码看上去 ...
- MyBatis知多少(1)
SQL (Structured Query Language,结构化查询语言)己经存在很长一段时间了.自从第一次提出“数据可以被规范化为一组相互关联的表”这样的思想以来,已经超过35年了. 从那时起, ...
- 编写高质量JS代码的68个有效方法(十三)
No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应 ...
- Pagekit – 现代化技术构建的轻量的 CMS 系统
Pagekit 是一个模块化,轻量的 CMS 系统,基于现代化的技术,如 Symfony 组件和 Doctrine.它提供了一个很好的平台,用于主题和延伸开发.Pagekit 为您提供了工具来创造美丽 ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- SQL Server中的事务日志管理(6/9):大容量日志恢复模式里的日志管理
当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...