深入理解maven及應(yīng)用_第1頁
深入理解maven及應(yīng)用_第2頁
深入理解maven及應(yīng)用_第3頁
深入理解maven及應(yīng)用_第4頁
深入理解maven及應(yīng)用_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1、三套生命周期 對于maven的生命周期來說,共有三個(gè)相互獨(dú)立的生命周期,分別是clean、default、site。clean生命周期目的是清理項(xiàng)目,default生命周期目的是構(gòu)建項(xiàng)目,而site生命周期目的是建立項(xiàng)目站點(diǎn)。 每個(gè)生命周期分別包含一些階段,這些階段是有順序的,并且后面的階段依賴于前面的階段。如clean生命周期包含pre-clean、clean和post-clean三個(gè)階段,如果執(zhí)行clean階段,則會(huì)先執(zhí)行pre-clean階段。 較之于生命周期階段有前后依賴關(guān)系,三套生命周期本身是相互獨(dú)立的,用戶可以僅調(diào)用clean生命周期的某個(gè)階段,也可以不執(zhí)行clean周期,而直

2、接執(zhí)行default生命周期的某幾個(gè)階段。2、clean生命周期 clean生命周期包含三個(gè)階段,主要負(fù)責(zé)清理項(xiàng)目,如下:pre-cleanexecutes processes needed prior to the actual project cleaningcleanremove all files generated by the previous buildpost-cleanexecutes processes needed to finalize the project cleaning3、default生命周期 default生命周期定義了真正構(gòu)建時(shí)所需要執(zhí)行的所有步驟,包含的

3、階段如下:validatevalidate the project is correct and all necessary information is available.initializeinitialize build state, e.g. set properties or create directories.generate-sourcesgenerate any source code for inclusion in cess-sourcesprocess the source code, for example to filter any

4、values.generate-resourcesgenerate resources for inclusion in the cess-resourcescopy and process the resources into the destination directory, ready for pilecompile the source code of the cess-classespost-process the generated files from compilation, for example to

5、do bytecode enhancement on Java classes.generate-test-sourcesgenerate any test source code for inclusion in cess-test-sourcesprocess the test source code, for example to filter any values.generate-test-resourcescreate resources for cess-test-resourcescopy and process the re

6、sources into the test destination directory.test-compilecompile the test source code into the test destination directoryprocess-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.testrun tests using a

7、suitable unit testing framework. These tests should not require the code be packaged or deployed.prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)packagetake the

8、 compiled code and package it in its distributable format, such as a JAR.pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required egration-testprocess and deploy the package if necessary into an environ

9、ment where integration tests can be run.post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.verifyrun any checks to verify the package is valid and meets quality criteria.installinstall the package into the local re

10、pository, for use as a dependency in other projects locally.deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.4、site生命周期 siet生命周期的目的是建立和發(fā)布項(xiàng)目站點(diǎn),maven能夠基于POM所包含的信息,自動(dòng)生成一個(gè)友好的站點(diǎn),方便團(tuán)隊(duì)交流和發(fā)布項(xiàng)目信息,包含的階段如下:pre-

11、siteexecutes processes needed prior to the actual project site generationsitegenerates the projects site documentationpost-siteexecutes processes needed to finalize the site generation, and to prepare for site deploymentsite-deploydeploys the generated site documentation to the specified web server5

12、、命令行與生命周期從命令行執(zhí)行maven任務(wù)的最主要方式就是調(diào)用maven的生命周期階段。需要注意的是,各個(gè)生命周期是相互獨(dú)立的,而一個(gè)生命周期的階段是有前后依賴關(guān)系的。例子如下: 1、$mvn clean :該命令調(diào)用clean生命周期的clean階段。實(shí)際執(zhí)行的階段為clean生命周期的pre-clean和clean階段。 2、$mvn test:該命令調(diào)用default生命周期的test階段。實(shí)際調(diào)用的是default生命周期的validate、initialize等,直到test的所有階段。 3、$mvn clean install:該命令調(diào)換用clean生命周期的clean階段和de

