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

工具是为人服务,边用边学,切忌本末倒置。
相关文章
相关标签/搜索