<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文件的內容:modelVersion: 這個(gè)XML文件所使用的POM格式的版本groupId: 相當于這個(gè)project的所有者或者機構的一個(gè)標識,一般是com.company.xxx這種格式artifactId: 這個(gè)project最后所生成的文檔(jar、war)的名字,比如對于junit這個(gè)開(kāi)源的project,它的artifactId就是junitpackaging: 這個(gè)project的打包的類(lèi)型,一般是war、jar等值version: project的版本name: project的名字,生成文檔等內容的時(shí)候會(huì )用的這個(gè)名字這個(gè)project創(chuàng )建好后和普通的project沒(méi)有什么不同,我們直接往里面放源代碼進(jìn)行開(kāi)發(fā)就可以了,如果有目錄想修改的也完全可以。POM & archetypearchetype就是一個(gè)project的模板,上面我們生成的project就是用默認的archetype生成的。如果使用不同的archetype,生成的project結構會(huì )有所不同。 一個(gè)archetype指明了1) 項目的目錄結構以及什么目錄是放source code,哪些是放test source code,哪些目錄是放resource的。2) 一個(gè)默認的pom.xml文件,這個(gè)默認的pom.xml文件中的groupId,artifactId,version用占位符表示,在創(chuàng )建project的時(shí)候通過(guò)參數傳進(jìn)來(lái)。pom.xml文件的POM全稱(chēng)是Project Object Model,這個(gè)文件對于maven的使用者來(lái)說(shuō)是一個(gè)和maven交互的渠道,pom.xml包含了一個(gè)maven project的配置,一個(gè)project該如何編譯打包,project有哪些依賴(lài)項等等。仔細看一下我們前面創(chuàng )建project的命令:mvn archetype:generate DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myapp1) archetype:generate, 這是一個(gè)maven的plugin,用來(lái)從一個(gè)archetype創(chuàng )建一個(gè)project,關(guān)于plugin后面再說(shuō)。2) DarchetypeGroupId,這個(gè)就是指的archetype的groupid,也就是說(shuō)是用的哪個(gè)archetype,或者說(shuō)用哪個(gè)項目模板。3) 后面的兩個(gè)參數,用來(lái)放大pom.xml文件里面,作為當前創(chuàng )建的project的描述信息。Project創(chuàng )建好了,看如何去編譯,直接進(jìn)入的project的目錄,在命令行下:mvn compile編譯完后maven會(huì )創(chuàng )建一個(gè)target目錄去保存編譯結果。 我們需要編譯成一個(gè)什么樣的內容,以及要輸出到什么地方等等,都是可以在pom.xml文件里面配置的,但是因為我們目前并沒(méi)有指定這些內容,所以maven會(huì )使用默認值。我們還可以用maven執行test:mvn test第一次執行時(shí),maven會(huì )去下載一些依賴(lài)項。另外要注意的時(shí),如果我們更改了默認的目錄結構,maven會(huì )因為找bu到test而無(wú)法去執行test。如果只需要編譯test可以執行:mvn test-compile要把項目打包,執行:mvn packagemvn會(huì )根據pom.xml里面的packaging選項打包成相應的文件。repository & dependencymaven里面有一個(gè)repository的概念,當我們的項目依賴(lài)于某個(gè)jar時(shí),maven會(huì )去repository里面去找。repository分兩種,一種是遠程的,一種是本地的。如果有幾個(gè)project都用到j(luò )unit,我們可以把junit放在repository里面,幾個(gè)project可以公用,節約存儲空間而且方便管理,這個(gè)repository的位置可以在pom.xml里面設置。本地的默認的路徑是安裝用戶(hù)的目錄下的 .m2\repository 文件夾。如果一個(gè)依賴(lài)項在本地的repository里面沒(méi)有,那么maven會(huì )去他自己的遠程的repositoryhttp://repo.maven.apache.org/maven2 去下載后放到本地的repository里面。也就是說(shuō),我們如果我們的project需要要引用一個(gè)依賴(lài)項,我們只需要在pom.xml文件中進(jìn)行配置,maven會(huì )自動(dòng)幫我們去引用。 我們之前的創(chuàng )建project里面需要寫(xiě)單元測試,引用到了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很簡(jiǎn)單,意思是說(shuō)我們需要怎么引用,比如我們上面的例子里面設置的是test,意思是說(shuō)只在test里面引用junit。 但是我們如何知道groupId,artifactId和version呢? 比如我現在想引用log4j,這個(gè)幾個(gè)值怎么填? 可以去http://mirrors.ibiblio.org/maven2/ 上去查找。比如log4j,我們就在上面這個(gè)地址加上log4j,也就是http://mirrors.ibiblio.org/maven2/junit/。進(jìn)去后會(huì )有一個(gè)maven-metadata.xml,打開(kāi)就可以知道這些值了然后添加這個(gè)dependency了。如果要把一個(gè)project安裝到本地的repository里面,可以執行下面的命令:mvn install到這里就說(shuō)完了創(chuà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的生命周期,是按照一套順序走下來(lái)的,這一套順序就叫一個(gè)生命周期(lifecycle)。maven內置三種生命周期: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是按照順序執行的,如果執行后面的build phase,前面的build phase 也會(huì )被執行。例如如果執行:mvn deploy前面的install、verify一直到validate這些build phase都會(huì )執行。每一個(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是可以獨立執行的,例如:mvn clean dependency:copy-dependencies package上面的命令會(huì )導致先執行clean這個(gè)phase,然后拷貝依賴(lài)項,最后打包。注意,這里clean這個(gè)goal是clean這個(gè)lifecycle里面的一個(gè)goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起執行的。 如果一個(gè)goal被綁定到多個(gè)phase上,那么goal就會(huì )被執行多次。phase的順序是已經(jīng)固定的,如果一個(gè)phase沒(méi)有綁定到任何goal,那么phase就不會(huì )被執行。 一個(gè)goal可以通過(guò)兩種方式綁定到一個(gè)phase,一個(gè)是指定packaging,另一個(gè)就是plugin。packaging&pluginplugin就是用來(lái)向maven提供goal的。一個(gè)plugin里面可以有多個(gè)goal,這就是為什么我們在指明goal時(shí),前面會(huì )用一個(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è)就在當前的lifecycle里面添加了一個(gè)名字叫java的goal,這goal會(huì )根據自己的配置去綁定到一個(gè)phase,在phase執行的時(shí)候這個(gè)goal會(huì )執行。并且在這個(gè)配置里面,可以指定多個(gè)execution來(lái)讓這個(gè)goal執行多次。看另一個(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上。在默認情況下,并不是所有的phase都綁定了goal,比如clean這個(gè)lifecycle是有三個(gè)phase的,但是只有其中的一個(gè)名為clean的phase默認綁定了一個(gè)clean:clean goal,其它兩個(gè)phase默認沒(méi)有綁定任何goal。之前已經(jīng)提到過(guò)packaging,在pom.xml可以指定packaging,每種packaging都設定了一組phase和goal之間的綁定關(guān)系。在default lifecycle下,當packaging為 ejb/ejb3/jar/par/rar/war 其中之一的值的時(shí)候,只有以下的phase綁定了goal,具體如下:process-resourcesresources:resourcescompilecompiler:compileprocess-test-resourcesresources:testResourcestest-compilecompiler:testCompiletestsurefire:testpackagejar:jarinstallinstall:installdeploydeploy:deploy總結首先搞清楚maven的project的目錄結構,然后理解maven的lifecycle,lifecycle是由build phase組成,每一個(gè)build phase會(huì )綁定到goal。goal是由plugin提供的。 每一種packaging的值都表明了一定的phase和goal之間的綁定關(guān)系。另外一個(gè)很重要的就是dependency,我們要在項目中引用一個(gè)依賴(lài),只需要在pom.xml指定依賴(lài)的名字和版本,maven會(huì )自動(dòng)去遠程的repository下載,然后放到本地的repository里面,這樣以后所有的project都可以共用其它細節可以參考http://maven.apache.org/guides/index.html。