日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入理解maven及应用--转

發布時間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入理解maven及应用--转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(一):生命周期和插件

原文地址:http://blog.csdn.net/chaofanwei/article/details/36197183

在項目里用了快一年的maven了,最近突然發現maven項目在eclipse中build時非常慢,因為經常用clean install命令來build項目,也沒有管那么多,但最近實在受不了烏龜一樣的build速度,于是下定決心再看看《maven實戰》吧,
??????? 對于我來說,maven最主要的作用有兩個方面,一個是對jar包的依賴解決功能,自己管理jar包,另一個功能就是項目的構建,打包部署。現在我覺得最重要的還是maven的生命周期和插件機制,下面就來總結一下吧。

??? 參考url:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html


1、三套生命周期


???? 對于maven的生命周期來說,共有三個相互獨立的生命周期,分別是clean、default、site。clean生命周期目的是清理項目,default生命周期目的是構建項目,而site生命周期目的是建立項目站點。
???? 每個生命周期分別包含一些階段,這些階段是有順序的,并且后面的階段依賴于前面的階段。如clean生命周期包含pre-clean、clean和post-clean三個階段,如果執行clean階段,則會先執行pre-clean階段。
???? 較之于生命周期階段有前后依賴關系,三套生命周期本身是相互獨立的,用戶可以僅調用clean生命周期的某個階段,也可以不執行clean周期,而直接執行default生命周期的某幾個階段。


2、clean生命周期


???? clean生命周期包含三個階段,主要負責清理項目,如下:

?

pre-cleanexecutes processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecutes processes needed to finalize the project cleaning

?


3、default生命周期


??? default生命周期定義了真正構建時所需要執行的所有步驟,包含的階段如下:

?

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 compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compilecompile the source code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-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 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 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 environment.
integration-testprocess and deploy the package if necessary into an environment 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 repository, 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生命周期的目的是建立和發布項目站點,maven能夠基于POM所包含的信息,自動生成一個友好的站點,方便團隊交流和發布項目信息,包含的階段如下:

????

pre-siteexecutes processes needed prior to the actual project site generation
sitegenerates the project's site documentation
post-siteexecutes processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploys the generated site documentation to the specified web server

?

5、命令行與生命周期

?????從命令行執行maven任務的最主要方式就是調用maven的生命周期階段。需要注意的是,各個生命周期是相互獨立的,而一個生命周期的階段是有前后依賴關系的。例子如下:
???? 1、$mvn clean :該命令調用clean生命周期的clean階段。實際執行的階段為clean生命周期的pre-clean和clean階段。
???? 2、$mvn test:該命令調用default生命周期的test階段。實際調用的是default生命周期的validate、initialize等,直到test的所有階段。
???? 3、$mvn clean install:該命令調換用clean生命周期的clean階段和default生命周期的instal階段。

6、插件目標

????? maven的核心僅僅定義了抽象的生命周期,具體的任務是交由插件完成的,插件以獨立的形式存在。
???? 對于插件本身,為了能夠復用代碼,它往往能夠完成多個任務。如maven-dependency-plugin有十多個目標,每個目標對應了一個功能,如 dependency:analyze、 dependency:tree和dependency:list。這是一種通用的寫法,冒號前面是插件前綴,后面是該插件的目標。

7、插件綁定

?maven的生命周期與插件相互綁定,用以完成實際的構建任務。具體而言,是生命周期的階段與插件的目標相互綁定,已完成某個具體的構建任務。例如項目編譯這一任務,它對應了default生命周期的compile階段,而maven-compiler-plugin這一插件的compile目標能夠完成該任務,因此將他們綁定。

7.1內置綁定

?maven在核心為一些主要的生命周期接到綁定了很多插件的目標,如下:
???? clean和site生命周期相對簡單。

?

cleanclean:clean

?

?

sitesite:site
site-deploysite:deploy

?