13、fault生命周期的instal階段。6、插件目標(biāo) maven的核心僅僅定義了抽象的生命周期,具體的任務(wù)是交由插件完成的,插件以獨(dú)立的形式存在。 對于插件本身,為了能夠復(fù)用代碼,它往往能夠完成多個(gè)任務(wù)。如maven-dependency-plugin有十多個(gè)目標(biāo),每個(gè)目標(biāo)對應(yīng)了一個(gè)功能,如 dependency:analyze、 dependency:tree和dependency:list。這是一種通用的寫法,冒號前面是插件前綴,后面是該插件的目標(biāo)。7、插件綁定maven的生命周期與插件相互綁定,用以完成實(shí)際的構(gòu)建任務(wù)。具體而言,是生命周期的階段與插件的目標(biāo)相互綁定,已完成某個(gè)具體的構(gòu)建任務(wù)

14、。例如項(xiàng)目編譯這一任務(wù),它對應(yīng)了default生命周期的compile階段,而maven-compiler-plugin這一插件的compile目標(biāo)能夠完成該任務(wù),因此將他們綁定。7.1內(nèi)置綁定maven在核心為一些主要的生命周期接到綁定了很多插件的目標(biāo),如下: clean和site生命周期相對簡單。cleanclean:cleansitesite:sitesite-deploysite:deploy default生命周期與插件目標(biāo)的綁定關(guān)系有點(diǎn)復(fù)雜一些。這是因?yàn)閷τ谌魏雾?xiàng)目來說,例如jar項(xiàng)目和war項(xiàng)目,他們的項(xiàng)目清理和站點(diǎn)生成任務(wù)是一樣的,不過構(gòu)建過程會(huì)有區(qū)別。例如jar項(xiàng)目需要打成j

15、ar包,而war項(xiàng)目需要打成war包。由于項(xiàng)目的打包類型會(huì)影響構(gòu)建的具體過程,因此,default生命周期的階段與插件目標(biāo)的綁定關(guān)系有項(xiàng)目打包類型所決定的,打包類型是通過pom中的packaging元素定義的。最常見的打包類型是jar,它也是默認(rèn)的打包類型?;谠摯虬愋?,default生命周期的內(nèi)置綁定關(guān)系如下:process-resourcesresources:resourcescompilecompiler:compileprocess-test-resourcesresources:testResourcestest-compilecompiler:testCompiletestsu

16、refire:testpackageejb:ejborejb3:ejb3orjar:jarorpar:parorrar:rarorwar:warinstallinstall:installdeploydeploy:deploy7、2自定義綁定 除了內(nèi)置綁定以為,用戶還能夠自己選擇獎(jiǎng)某個(gè)插件目標(biāo)綁定到生命周期的某個(gè)階段以執(zhí)行更多更特色的任務(wù)。htmlview plaincopyprint? 1. 2. 3. org.apache.maven.plugins4. maven-resources-plugin5. 2.66. 7. true8. GBK9. 10. exe11. zip12. vbs

17、13. sh14. 15. 16. 17. 18. copy-resources19. validate20. 21. copy-resources22. 23. 24. true25. $project.build.directory26. 27. agentmanager.jsmooth28. assembly.xml29. 30. 31. 32. src/main/resources/33. true34. 35. 36. 37. 38. 39. 如上圖定義了一個(gè)id為copy-resources的任務(wù),綁定到default生命周期的validate階段,綁定的插件為maven-reso

18、urces-plugin,插件目標(biāo)為copy-resources。即用插件的copy-resources功能來實(shí)現(xiàn)項(xiàng)目資源文件的拷貝。htmlview plaincopyprint? 1. 2. 3. org.apache.maven.plugins4. maven-dependency-plugin5. 2.16. 7. 8. copy9. install10. 11. copy-dependencies12. 13. 14. lib15. 16. 17. 18. 同上,定義了一個(gè)id為copy的任務(wù),利用插件maven-dependency-plugin的copy-dependencies

