<modelVersion>4.0.0</modelVersion><groupId>com.mycompany.app</groupId><artifactId>my-app</artifactId><version>1.1.0.1</version><packaging>jar</packaging><name>myapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></project>解釋一下這個(gè)xml文件的內(nèi)容:modelVersion: 這個(gè)XML文件所使用的POM格式的版本groupId: 相當(dāng)于這個(gè)project的所有者或者機(jī)構(gòu)的一個(gè)標(biāo)識,一般是com.company.xxx這種格式artifactId: 這個(gè)project最后所生成的文檔(jar、war)的名字,比如對于junit這個(gè)開源的project,它的artifactId就是junitpackaging: 這個(gè)project的打包的類型,一般是war、jar等值version: project的版本name: project的名字,生成文檔等內(nèi)容的時(shí)候會用的這個(gè)名字這個(gè)project創(chuàng)建好后和普通的project沒有什么不同,我們直接往里面放源代碼進(jìn)行開發(fā)就可以了,如果有目錄想修改的也完全可以。POM & archetypearchetype就是一個(gè)project的模板,上面我們生成的project就是用默認(rèn)的archetype生成的。如果使用不同的archetype,生成的project結(jié)構(gòu)會有所不同。 一個(gè)archetype指明了1) 項(xiàng)目的目錄結(jié)構(gòu)以及什么目錄是放source code,哪些是放test source code,哪些目錄是放resource的。2) 一個(gè)默認(rèn)的pom.xml文件,這個(gè)默認(rèn)的pom.xml文件中的groupId,artifactId,version用占位符表示,在創(chuàng)建project的時(shí)候通過參數(shù)傳進(jìn)來。pom.xml文件的POM全稱是Project Object Model,這個(gè)文件對于maven的使用者來說是一個(gè)和maven交互的渠道,pom.xml包含了一個(gè)maven project的配置,一個(gè)project該如何編譯打包,project有哪些依賴項(xiàng)等等。仔細(xì)看一下我們前面創(chuàng)建project的命令:mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp1) archetype:generate, 這是一個(gè)maven的plugin,用來從一個(gè)archetype創(chuàng)建一個(gè)project,關(guān)于plugin后面再說。2) DarchetypeGroupId,這個(gè)就是指的archetype的groupid,也就是說是用的哪個(gè)archetype,或者說用哪個(gè)項(xiàng)目模板。3) 后面的兩個(gè)參數(shù),用來放大pom.xml文件里面,作為當(dāng)前創(chuàng)建的project的描述信息。Project創(chuàng)建好了,看如何去編譯,直接進(jìn)入的project的目錄,在命令行下:mvn compile編譯完后maven會創(chuàng)建一個(gè)target目錄去保存編譯結(jié)果。 我們需要編譯成一個(gè)什么樣的內(nèi)容,以及要輸出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因?yàn)槲覀兡壳安]有指定這些內(nèi)容,所以maven會使用默認(rèn)值。我們還可以用maven執(zhí)行test:mvn test第一次執(zhí)行時(shí),maven會去下載一些依賴項(xiàng)。另外要注意的時(shí),如果我們更改了默認(rèn)的目錄結(jié)構(gòu),maven會因?yàn)檎襜u到test而無法去執(zhí)行test。如果只需要編譯test可以執(zhí)行:mvn test-compile要把項(xiàng)目打包,執(zhí)行:mvn packagemvn會根據(jù)pom.xml里面的packaging選項(xiàng)打包成相應(yīng)的文件。repository & dependencymaven里面有一個(gè)repository的概念,當(dāng)我們的項(xiàng)目依賴于某個(gè)jar時(shí),maven會去repository里面去找。repository分兩種,一種是遠(yuǎn)程的,一種是本地的。如果有幾個(gè)project都用到j(luò)unit,我們可以把junit放在repository里面,幾個(gè)project可以公用,節(jié)約存儲空間而且方便管理,這個(gè)repository的位置可以在pom.xml里面設(shè)置。本地的默認(rèn)的路徑是安裝用戶的目錄下的 .m2\repository 文件夾。如果一個(gè)依賴項(xiàng)在本地的repository里面沒有,那么maven會去他自己的遠(yuǎn)程的repositoryhttp://repo.maven.apache.org/maven2 去下載后放到本地的repository里面。也就是說,我們?nèi)绻覀兊膒roject需要要引用一個(gè)依賴項(xiàng),我們只需要在pom.xml文件中進(jìn)行配置,maven會自動幫我們?nèi)ヒ谩?我們之前的創(chuàng)建project里面需要寫單元測試,引用到了junit,看pom中的配置:12345678<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies>每一個(gè)需要為每一個(gè) dependency 指明groupId,artifactId,version。scope很簡單,意思是說我們需要怎么引用,比如我們上面的例子里面設(shè)置的是test,意思是說只在test里面引用junit。 但是我們?nèi)绾沃纆roupId,artifactId和version呢? 比如我現(xiàn)在想引用log4j,這個(gè)幾個(gè)值怎么填? 可以去http://mirrors.ibiblio.org/maven2/ 上去查找。比如log4j,我們就在上面這個(gè)地址加上log4j,也就是http://mirrors.ibiblio.org/maven2/junit/。進(jìn)去后會有一個(gè)maven-metadata.xml,打開就可以知道這些值了然后添加這個(gè)dependency了。如果要把一個(gè)project安裝到本地的repository里面,可以執(zhí)行下面的命令:mvn install到這里就說完了創(chuàng)建,編譯,測試,打包以及安裝,大部分的項(xiàng)目也就是做這些事情。再介紹幾個(gè)其它命令:mvn site : 為你的project創(chuàng)建一個(gè)站點(diǎn)mvn clean: 清除target目錄下的所有文件mvn eclipse:eclipse :為project生成eclipse的工程文件和classpath文件build lifecycle & build phase & goalmaven有一套build的生命周期,是按照一套順序走下來的,這一套順序就叫一個(gè)生命周期(lifecycle)。maven內(nèi)置三種生命周期:default, clean 和 site。一個(gè)生命周期分為多個(gè)build phase,下面是default生命周期全部的build phase:validate:validate the project is correct and all necessary information is available.initialize:initialize build state, e.g. set properties or create directories.generate-sources:generate any source code for inclusion in compilation.process-sources:process the source code, for example to filter any values.generate-resources:generate resources for inclusion in the package.process-resources:copy and process the resources into the destination directory, ready for packaging.compile:compile the source code of the project.process-classes:post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.generate-test-sources:generate any test source code for inclusion in compilation.process-test-sources:process the test source code, for example to filter any values.generate-test-resources:create resources for testing.process-test-resources:copy and process the resources into the test destination directory.test-compile:compile the test source code into the test destination directoryprocess-test-classes:post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.test:run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.prepare-package:perform 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)package:take the compiled code and package it in its distributable format, such as a JAR.pre-integration-test:perform actions required before integration tests are executed. This may involve things such as setting up the required environment.integration-test:process and deploy the package if necessary into an environment where integration tests can be run.post-integration-test:perform actions required after integration tests have been executed. This may including cleaning up the environment.verify:run any checks to verify the package is valid and meets quality criteria.install:install the package into the local repository, for use as a dependency in other projects locally.deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.這些build phase是按照順序執(zhí)行的,如果執(zhí)行后面的build phase,前面的build phase 也會被執(zhí)行。例如如果執(zhí)行:mvn deploy前面的install、verify一直到validate這些build phase都會執(zhí)行。每一個(gè)build phase是由goal組成的,一個(gè)goal其實(shí)就是一個(gè)任務(wù),一個(gè)goal可以關(guān)聯(lián)到一個(gè)build phase也可以不關(guān)聯(lián)到任何build phase 。 不關(guān)聯(lián)到任何phase的goal是可以獨(dú)立執(zhí)行的,例如:mvn clean dependency:copy-dependencies package上面的命令會導(dǎo)致先執(zhí)行clean這個(gè)phase,然后拷貝依賴項(xiàng),最后打包。注意,這里clean這個(gè)goal是clean這個(gè)lifecycle里面的一個(gè)goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起執(zhí)行的。 如果一個(gè)goal被綁定到多個(gè)phase上,那么goal就會被執(zhí)行多次。phase的順序是已經(jīng)固定的,如果一個(gè)phase沒有綁定到任何goal,那么phase就不會被執(zhí)行。 一個(gè)goal可以通過兩種方式綁定到一個(gè)phase,一個(gè)是指定packaging,另一個(gè)就是plugin。packaging&pluginplugin就是用來向maven提供goal的。一個(gè)plugin里面可以有多個(gè)goal,這就是為什么我們在指明goal時(shí),前面會用一個(gè)冒號與plugin的名字。一個(gè)plugin自己可以指定自己的goal綁定到哪個(gè)lifecycle的哪一個(gè)Phase上,另外也可以配置一個(gè)goal綁定到哪個(gè)phase上??梢栽趐om.xml里面配置。 看兩個(gè)配置:123456789101112131415161718<plugin><groupId>org.codehaus.modello</groupId><artifactId>modello-maven-plugin</artifactId><version>1.4</version><executions><execution><configuration><models><model>src/main/mdo/maven.mdo</model></models><version>4.0.0</version></configuration><goals><goal>java</goal></goals></execution></executions></plugin>這個(gè)就在當(dāng)前的lifecycle里面添加了一個(gè)名字叫java的goal,這goal會根據(jù)自己的配置去綁定到一個(gè)phase,在phase執(zhí)行的時(shí)候這個(gè)goal會執(zhí)行。并且在這個(gè)配置里面,可以指定多個(gè)execution來讓這個(gè)goal執(zhí)行多次。看另一個(gè)示例配置:12345678910111213<plugin><groupId>com.mycompany.example</groupId><artifactId>display-maven-plugin</artifactId><version>1.0</version><executions><execution><phase>process-test-resources</phase><goals><goal>time</goal></goals></execution></executions></plugin>這個(gè)名為time的goal把自己綁定到了process-test-resource這個(gè)phase上。在默認(rèn)情況下,并不是所有的phase都綁定了goal,比如clean這個(gè)lifecycle是有三個(gè)phase的,但是只有其中的一個(gè)名為clean的phase默認(rèn)綁定了一個(gè)clean:clean goal,其它兩個(gè)phase默認(rèn)沒有綁定任何goal。之前已經(jīng)提到過packaging,在pom.xml可以指定packaging,每種packaging都設(shè)定了一組phase和goal之間的綁定關(guān)系。在default lifecycle下,當(dāng)packaging為 ejb/ejb3/jar/par/rar/war 其中之一的值的時(shí)候,只有以下的phase綁定了goal,具體如下:process-resourcesresources:resourcescompilecompiler:compileprocess-test-resourcesresources:testResourcestest-compilecompiler:testCompiletestsurefire:testpackagejar:jarinstallinstall:installdeploydeploy:deploy總結(jié)首先搞清楚maven的project的目錄結(jié)構(gòu),然后理解maven的lifecycle,lifecycle是由build phase組成,每一個(gè)build phase會綁定到goal。goal是由plugin提供的。 每一種packaging的值都表明了一定的phase和goal之間的綁定關(guān)系。另外一個(gè)很重要的就是dependency,我們要在項(xiàng)目中引用一個(gè)依賴,只需要在pom.xml指定依賴的名字和版本,maven會自動去遠(yuǎn)程的repository下載,然后放到本地的repository里面,這樣以后所有的project都可以共用其它細(xì)節(jié)可以參考http://maven.apache.org/guides/index.html。