maven是经过插件来实现功能的。所谓的生命周期就是咱们在构建项目时,maven默认须要是想的一些功能,而每个功能就经过插件的某一功能来实现。html
每一个插件会有一个或多个功能,每一个功能会实现一个目标(例如:生成test,compile,jar,rar,war)。java
先介绍maven的插件,理解了插件的做用,而后了解maven默认生命周期。就能很快掌握maven插件的使用。web
插件的使用命令。goal:插件目标,就是能够作成哪些事,例如:检验,打包,测试等等apache
mvn pluginname:goal
从一个maven官网提供的source插件介绍。这个插件能将源码打包,能够对不一样的源码进行打包。从下面图中,咱们能够知道有不少goalapi
而后咱们在pom.xml中配置以下,这个插件默认的phase是package,先不配置phase。tomcat
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </build>
使用命令:app
mvn clean install
查看输出,插件并无运行。直接使用插件eclipse
mvn source:jar
直接就生成了一个包文件,可是只运行了插件,没有运行其它的编译,测试功能。 修改下插件的phrase和goal,webapp
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.0</version> <configuration> <outputDirectory>/absolute/path/to/the/output/directory</outputDirectory> <finalName>filename-of-generated-jar-file</finalName> <attach>false</attach> </configuration> <executions> <execution> <id>source-jar</id> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> <execution> <id>test-source-jar</id> <phase>test-compile</phase> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
执行maven的默认命令jsp
mvn package
从打印日志能够看出,在compiler和test-compiler后,使用了source插件。
从这里能够看出来,若是没有绑定和插件是不会插入到maven默认的生命周期的。
总结:一个插件有多个功能(goal),插件实现一个功能(goal)就是一个生命周期。要在MAVEN默认的生命周期中使用插件,就须要使用配置<phase>和<goal>。不然,就只能经过插件命令source:jar,可是这个命令只会执行source:jar的功能。在咱们使用插件的时候,为了将其插入到maven的默认生命周期中,都须要设置<phase>和<goal>。
maven有3个大的生命周期:clean,default,site。咱们平时用的是clean,default周期。这么多生命周期,就是经过多个插件的多个功能实现的。下面整理的表,记录了这些生命周期名,做用,对应的插件,以对应的插件命令(和默认命令分清)。
clean的生命周期:
生命周期 | 做用 | 插件 | 命令 |
pre-clean | 执行清理前的工做 | maven-clean-plugin | clean:clean |
clean | 清理上一次构建生成的全部文件 | maven-clean-plugin | clean:clean |
post-clean | 执行清理后的工做 | maven-clean-plugin | clean:clean |
site的生命周期:
生命周期 | 做用 | 插件 | 命令 |
pre-site | 生成前的工做 | mave-site-plugin | site:site |
site | 生成项目的站点文件 | mave-site-plugin | site:site |
post-site | 生成后的工做 | maven-site-plugin | site:site |
site-deploy | 发布生成的站点文档 | maven-site-plugin | site-deploy |
default的生命周期:
生命周期 | 做用 | 插件 | 命令 |
validate | 检测工程信息 | ||
initialize | 初始化构建信息 | ||
generate-sources | 在target中生成源码 | ||
process-sourcess | 操做源码,过滤掉${}这些信息 | ||
generate-resources | 生成资源文件 | maven-plugin-plugin | plugin:descriptor |
process-resources | 复制资源文件和源码到目标文件夹 | maven-resources-plugin | resources:resources |
compile | 编译源码 | maven-compiler-plugin | compiler:compile |
process-classes | 编译后的文件处理 | ||
generate-test-sources | 生成测试的源码文件 | ||
process-test-sources | 处理源码文件 | ||
generate-test-resources | 生成测试的资源文件 | ||
process-test-resources | 处理测试的资源文件 | maven-resources-plugin | resources:testresources |
test-compile | 编译测试 | maven-compiler-plugin | compiler:testCompile |
process-compile | 处理测试文件 | ||
test | 测试 | maven-surefire-plugin | surefire:test |
prepare-package | 打包前的准备工做 | ||
package | 打包 | 不一样的打包有不一样的文件 | |
pre-integration-test | 集成测试前的准备 | ||
integration-test | 集成测试 | ||
post-integration-test | 继承测试的后续处理 | ||
verify | 运行检测 | maven-verifier-plugin | verifier:verify |
install | 生成到本地库 | maven-install-plugin | install:install |
deploy | 发布到远程库 | maven-deploy-plugin | deploy:deploy |
第三方插件的使用,就是在maven默认的生命周期中插入一个生命周期(插件实现一个目标)。下面使用jetty插件实现一下
a、使用:
在pom中设置:
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <properties> <test.name>test</test.name> <test.password>test</test.password> </properties> </profile> <profile> <id>dev</id> <properties> <test.name>release</test.name> <test.password>release</test.password> </properties> </profile> </profiles> <build> <finalName>test-web</finalName> <resources> <resource> <filtering>true</filtering> <directory>${basedir}/src/main/resources</directory> <includes> <include>*.properties</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.8.v20160314</version> <configuration> <scanIntervalSeconds>3</scanIntervalSeconds> <stopKey>foo</stopKey> <stopPort>9999</stopPort> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8000</port> </connector> </connectors> </configuration> </plugin> </plugins> </build>
在src/main/resources中建立db.properties文件,内容以下
test.name=${test.name} test.password=${test.password}
建立一个servlet
public class ThredServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 786750866880663272L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Properties pro = new Properties(); pro.load(this.getClass().getResourceAsStream("/db.properties")); req.setAttribute("test.password", pro.get("test.password")); req.setAttribute("test.name", pro.getProperty("test.name")); PrintWriter out = resp.getWriter(); out.write("test.password:--" + pro.getProperty("test.password") + "\n\t"); out.write("test.name:--" + pro.getProperty("test.name")); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
而后执行:
mvn jetty:run -P test
输出
接着执行
mvn jetty:run -P dev
输出
这样,经过<profile>能够很方便的在设置的环境中作开发,不用每次都去配置,节约时间。也不须要发布到tomcat中,每次都须要重启tomcat,至关浪费事件。
b、jetty的配置信息
从log中咱们,发现jetty会先编译,测试,而后在运行。从下图能够看出jetty运行时加载的数据:class(class文件和property文件),web.xml,webapp(jsp,html)等信息
jetty默认的class(class文件和property文件),web.xml,webapp(jsp,html)加载地址
resources in ${project.basedir}/src/main/webapp classes in ${project.build.outputDirectory} web.xml in ${project.basedir}/src/main/webapp/WEB-INF/
能够直接设置
<webApp> <!-- context地址:就是模块的地址 --> <contextPath>/</contextPath> <!-- webapp地址 --> <baseResource>src/main/webapp</baseResource> <!-- web.xml地址 --> <descriptor>${project.basedir}/src/over/webapp/web.xml</descriptor> </webApp>
c、jetty的war包执行
上面配置仍是很麻烦,jetty提供了一个简单的配置。能够直接加载运行war包,也就是jetty会先编译,测试,打包,运行。不准要指定其它的文件信息,直接运行war包
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.8.v20160314</version> <configuration> <war>${project.basedir}/target/mycustom.war</war> </configuration> </plugin>
执行命令
mvn jetty:run-war -P test