???? default生命周期與插件目標的綁定關系有點復雜一些。這是因為對于任何項目來說,例如jar項目和war項目,他們的項目清理和站點生成任務是一樣的,不過構建過程會有區別。例如jar項目需要打成jar包,而war項目需要打成war包。
?????由于項目的打包類型會影響構建的具體過程,因此,default生命周期的階段與插件目標的綁定關系有項目打包類型所決定的,打包類型是通過pom中的packaging元素定義的。最常見的打包類型是jar,它也是默認的打包類型。基于該打包類型,default生命周期的內置綁定關系如下:

?

process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packageejb:ejb?or?ejb3:ejb3?or?jar:jar?or?par:par?or?rar:rar?or?war:war
installinstall:install
deploydeploy:deploy

?

?

7、2自定義綁定

???? 除了內置綁定以為,用戶還能夠自己選擇獎某個插件目標綁定到生命周期的某個階段以執行更多更特色的任務。

[html]?view plaincopyprint?
  • <!--?自動復制資源文件件到根目錄?-->??
  • ????????????<plugin>??
  • ????????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????????<artifactId>maven-resources-plugin</artifactId>??
  • ????????????????<version>2.6</version>??
  • ????????????????<configuration>??
  • ????????????????????<includeEmptyDirs>true</includeEmptyDirs>??
  • ????????????????????<encoding>GBK</encoding>??
  • ????????????????????<nonFilteredFileExtensions>??
  • ????????????????????????<nonFilteredFileExtension>exe</nonFilteredFileExtension>??
  • ????????????????????????<nonFilteredFileExtension>zip</nonFilteredFileExtension>??
  • ????????????????????????<nonFilteredFileExtension>vbs</nonFilteredFileExtension>??
  • ????????????????????????<nonFilteredFileExtension>sh</nonFilteredFileExtension>??
  • ????????????????????</nonFilteredFileExtensions>??
  • ????????????????</configuration>??
  • ????????????????<executions>??
  • ????????????????????<execution>??
  • ????????????????????????<id>copy-resources</id>??
  • ????????????????????????<phase>validate</phase>??
  • ????????????????????????<goals>??
  • ????????????????????????????<goal>copy-resources</goal>??
  • ????????????????????????</goals>??
  • ????????????????????????<configuration>??
  • ????????????????????????????<includeEmptyDirs>true</includeEmptyDirs>??
  • ????????????????????????????<outputDirectory>${project.build.directory}</outputDirectory>??
  • ????????????????????????????<excludes>??
  • ????????????????????????????????<exclude>agentmanager.jsmooth</exclude>??
  • ????????????????????????????????<exclude>assembly.xml</exclude>??
  • ????????????????????????????</excludes>??
  • ????????????????????????????<resources>??
  • ????????????????????????????????<resource>??
  • ????????????????????????????????????<directory>src/main/resources/</directory>??
  • ????????????????????????????????????<filtering>true</filtering>??
  • ????????????????????????????????</resource>??
  • ????????????????????????????</resources>??
  • ????????????????????????</configuration>??
  • ????????????????????</execution>??
  • ????????????????</executions>??
  • ????????????</plugin>??

  • ???? 如上圖定義了一個id為copy-resources的任務,綁定到default生命周期的validate階段,綁定的插件為maven-resources-plugin,插件目標為copy-resources。即用插件的copy-resources功能來實現項目資源文件的拷貝。

    [html]?view plaincopyprint?
  • <!--?自動復制maven依賴包到lib目錄?-->??
  • ????????????<plugin>??
  • ????????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????????<artifactId>maven-dependency-plugin</artifactId>??
  • ????????????????<version>2.1</version>??
  • ????????????????<executions>??
  • ????????????????????<execution>??
  • ????????????????????????<id>copy</id>??
  • ????????????????????????<phase>install</phase>??
  • ????????????????????????<goals>??
  • ????????????????????????????<goal>copy-dependencies</goal>??
  • ????????????????????????</goals>??
  • ????????????????????????<configuration>??
  • ????????????????????????????<outputDirectory>lib</outputDirectory>??
  • ????????????????????????</configuration>??
  • ????????????????????</execution>??
  • ????????????????</executions>??
  • ????????????</plugin>??

  • 同上,定義了一個id為copy的任務,利用插件maven-dependency-plugin的copy-dependencies目標綁定到default生命周期的install階段,來實現項目依賴的jar包的自動復制。

    ?????當插件目標被綁定到不同的生命周期階段時候,其執行順序會有生命周期階段的先后順序決定的。如果多個目標被綁定到同一個階段,他們的執行順序是由插件聲明的先后順序決定目標的執行順序

    8、插件配置


    ?用戶可以配置插件目標的參數,進一步調整插件目標所執行的任務。

    8、1命令行插件配置

    ?如 $mvn install -Dmaven.test.skip=true 的意義即跳過測試步驟。
    ????? 參數-D的java自帶的,其功能是通過命令行設置一個java系統屬性,maven簡單地重用了該參數以實現插件參數的配置。

    8、2pom中插件全局配置

    如項目編譯使用1.6版本的源文件,生成與JVM1.6兼容的字節碼文件,如下:

    [html]?view plaincopyprint?
  • <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>??
  • ????????????????</configuration>??
  • ????????????</plugin>??

  • ?

    9、獲取插件描述信息

    ??? $mvn help:describe-Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1? 來獲取插件的詳細信息
    ??? 可以簡化為:
    ??? $mvn help:describe-Dplugin=compiler
    ??? 如果僅僅描述插件目標的信息,可以加上goal參數:
    ??? $mvn help:describe-Dplugin=compiler-Dgoal=compile
    ??? 如果想輸出更詳細的信息,可以加上detail參數:
    ??? $mvn help:describe-Dplugin=compiler-Ddetail

    (二):靈活的構建

    原文地址:http://blog.csdn.net/chaofanwei/article/details/36652373

    參考官方url:http://maven.apache.org/guides/index.html

    ?一個優秀的構建系統必須足夠靈活,應該能夠讓項目在不同的環境下都能成功構建。maven為了支持構建的靈活性,內置了三大特性,即:屬性、profile和資源過濾

    1、maven屬性

    ?maven屬性分6類:
    ??? 1、內置屬性:如${basedir}表示項目根目錄,${version}表示項目版本
    ??? 2、POM屬性:用戶可以引用pom文件中對應的值。如:
    ??????? ?${basedir} 項目根目錄
    ??????? ?${project.build.directory} 構建目錄,缺省為target
    ?????????${project.build.outputDirectory} 構建過程輸出目錄,缺省為target/classes
    ?????????${project.build.finalName} 產出物名稱,缺省為${project.artifactId}-${project.version}
    ?????????${project.packaging} 打包類型,缺省為jar
    ?????????${project.xxx} 當前pom文件的任意節點的內容?
    ??? 3、自定義屬性:用戶可以在pom的<properties>元素下自定義maven屬性。
    ??? 4、setting屬性:用戶可以使用以settings開頭的屬性引用settings.xml中xml元素的值,如${settings.localRepository}指向用戶本地倉庫的地址。
    ??? 5、java系統屬性:maven可以使用當前java系統的屬性,如${user.home}指向了用戶目錄。
    ??? 6、環境變量屬性:所有環境變量都可以使用以env.開頭的屬性。如:${env.JAVA_HOE}。

    2、資源過濾

    ???? 這里所謂的資源:也就就是指src/main/resources和src/test/resources文件下的所有文件,默認情況下,這些文件會被復制到classpath下面,即target/classes下面。
    ???? 所謂資源過濾,就是過濾這些文件夾下面的文件里面的內容,看里面的maven變量是否需要替換。默認情況下,只有pom.xml里面的變量才會被替換,資源文件是不會被過濾的,但是可以設置,如下:

    [html]?view plaincopyprint?
  • <build>??
  • ????????<finalName>agentmanager</finalName>??
  • ????????<sourceDirectory>src/main/java</sourceDirectory>??
  • ????????<resources>??
  • ????????????<!--?控制資源文件的拷貝?-->??
  • ????????????<resource>??
  • ????????????????<directory>src/main/resources</directory>??
  • ????????????????<excludes>??
  • ????????????????????<exclude>**/jre.zip</exclude>??
  • ????????????????????<exclude>**/jre.tar</exclude>??
  • ????????????????????<exclude>agentmanager.jsmooth</exclude>??
  • ????????????????????<exclude>assembly.xml</exclude>??
  • ????????????????</excludes>??
  • ????????????????<targetPath>${project.build.directory}</targetPath>??
  • ????????????</resource>??
  • ????????????<resource>??
  • ????????????????<directory>src/main/resources/conf</directory>??
  • ????????????????<targetPath>${basedir}/conf</targetPath>??
  • ????????????????<filtering>true</filtering>??
  • ????????????</resource>??
  • ????????</resources>??
  • ????</build>??

  • ?

    如jdbc.properties

    [css]?view plaincopyprint?
  • jdbc.driverClassName=${db.driver}??
  • jdbc.url=${db.url}??
  • jdbc.username=${db.user}??
  • jdbc.password=${db.pwd}??
  • ?

    profile文件

    [html]?view plaincopyprint?
  • <profiles>??
  • ?<profile>??
  • ??<id>dev</id>??
  • ??<properties>??
  • ???<db.driver>oracle.jdbc.driver.OracleDriver</db.driver>??
  • ???<db.url>jdbc:oracle:thin:@10.252.48.3:1521:dbname</db.url>??
  • ???<db.user>username</db.user>??
  • ???<db.pwd>userpwd</db.pwd>??
  • ??</properties>??
  • ?</profile>??
  • ?<profile>??
  • ??<id>test</id>??
  • ??<properties>??
  • ???<db.driver>oracle.jdbc.driver.OracleDriver</db.driver>??
  • ???<db.url>jdbc:oracle:thin:@10.252.48.3:1521:testdbname</db.url>??
  • ???<db.user>testusername</db.user>??
  • ???<db.pwd>testuserpwd</db.pwd>??
  • ??</properties>??
  • ?</profile>??
  • </profiles>??


  • 在構建時可以使用-P參數激活一個或多個profile,多個之間用逗號分隔
    如?mvn clean install -Pdev

    3、maven profile

    ???? 上面例子應該可以看出profile是做什么的,其實就相當于定義了一系列的profile變量,在具體構建時可用使用其中的某個profile去變量替換資源文件。
    ??????激活profile的方式有很多,如命令行激活(上面),settings文件顯式激活、系統屬性激活、操作系統環境激活、默認激活、文件存在與否激活等,具體可以參考官網資料


    3.1 profile的種類

    ??? ?根據需要,可以在以下文件聲明profile。
    ????? 1、pom.xml?針對當前項目
    ???? ?2、用戶 settings.xml?用戶目錄下的.m2/settings.xml, 對當前用戶的所有項目有效。
    ????? 3、全局 settings.xml?即maven安裝目錄下的conf/settings.xml。對本機上的所有項目有效。

    4、web資源過濾

    ???? 在maven的web項目里面,除了上面所說的資源文件(src/main/resources)之外,還有一類叫做web資源目錄,即src/main/webapp下面的js、css等等。默認情況下,這些目錄是不被資源過濾的,開啟的命令如下:

    [html]?view plaincopyprint?
  • ??<plugin>??
  • ????<groupId>org.apache.maven.plugins</groupId>??
  • ????<artifactId>maven-war-plugin</artifactId>??
  • ????<version>2.1.1</version>??
  • ????<configuration>??
  • ????????<webResources>??
  • ????????<resource>??
  • ????????????<directory>src/main/webapp</directory>??
  • ????????????<filtering>true</filtering>??
  • ????????????<includes>??
  • ????????????<include>**/*.css</include>??
  • ????????????<include>**/*.js</include>??
  • ????????????</includes>????????????????????????????
  • ????????</resource>??????
  • ????????</webResources>??
  • ????</configuration>??
  • </plugin>??
  • ?

    轉載于:https://www.cnblogs.com/davidwang456/p/3915031.html

    總結

    以上是生活随笔為你收集整理的深入理解maven及应用--转的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。