19、目標(biāo)綁定到default生命周期的install階段,來實(shí)現(xiàn)項(xiàng)目依賴的jar包的自動(dòng)復(fù)制。當(dāng)插件目標(biāo)被綁定到不同的生命周期階段時(shí)候,其執(zhí)行順序會(huì)有生命周期階段的先后順序決定的。如果多個(gè)目標(biāo)被綁定到同一個(gè)階段,他們的執(zhí)行順序是由插件聲明的先后順序決定目標(biāo)的執(zhí)行順序。8、插件配置用戶可以配置插件目標(biāo)的參數(shù),進(jìn)一步調(diào)整插件目標(biāo)所執(zhí)行的任務(wù)。8、1命令行插件配置如 $mvn install -Dmaven.test.skip=true 的意義即跳過測試步驟。 參數(shù)-D的java自帶的,其功能是通過命令行設(shè)置一個(gè)java系統(tǒng)屬性,maven簡單地重用了該參數(shù)以實(shí)現(xiàn)插件參數(shù)的配置。8、2pom中插件全局配

20、置如項(xiàng)目編譯使用1.6版本的源文件,生成與JVM1.6兼容的字節(jié)碼文件,如下:htmlview plaincopyprint? 1. 2. org.apache.maven.plugins3. maven-compiler-plugin4. 2.3.25. 6. 1.67. 1.68. 9. 9、獲取插件描述信息 $mvn help:describe-Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1 來獲取插件的詳細(xì)信息 可以簡化為: $mvn help:describe-Dplugin=compiler 如果僅僅描述插件目標(biāo)

21、的信息,可以加上goal參數(shù): $mvn help:describe-Dplugin=compiler-Dgoal=compile 如果想輸出更詳細(xì)的信息,可以加上detail參數(shù): $mvn help:describe-Dplugin=compiler-Ddetail(二):靈活的構(gòu)建原文地址:參考官方url:/guides/index.html一個(gè)優(yōu)秀的構(gòu)建系統(tǒng)必須足夠靈活,應(yīng)該能夠讓項(xiàng)目在不同的環(huán)境下都能成功構(gòu)建。maven為了支持構(gòu)建的靈活性,內(nèi)置了三大特性,即:屬性、profile和資源過濾。1、maven屬性maven屬性分6類: 1、內(nèi)

22、置屬性:如$basedir表示項(xiàng)目根目錄,$version表示項(xiàng)目版本 2、POM屬性:用戶可以引用pom文件中對應(yīng)的值。如: $basedir 項(xiàng)目根目錄 $project.build.directory 構(gòu)建目錄,缺省為target$project.build.outputDirectory 構(gòu)建過程輸出目錄,缺省為target/classes$project.build.finalName 產(chǎn)出物名稱,缺省為$project.artifactId-$project.version$project.packaging 打包類型,缺省為jar$project.xxx 當(dāng)前pom文件的任意節(jié)點(diǎn)

23、的內(nèi)容 3、自定義屬性:用戶可以在pom的元素下自定義maven屬性。 4、setting屬性:用戶可以使用以settings開頭的屬性引用settings.xml中xml元素的值,如$settings.localRepository指向用戶本地倉庫的地址。 5、java系統(tǒng)屬性:maven可以使用當(dāng)前java系統(tǒng)的屬性,如$user.home指向了用戶目錄。 6、環(huán)境變量屬性:所有環(huán)境變量都可以使用以env.開頭的屬性。如:$env.JAVA_HOE。2、資源過濾 這里所謂的資源:也就就是指src/main/resources和src/test/resources文件下的所有文件,默認(rèn)情況下

24、,這些文件會(huì)被復(fù)制到classpath下面,即target/classes下面。 所謂資源過濾,就是過濾這些文件夾下面的文件里面的內(nèi)容,看里面的maven變量是否需要替換。默認(rèn)情況下,只有pom.xml里面的變量才會(huì)被替換,資源文件是不會(huì)被過濾的,但是可以設(shè)置,如下:htmlview plaincopyprint? 1. 2. agentmanager3. src/main/java4. 5. 6. 7. src/main/resources8. 9. */jre.zip10. */jre.tar11. agentmanager.jsmooth12. assembly.xml13. 14. $

25、project.build.directory15. 16. 17. src/main/resources/conf18. $basedir/conf19. true20. 21. 22. 如pertiescssview plaincopyprint? 1. jdbc.driverClassName=$db.driver2. jdbc.url=$db.url3. jdbc.username=$db.user4. jdbc.password=$db.pwdprofile文件htmlview plaincopyprint? 1. 2. 3. dev4. 5. oracle.jdbc.driver.OracleDriver6. jdbc:oracle:thin::1521:dbname7. username8. userpwd9.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論