Maven的生命週期和maven-assembly-plugin插件使用


記住,運行任何一個階段的時候,它前面的全部階段都會被運行,這也就是爲何咱們運行mvn install 的時候,代碼會被編譯,測試,打包。 Maven的插件機制是徹底依賴Maven的生命週期的。
Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute.

此外,每一個build階段能夠有0到多個目標與其綁定。若是一個build階段沒有目標與其綁定,那這個build階段不會執行。
Maven有一個概念:goal。每一個build階段可能包含一個或多個。



The first, and most common way, is to set the packaging for your project via the equally named POM element <packaging>. Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar. Each packaging contains a list of goals to bind to a particular phase. For example, the jar packaging will bind the following goals to build phases of the default lifecycle.
在packaging標籤中,你所選擇打成什麼樣的包,會自動將一些goal綁定到相應的build階段。默認是jar。



<modelVersion>4.0.0</modelVersion>
 <groupId>com.a.b</groupId>
 <artifactId>test</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>testI</name>
 <url>http://maven.apache.org</url>



要想將寫的程序和它自己所依賴的jar包一塊兒build到一個包裏,使用maven-assembly-plugin是一種選擇,簡單的結構以下: html


<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.a.b</groupId>
	<artifactId>test</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>testI</name>
	<url>http://maven.apache.org</url>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>attached</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.1</version>
		</dependency>
		</dependency>
	</dependencies>
</project>


可是要注意一點: spring

The main goal in the assembly plugin is the single goal. It is used to create all assemblies. All other goals are deprecated and will be removed in a future release.


官網說,下面的goal已通過時了(雖然deprecated,可是maven3.0.3仍是可使用的,只是可能之後更新maven的時候,這個插件不會更新了,這可能更新了maven,這個插件的這個goal很差用了!apache


<goal>attached</goal>


只要使用下面的替換上面的配置就能夠了。 maven

<goal>single</goal>


下面的參考網址: ide

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://juvenshun.iteye.com/blog/213959
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html 工具

若是想要獲得比assembly plugin 更多的控制,請使用Maven Shade Plugin
測試

工具是爲人服務,邊用邊學,切忌本末倒置。
相關文章
相關標籤/搜索