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 isoutputDirectory.
prependGroupId boolean 2.7 Prepend artifact groupId during copy
Default value isfalse.
User property ismdep.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 isexcludeArtifactIds.
excludeClassifiers String 2.0 Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default).
User property isexcludeClassifiers.
excludeGroupIds String 2.0 Comma separated list of GroupId Names to exclude.
User property isexcludeGroupIds.
excludeScope String 2.0 Scope to exclude. An Empty string indicates no scopes (default).
User property isexcludeScope.
excludeTransitive boolean 2.0 If we should exclude transitive dependencies
Default value isfalse.
User property isexcludeTransitive.
excludeTypes String 2.0 Comma Separated list of Types to exclude. Empty String indicates don't exclude anything (default).
User property isexcludeTypes.
includeArtifactIds String 2.0 Comma separated list of Artifact names to include.
User property isincludeArtifactIds.
includeClassifiers String 2.0 Comma Separated list of Classifiers to include. Empty String indicates include everything (default).
User property isincludeClassifiers.
includeGroupIds String 2.0 Comma separated list of GroupIds to include.
User property isincludeGroupIds.
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:

  • runtime scope gives runtime and compile dependencies,
  • compile scope gives compile, provided, and system dependencies,
  • test (default) scope gives all dependencies,
  • provided scope just gives provided dependencies,
  • system scope just gives system dependencies.

User property isincludeScope.

includeTypes       String 2.0 Comma Separated list of Types to include. Empty String indicates include everything (default).
User property isincludeTypes.

例如当前工程有以下依赖:

<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插件的使用相关推荐

  1. 关于maven的plugin插件问题(踩坑)

    关于maven的plugin插件问题(踩坑) 今天几乎花了一整天的时间来弄插件的问题 特此来复盘一下,没想到不起眼的plugin竟然困扰了我一天 文章目录 关于maven的plugin插件问题(踩坑) ...

  2. Maven 的 Maven Compiler Plugin 插件

    当前 Maven Compiler Plugin 这个插件的版本为 3.10.1 你可以使用最新的版本. 我们今天主要说一下这个插件中 --release 参数 这个插件的配置方法如下: <?x ...

  3. Maven版本管理-Maven Release Plugin插件

    一.什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基 ...

  4. 用Maven Archetype Plugin快速创建项目

    还在用mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.ryanote -Dart ...

  5. 【Maven】自定义插件(新建项目开始):AbstractMojo,Mojo,pluginGroups,build,plugin,execution,phase,goals

    一.前言 实际开发中很少会有开发插件的需求 只是通过此次开发更好地理解插件目标与生命周期阶段的关系 二.插件开发 2.1 创建项目 2.2 环境准备 根据个人情况修改项目的Maven配置 修改工程打包 ...

  6. eclipse maven plugin 插件 安装 和 配置

    本文转自:http://www.blogjava.net/fancydeepin/archive/2012/07/13/eclipse_maven3_plugin.html maven3 安装: 安装 ...

  7. 学习Maven之Maven Enforcer Plugin

    1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比 ...

  8. Maven的单元测试插件maven-surefire-plugin详解

    文章目录 pom.xml 的配置(注意事项,非常重要) 测试案例 执行测试命令 surefire 插件配置 pom.xml 的配置(注意事项,非常重要) 1.必须引入 maven-surefire-p ...

  9. 使用Maven的assembly插件实现自定义打包

    一.背景 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...

  10. maven scala plugin 实现jvmArgs,执行过程原理解析笔记

    scala-maven-plugin 构建scala,springboot工程 我们在使用maven构建scala工程的时候, 要使用scala-maven-plugin插件,常规配置如下 : < ...

最新文章

  1. git-【五】远程仓库
  2. python2 webserver class
  3. jupyterlab debugger+显示图片
  4. IE/Firefox中css兼容常见问题
  5. 【面试系列】之一:关于Cmd和Amd
  6. 达策— 毕盛财务分析系统
  7. hashcat源码分析1
  8. linux shell 脚本路径,linux获取shell脚本所在绝对路径操作介绍
  9. 阿里开源 GNN 框架 Graph-Learn,实现了各类可复用模型和编程接口!
  10. JQueryDOM之CSS操作
  11. 计算机程序设计里的奇书
  12. .net通用CMS快速开发框架——问题:Dapper通用的多表联合分页查询怎么破?
  13. linux下oracle完全卸载,linux环境下卸载oracle 11g
  14. 高德地图自定义主题地图,盖住中国红色边境线
  15. 苹果手机搭建php服务器,给自己搭建iPhone Installer软件源
  16. linux mysql导出表中的数据_MySQL导出指定表中的数据
  17. 基于FPGA的VGA显示,简单的历程和注释(DE2-115)
  18. LANP+KEEPALIVED集群(一)
  19. 【机器学习】在生产环境使用Kafka构建和部署大规模机器学习
  20. Google 镜像站搜集[转]

热门文章

  1. 设计模式学习之策略模式(Strategy,行为型模式)(13)
  2. mpls 跨域 optionA 配置实例
  3. .NET 源代码的安全性(源代码工具真正比拼) (論)
  4. php简单的设计模式,MVC,composer
  5. C#学习历程(四)[实际问题]
  6. 使用Ruby来实现批量更新AD中字段
  7. 洗衣机一边进水一边出水 更换排水阀皮碗
  8. spring+hibernate 下载
  9. iOS学习笔记之正则表达式
  10. Java设计模式(四)——再谈观察者模式