maven-dependency-plugin是處理與依賴相關的插件。它有不少可用的goal,大部分是和依賴構建、分析和解決相關的goal,這部分goal能夠直接用maven的命令操做,例如:mvn dependency:tree、mvn dependency:analyze;這類操做在平時的maven應用中不多會用到。這裏主要介紹除此以外的、用得最多的幾個操做:copy, copy-dependencies和它們對應的unpack, unpack-dependencieshtml
首先聲明插件:web
Xml代碼 spring
<build> apache
<plugins> less
<plugin> maven
<groupId>org.apache.maven.plugins</groupId> ide
<artifactId>maven-dependency-plugin</artifactId> ui
<version>2.8</version> spa
</plugin> 插件
</plugins>
</build>
copy操做能夠用來將某個(些)maven artifact(s)拷貝到某個目錄下。添加phase和goal以下:
Xml代碼
<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. |
這裏除了artifactItems沒有默認值,須要指定外,全部其餘的選項均可以被忽略:
Xml代碼
<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代碼
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</artifactItem>
</artifactItems>
<outputDirectory>lib</outputDirectory>
</configuration>
或者:
Xml代碼
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<outputDirectory>lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
根據上面的說明,artifactItem裏能夠有如下幾個參數:
一樣的參數,artifactItem裏的優先級更高,例如:
Xml代碼
<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代碼
<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 和 unpack操做是由要拷某個包,這個包須要具體指定要拷哪一個包,與當前工程的依賴沒有關係。copy-dependencies和它有點相似,可是它是用來拷當前工程的依賴包的,典型的,例如咱們有一個web應用,當打成war包的時候,它全部的依賴也須要被打到應用中。
copy-dependencies的參數有不少,詳細的能夠參考:copy-dependencies Doc,可是幾乎全部都有默認值。因此一個最簡單的定義以下:
Xml代碼
<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. |
例如當前工程有如下依賴:
Xml代碼
<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代碼
<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代碼
<configuration>
<excludeGroupIds>org.apache.camel</excludeGroupIds>
</configuration>
要排除除camel-spring外的全部其餘依賴以下:
Xml代碼
<configuration>
<includeArtifactIds>camel-spring</includeArtifactIds>
</configuration>