目录

maven-dependency-plugin插件使用笔记

简介

maven-dependency-plugin是处理与依赖相关的插件。它有很多可用的goal,大部分是和依赖构建、分析和解决相关的goal,这部分goal可以直接用maven的命令操作,例如:mvn dependency:tree、mvn dependency:analyze;这类操作在平时的maven应用中很少会用到。这里主要介绍除此之外的、用得最多的几个操作:copy, copy-dependencies和它们对应的unpack, unpack-dependencies

使用

首先声明插件:

Xml代码

1
2
3
4
5
6
7
8
9
<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如下:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<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>

比如把junit拷到libs目录下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        <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>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>junit</groupId>
                                <artifactId>junit</artifactId>
                                <version>4.11</version>
                                <outputDirectory>${project.build.directory}/libs</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

执行mvn package打包命令之后,会多出libs目录

./1.jpg

然后就是配置,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没有默认值,需要指定外,所有其他的选项都可以被忽略:

Xml代码

1
2
3
4
5
6
7
8
9
<configuration>
    <artifactItems>
        <artifactItem>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </artifactItem>
    </artifactItems>
</configuration>

以上配置会将junit包拷到target/dependency目录下,文件名为:junit-4.11.jar。

如果想把它拷到lib目录下,可以如下配置:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<configuration>
    <artifactItems>
        <artifactItem>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </artifactItem>
    </artifactItems>
    <outputDirectory>lib</outputDirectory>
</configuration>

或者:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<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里的优先级更高,例如:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<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类似,只不过它会把拷来的包解开,例如:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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>

执行mvn package打包命令之后,slf4j复制到lib目录下,junit复制到libs目录下

./2.jpg

则junit和slf4j-log4j12拷完以后,放到lib和lib2下的不再是Jar包,还是Jar包里的内容。

copy-dependencies 和 unpack-dependencies

上面介绍的copy 和 unpack操作是由要拷某个包,这个包需要具体指定要拷哪个包,与当前工程的依赖没有关系。copy-dependencies和它有点类似,但是它是用来拷当前工程的依赖包的,典型的,例如我们有一个web应用,当打成war包的时候,它所有的依赖也需要被打到应用中。

copy-dependencies的参数有很多,详细的可以参考:copy-dependencies Doc,但是几乎所有都有默认值。所以一个最简单的定义如下:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<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: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 is: includeScope.
includeTypes String 2.0 Comma Separated list of Types to include. Empty String indicates include everything (default). User property is: includeTypes.

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

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<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的依赖:

Xml代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<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的依赖,如下:

Xml代码

1
2
3
<configuration>
    <excludeGroupIds>org.apache.camel</excludeGroupIds>
</configuration>

要排除除camel-spring外的所有其他依赖如下:

Xml代码

1
2
3
<configuration>
    <includeArtifactIds>camel-spring</includeArtifactIds>
</configuration>

插件目标(goals)标签说明

1、analyze:分析项目依赖,确定哪些是已使用已声明的,哪些是已使用未声明的,哪些是未使用已声明的

2、analyze-dep-mgt:分析项目依赖,列出已解析的依赖项与dependencyManagement中定义的依赖项不匹配的部分

3、analyze-report:分析项目依赖关系,并生成一个报告,该报告总结一下内容:使用和声明;使用和未声明的;未使用和声明

4、analyze-duplicate:分析pom.xml中的<dependencies/>和<dependencyManagement/>标记,确定重复声明的依赖项

5、build-classpath:告诉Maven以java.cp中使用的类路径格式从本地存储库输出依赖项的路径。类路径文件也可以与主要工件一起附加和安装/部署

6、copy:获取插件配置部分中定义的工件列表,并将它们复制到指定位置,重命名它们或根据需要剥离版本。如果本地存储库或者反应堆中都不存在远程工件,则此目标可以解决这些工件。

7、copy-dependencies:列出项目直接依赖项和(可选)传递性依赖项的列表,并将其复制到指定位置,如果需要,剥离版本。该目标也可以从命令行运行。

8、display-ancestors:显示项目所有祖先pom。在想要了解项目所有的父poms的连续集成系统中,这可能很有用。该目标也可以从命令行运行。

8、get:最终以可传递方式从指定的远程存储库解析单个工件

9、go-offline:让maven解决该项目所依赖的所有内容(依赖项、插件、报告),以准备脱机

10、list:解析别名,列出项目的所有依赖项

11、list-repositores:显示项目所有依赖关系,然后列出使用的存储库

12、properties:为每个项目依赖项设置一个属性,该属性包含文件系统上工件

13、purge-local-repository:清除本地存储库中的依赖,并重新解析

14、resolve:告诉Maven解析所有依赖项并显示版本。JAVA 9注意: 在使用Java 9运行时将显示模块名称。

15、resolve-plugins:告诉Maven解决插件及其依赖项

16、sources:告诉Maven解析所有依赖项及其源附件、并显示版本

17、tree:显示该项目的依赖关系树

18、unpack:与copy功能一直,但是会解压缩

19、unpack-dependencies:与copy-dependencies功能一致,只是会解压