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 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

时间: 2024-08-24 16:15:44

maven-dependency-plugin插件的使用的相关文章

question --&gt; maven assembly plugin 修改文件默认权限

使用maven assembly plugin插件添加执行脚本时,发现默认权限为644,还需要手动添加执行权限.这很麻烦,于是查看文档 官方文档 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet fileMode String Similar to a UNIX permission, sets the file mode of the files included. THIS IS

eclipse maven plugin 插件 安装 和 配置

环境准备: eclipse(Helios) 3.6 maven 3.0.4 maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版本是 3.0.4,我用的也是这个. 首先去官网下载 Maven:http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.tar.gz 下载完成之后将其解压,我将解压后的文件夹重命名成 mave

eclipse maven plugin 插件 安装 和 配置(2)

eclipse maven plugin 插件 安装 和 配置(2) 就像上篇文章所说,折腾一会终于安装完成,终于松了一口气,不料再次打开eclipse时又有错误信息,在网上找了找,找了篇比较详细的,原文地址: http://www.sunchis.com/html/hsware/software/2011/1102/371.html 在Eclipse中安装了m2eclipse(maven插件),安装完成后重启Eclipse,出现下列警告:Please make sure the -vm opt

深入理解maven构建生命周期和各种plugin插件

深入理解maven构建生命周期和各种plugin插件 本博文不会长篇大论的讨论生命周期的概念,而是从各种plugin的实际功能和应用出发,来讨论maven的实际应用,说得通透一点,生命周期(lifecycle)可以理解成由各种plugin按照一定的顺序执行来完成java项目清理.编译.打包.测试.布署等整个项目的流程的一个过程. 生命周期(lifecycle)由各个阶段组成,每个阶段由maven的插件plugin来执行完成.生命周期(lifecycle)主要包括clean.resources.c

使用Eclipse的Maven插件时maven dependency找不到包的解决办法

引入maven项目,提示找不到maven dependency包时候: 解决方法如下: 1.eclipse菜单 window-> show view –> other –> Maven 2.在打开的窗口里,右键 local repositories –> local repository ,右击选择 rebuild index,耐心的等待重新下载.

学习 Maven之Maven Enforcer plugin

1.Maven Enforcer plugin是什么鬼? 在说这个插件是什么前我们先思考这么一个问题:当我们开发人员进入项目组进行开发前,要准备开发环境,而领导总是会强调工具的统一,编译环境的统一.比如要求所有开发人员使用JDK1.8进行开发. 开发人员接下来就是去下载指定版本的JDK,然后开始开发.但是如果开发人员的机器配置比较多,有好几个版本的JDK,而他虽然下载了JDK1.8,但是忘记配置环境变量,很有可能他用了JDK1.6进行的编译. 问题有了,该如何解决? Maven Enforcer

【maven详解-插件】maven 插件机制

Maven插件运行方式 Maven在运行命令时都会对应每个生命周期,每个生命周期在执行时都会看pom.xml文件中配置了哪些插件,然后运行它. Maven 的生命周期与插件相互绑定,用以完成实际的构建任务.更具体而言,是生命周期的阶段与插件的目标相互绑定,以完成某个具体的构建任务. 具体的功能由插件(Plugin)实现.一个插件可以实现多个目标(Goal) 为了解耦插件的功能和工程阶段,实现高度的可配置性,maven规定插件只是实现目标的功能, 通过配置来决定在哪个阶段执行(Execution)

maven常用plugin

Dependency management plugin Maven dependencies have six possible scopes: maven依赖的6个有效范围 Compile: This is the default scope. Compile dependencies are available inthe classpaths.Provided: This scope assumes that the JDK or the environment providesdepe

maven集成tomcat7插件运行web项目

maven集成tomcat插件运行web项目1.修改pom.xml如下所示:添加依赖servlet,jsp,jstl,tomcat插件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

如何通过Maven的Tomcat插件运行Web工程

去tomcat官网http://tomcat.apache.org/,左侧栏Apache Tomcat下的Maven Plugin,点进去选择最新版本Version 2.2 通过介绍可知,使用tomcat的maven插件有两种配置方式: 第一种:在pom.xml文件的<build></build>中加入如下配置: 1 <pluginManagement> 2 <plugins> 3 <plugin> 4 <groupId>org.ap