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

