Maven打包插件—— 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-dependencieshtml

首先聲明插件:java

<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以下:web

<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配置。下面是一些經常使用項說明:spring

Name Type Since Descriptionapache

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沒有默認值,須要指定外,全部其餘的選項均可以被忽略:app

<configuration>  
    <artifactItems>  
        <artifactItem>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.11</version>  
        </artifactItem>  
    </artifactItems>  
</configuration>

以上配置會將junit包拷到target/dependency目錄下,文件名爲:junit-4.11.jar。less

若是想把它拷到lib目錄下,能夠以下配置:eclipse

<configuration>  
    <artifactItems>  
        <artifactItem>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.11</version>  
        </artifactItem>  
    </artifactItems>  
    <outputDirectory>lib</outputDirectory>  
</configuration>

或者:maven

<configuration>  
        <artifactItems>  
            <artifactItem>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.11</version>  
                <outputDirectory>lib</outputDirectory>  
            </artifactItem>  
        </artifactItems>  
    </configuration>

根據上面的說明,artifactItem裏能夠有如下幾個參數:ide

  • 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:
  • 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.

例如當前工程有如下依賴:

<pluginManagement>
	<plugins>
		<plugin>
			<groupId>org.eclipse.m2e</groupId>
			<artifactId>lifecycle-mapping</artifactId>
			<version>1.0.0</version>
			<configuration>
				<lifecycleMappingMetadata>
					<pluginExecutions>
						<pluginExecution>
							<pluginExecutionFilter>
								<groupId>org.apache.maven.plugins</groupId>
								<artifactId>maven-dependency-plugin</artifactId>
								<versionRange>[2.0,)</versionRange>
								<goals>
									<goal>copy-dependencies</goal>
								</goals>
							</pluginExecutionFilter>
							<action>
								<ignore />
							</action>
						</pluginExecution>
					</pluginExecutions>
				</lifecycleMappingMetadata>
			</configuration>
		</plugin>
		
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>2.6</version>
			<executions>
				<execution>
					<id>copy-resources</id>
					<phase>package</phase>
					<goals>
						<goal>copy-resources</goal>
					</goals>
					<configuration>
						<outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>
						<resources>
							<resource>
								<directory>${basedir}/src/main/resources</directory>
							</resource>
						</resources>
					</configuration>
				</execution>
			</executions>
		</plugin>
        <!-- 針對sprong boot web項目打包 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<excludes>
					<exclude>conf/**</exclude>
					<exclude>templates/**</exclude>
					<exclude>static/**</exclude>
				</excludes>
				<argLine>-Dfile.encoding=UTF-8</argLine>
			</configuration>
			<executions>
                <execution>
                        <id>copy-current-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>${project.version}</classifier>
                            <jarName>${project.artifactId}</jarName>
                            <outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory>
                        </configuration>
                </execution>
            </executions>
		</plugin>
		
		<plugin>
			<artifactId>maven-source-plugin</artifactId>
			<version>2.1</version>
			<executions>
				<execution>
					<phase>compile</phase>
					<goals>
						<goal>jar</goal>
					</goals>
					<configuration>
						<excludes>
							<exclude>conf/**</exclude>
							<exclude>templates/**</exclude>
							<exclude>static/**</exclude>
						</excludes>
					</configuration>
				</execution>
			</executions>
		</plugin>
       <!-- 複製依賴包 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<version>2.8</version>
			<executions>
				<execution>
					<id>copy</id>
					<phase>package</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
					<outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory>
						<includeScope>compile</includeScope>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</pluginManagement>

要排除全部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>

 

 

 

 

maven-dependency-plugin

<execution>:執行單元

id:標識

phase:在哪一個階段執行 如 package

goals-goal:執行的操做:copy、copy-dependencies、unpack、unpack-dependencies 四種

<configuration>:執行單元內的配置

type: 類型 jar

includeTypes: jar

outputDirectiry:存放的路徑 project.build.directory標識 target目錄

excludeTransitive:是否排除間接依賴 默認false 不排除

stripVersion:是否消除依賴jar包後綴的版本信息 默認是false 不取消版本信息

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <type>jar</type>
                <includeTypes>jar</includeTypes>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
                <!-- 是否排除間接依賴 默認false 不排除 -->
                <excludeTransitive>false</excludeTransitive>
                <!-- 是否消除依賴jar包後綴的版本信息 默認是false 不取消版本信息-->
                <stripVersion>false</stripVersion>
            </configuration>
        </execution>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>
                    ${project.build.directory}/lib/libs
                </outputDirectory>
            </configuration>
        </execution>
        <execution>
            <id>unpack</id>
            <phase>package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>
                    ${project.build.directory}/lib/libs
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

若是:goals-goal:爲 copy 或者 unpack 的話 configuration中能夠加多個artifactItem 信息 進行打包

copy : 拷貝指定jar包 到指定目錄

copy-dependencies : 拷貝依賴jar包 到指定目錄

unpack : 解壓指定jar包到指定目錄

unpack-dependencies : 解壓依賴jar包到指定目錄

相關文章
相關標籤/搜索