记住,运行任何一个阶段的时候,它前面的所有阶段都会被运行,这也就是为什么我们运行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是一种选择,简单的结构如下:
<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>
但是要注意一点:
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不好用了!)
<goal>attached</goal>
只要使用下面的替换上面的配置就可以了。
<goal>single</goal>
下面的参考网址:
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
工具是为人服务,边用边学,切忌本末倒置。