maven打包插件詳解


maven-jar-plugin插件的使用及詳解

該插件的xml配置及詳解如下:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.4</version>
	<configuration>
		<archive>
			<!-- 生成的jar中,包含pom.xml和pom.properties這兩個文件 -->
			<addMavenDescriptor>true</addMavenDescriptor>
			<!-- 生成MANIFEST.MF的設置 -->
			<manifest>
				<!--這個屬性特別關鍵,如果沒有這個屬性,有時候我們引用的包maven庫 
				下面可能會有多個包,並且只有一個是正確的,其余的可能是帶時間戳的, 
				此時會在classpath下面把那個帶時間戳的給添加上去,
				然后我們在依賴打包的時候,打的是正確的,所以兩頭會對不上,報錯。 -->
				<useUniqueVersions>false</useUniqueVersions>
				<!-- 為依賴包添加路徑, 這些路徑會寫在MANIFEST文件的Class-Path下 -->
				<addClasspath>true</addClasspath>
				<!-- 這個jar所依賴的jar包添加classPath的時候的前綴,如果這個jar本身
				和依賴包在同一級目錄,則不需要添加 -->
				<classpathPrefix>lib/</classpathPrefix>
				<!-- jar啟動入口類 -->
				<mainClass>com.ht.pojo.Test</mainClass>
			</manifest>
			<manifestEntries>
				<!-- 在Class-Path下添加配置文件的路徑 -->
				<Class-Path>../config/</Class-Path>
				<!-- 假如這個項目可能要引入一些外部資源,但是你打包的時候並不想把
				這些資源文件打進包里面,這個時候你必須在這邊額外指定一些這些資源 
				文件的路徑,這個位置指定的話,要根據你預期的這些位置去設置,我這邊 
				所有jar都在lib下,資源文件都在config下,lib和config是同級的 
				同時還需要注意另外一個問題,假如你的pom文件里面配置了 
				<scope>system</scope>,就是你依賴是你本地的資源,這個時候使用 
				這個插件,classPath里面是不會添加,所以你得手動把這個依賴添加進 
				這個地方,用空格隔開就行 -->
			</manifestEntries>
		</archive>
		<!-- jar包的位置 -->
		<outputDirectory>${project.build.directory}/lib</outputDirectory>
		<includes>
			<!-- 打jar包時,打包class文件和config目錄下面的 properties文件 -->
			<!-- 有時候可能需要一些其他文件,這邊可以配置,包括剔除的文件等等 -->
			<include>**/*.class</include>
			<include>**/*.properties</include>
		</includes>
	</configuration>
</plugin>

使用clean package命令打包成功后,目錄結構如下所示:

Maven常用插件:maven-jar-plugin的使用及詳解-圖片-1

打開meventest-0.0.1-SNAPSHOT.jar文件,查看打包成功后的項目結構

Maven常用插件:maven-jar-plugin的使用及詳解-圖片-2

maven-dependency-plugin插件的使用及詳解

簡介

maven-dependency-plugin是處理與依賴相關的插件。它有很多可用的goal,大部分是和依賴構建、分析和解決相關的goal,這部分goal可以直接用maven的命令操作,例如:mvn dependency:tree、mvn dependency:analyze 。

但是我們最常用到的是 dependency:copy,dependency:copy-dependencies 及dependency:unpack ,dependency:unpack-dependencies 這四個。

使用

插件聲明:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-dependency-plugin</artifactId>  
    <version>2.8</version>  
</plugin>  

dependency:copy

將一系列在此插件內列出的artifacts ,將他們copy到一個特殊的地方,重命名或者去除其版本信息。這個可以解決遠程倉庫存在但是本地倉庫不存在的依賴問題,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>  

比如把junit拷到libs目錄下

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

img

dependency:unpack

unpack和copy類似,只不過它會把拷來的包解開,例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                        <version>1.7.7</version>
                    </artifactItem>
                    <artifactItem>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                        <outputDirectory>${project.build.directory}/libs</outputDirectory>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

執行mvn package打包命令之后,slf4j復制到lib目錄下,junit復制到libs目錄下

img

junit和slf4j-log4j12拷完以后,放到lib和libs下的不再是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.

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

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

例子

打包需求描述

1、導出單獨的項目jar包(精簡,不包含依賴jar)

2、項目依賴的所有jar包導出到lib目錄下

3、項目依賴oracle ojdbc8.jar,假設在maven倉庫中並不存在,需要一並導出並添加進MANIFEST.MF文件中的Class-Path。--也可以將ojdbc8安裝到本地maven倉庫后直接導出

工程目錄:

img

maven pom.xml

<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
    http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.xk.dp</groupId>
		<artifactId>thirdparty</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>weixin-api</artifactId>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<log4j.version>2.13.0</log4j.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.30</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.11</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.7.30</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.66</version>
		</dependency>
		<dependency> <!--添加本地jar包依賴-->
   			<groupId>oracle</groupId> 
   			<artifactId>ojdbc8</artifactId> 
   			<version>1.0</version> 
   			<scope>system</scope> 
   			<systemPath>${pom.basedir}/lib/ojdbc8.jar</systemPath> 
		</dependency>
	</dependencies>

	<build>
		<plugins>
		 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <useUniqueVersions>false</useUniqueVersions>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>cn.xk.dp.weixin.Driver</mainClass>
                        </manifest>
                        <manifestEntries>  <!--將ojdbc8-1.0.jar寫進MANIFEST.MF文件中的Class-Path-->
   				           <Class-Path>lib/ojdbc8-1.0.jar</Class-Path>
  			            </manifestEntries> 
                    </archive>
                    <excludes>   <!--排除用於測試的日志配置資源文件-->
                        <exclude>log4j2-test.xml</exclude>
                    </excludes>
                </configuration>
           </plugin>
		<plugin>  <!--在打包階段將依賴的jar包導出到lib目錄下-->
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-dependency-plugin</artifactId>
		  <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>
			</configuration>
		    </execution>
		  </executions>
	     </plugin>
      </plugins>
	</build>
</project>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM