MAVEN的生命週期,jetty插件

MAVEN的生命週期和插件

    maven是經過插件來實現功能的。所謂的生命週期就是咱們在構建項目時,maven默認須要是想的一些功能,而每個功能就經過插件的某一功能來實現。html

    每一個插件會有一個或多個功能,每一個功能會實現一個目標(例如:生成test,compile,jar,rar,war)。java

1.maven的插件

    先介紹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>。

2.maven的生命週期

    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

3.jetty插件的使用

  第三方插件的使用,就是在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
相關文章
相關標籤/搜索