Maven瀹樼綉涓奙aven in 五 Minutes 鏂囨。缈昏瘧

Maven瀹樼綉涓奙aven in 5 Minutes 鏂囨。缈昏瘧

Maven in 5 Minutes
Maven 5鍒嗛挓鏁欑▼
Prerequisites
鍓嶆彁锛?br>
You must have an understanding of how to install software on your computer. If you do not know how to do this,
浣犲凡缁忕悊瑙e浣曞湪浣犵數鑴戜笂瀹夎杞欢銆傚鏋滆繖涓綘杩樹笉鐭ラ亾鎬庝箞鍋氾紝
please ask someone at your office, school, etc or pay someone to explain this to you. The Maven mailing lists are
璇蜂綘闂竴涓嬩綘鐨勫悓浜嬨€佸悓瀛︼紝鎴栬€呰姳閽遍棶涓€涓嬪叾浠栫殑浜虹粰浣犺В閲婁竴涓嬶紝缁橫aven缁勭粐鍙戦偖浠朵笉鏄緢濂界殑寤鸿銆?br>not the best place to ask for this advice.
Installation
瀹夎
Maven is a Java tool, so you must have Java installed in order to proceed.
Maven 鏄竴涓猨ava宸ュ叿锛屼负浜嗗紑濮嬩娇鐢∕aven璇峰厛瀹夎java
First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
棣栧厛涓嬭浇Maven骞舵牴鎹彁绀烘寚寮曞畬鎴愬畨瑁咃紝鐒跺悗鍦ㄥ懡浠ゆ彁绀鸿绐楀彛鎵ц涓嬮潰鐨勮剼鏈細
mvn --version

It should print out your installed version of Maven, for example:
浣犵殑灞忓箷涓婂簲璇ヤ細鍑虹幇浣犲畨瑁呯殑Maven鐨勭増鏈俊鎭紝渚嬪锛?br>Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: D:\apache-maven-3.0.5\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_25\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.
鍩轰簬浣犵殑缃戠粶鐜锛屼綘鍙兘闇€瑕佽繘琛岄澶栫殑閰嶇疆銆傚鏋滈渶瑕佹煡鐪嬫寚鍗楅厤缃甅aven銆?br>If you are using Windows, you should look at Windows Prerequisites to ensure that you are prepared to use Maven on Windows.
濡傛灉浣犵敤鐨勬槸Windows鎿嶄綔绯荤粺锛屼綘搴旇鐪嬬湅Windows鍏堝喅鏉′欢,纭繚浣犲凡缁忓噯澶囧ソ鍦╓indows涓婁娇鐢∕aven銆?br>
Creating a Project
鍒涘缓涓€涓」鐩?br>
You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
涓轰簡瀛樻斁浣犵殑椤圭洰锛屼綘闇€瑕佸垱寤轰竴涓洰褰曪紝骞跺湪姝ょ洰褰曟墽琛屼竴涓猻hell鑴氭湰銆傚湪浣犵殑鍛戒护琛岀獥鍙o紝鎵ц涓嬮潰鐨凪aven鑴氭湰锛?br>mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

If you have just installed Maven, it may take a while on the first run.
濡傛灉浣犲垰鍒氬畬鎴愬畨瑁匨aven,绗竴娆¤繍琛屽彲鑳介渶瑕佺瓑寰呬竴娈垫椂闂淬€?br>This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository.
杩欐槸鍥犱负Maven姝e湪涓嬭浇鏈€杩戠殑宸ュ叿锛堟彃浠禞Ar鍖呭拰鍏朵粬鐨勬枃浠讹級鍒颁綘鏈湴鐨勫簱
You may also need to execute the command a couple of times before it succeeds.
杩欏彲鑳介渶瑕佹墽琛屽ソ鍑犳杩欎釜鍛戒护锛岀洿鍒拌繖涓懡浠ゆ墽琛屾垚鍔熴€?br>This is because the remote server may time out before your downloads are complete.
杩欐槸鍥犱负鍦ㄤ綘涓嬭浇瀹屾墍鏈夐渶瑕佺殑鏂囦欢涔嬪墠锛岃繙绋嬫湇鍔¤繛鎺ュ彲鑳戒細瓒呮椂
Don't worry, there are ways to fix that.
鍒媴蹇冿紝杩欐湁濂藉嚑绉嶆柟寮忓幓瑙e喅闂銆?br>
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
浣犱細鍙戠幇锛実enerate goal鍛戒护鍒涘缓鐨勭洰褰曞悕绉颁笌缁欏畾鐨勨€渁rtifactId鈥濆弬鏁颁竴鑷淬€傚垏鎹㈠埌璇ョ洰褰曘€?br>
cd my-app

Under this directory you will notice the following standard project structure.
鍦ㄨ繖涓洰褰曚腑锛屼綘浼氬彂鐜版爣鍑嗙殑椤圭洰鐩綍缁撴瀯

my-app
|-- pom.xml
`-- src
聽聽聽 |-- main
聽聽聽 |聽聽 `-- java
聽聽聽 |聽聽聽聽聽聽 `-- com
聽聽聽 |聽聽聽聽聽聽聽聽聽聽 `-- mycompany
聽聽聽 |聽聽聽聽聽聽聽聽聽聽聽聽聽聽 `-- app
聽聽聽 |聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 `-- App.java
聽聽聽 `-- test
聽聽聽聽聽聽聽 `-- java
聽聽聽聽聽聽聽聽聽聽聽 `-- com
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 `-- mycompany
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 `-- app
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 `-- AppTest.java

The src/main/java directory contains the project source code,
杩欎釜鈥渟rc/main/java鈥濈洰褰曪紝鍖呭惈浜嗛」鐩殑婧愪唬鐮?br>the src/test/java directory contains the test source, and the pom.xml file is the project's Project Object Model, or POM.
杩欎釜"src/test/java"鐩綍鍖呭惈浜嗘祴璇曚唬鐮侊紝杩欎釜pom.xml鏂囦欢鏄」鐩殑椤圭洰瀵硅薄妯″瀷锛圥OM锛?br>The POM

The pom.xml file is the core of a project's configuration in Maven.
pom.xml鏂囦欢鏄疢aven鐨勯」鐩厤缃牳蹇?br>It is a single configuration file that contains the majority of information required to build a project in just the way you want.
杩欐槸涓€涓崟鐙殑閰嶇疆鏂囦欢锛屽寘鍚互浣犳兂瑕佺殑鏂瑰紡鏋勫缓涓€涓」鐩殑蹇呴』鐨勫ぇ閮ㄥ垎淇℃伅銆?br>The POM is huge and can be daunting in its complexity,
POM 鏄竴涓畯澶х殑骞朵笖瀹冪殑澶嶆潅鎬у彲鑳戒細浣夸綘閫€缂╃殑
but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
浣嗘槸鏈夋晥鐨勪娇鐢ㄥ畠涔熷苟涓嶉渶瑕佺悊瑙e叏閮ㄧ殑澶嶆潅鍐呭锛屼竴涓」鐩殑POM鏄細
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
聽 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
聽 <modelVersion>4.0.0</modelVersion>

聽 <groupId>com.mycompany.app</groupId>
聽 <artifactId>my-app</artifactId>
聽 <version>1.0-SNAPSHOT</version>
聽 <packaging>jar</packaging>

聽 <name>Maven Quick Start Archetype</name>
聽 <url>http://maven.apache.org</url>

聽 <dependencies>
聽聽聽 <dependency>
聽聽聽聽聽 <groupId>junit</groupId>
聽聽聽聽聽 <artifactId>junit</artifactId>
聽聽聽聽聽 <version>4.8.2</version>
聽聽聽聽聽 <scope>test</scope>
聽聽聽 </dependency>
聽 </dependencies>
</project>

What did I just do?
鎴戝垰鍒氬仛浜嗕粈涔?

You executed the Maven goal archetype:generate, and passed in various parameters to that goal.
浣犳墽琛屼簡Maven鐩爣鍘熷瀷鍛戒护锛氶€氳繃澶氭牱鐨勫弬鏁扮敓鎴愪簡閭d釜鐩爣銆?br>The prefix archetype is the plugin that contains the goal.
杩欎釜鍔犲墠缂€鐨勫師鍨嬫槸鍖呭ソ浜嗚繖涓洰鏍囩殑鎻掍欢銆?br>If you are familiar with Ant, you may conceive of this as similar to a task.
濡傛灉浣犵啛鎮堿nt锛屼綘鍙互鍋囧畾瀹冧笌涓€涓换鍔$浉浼笺€?br>This goal created a simple project based upon an archetype.
杩欎釜鐩爣鍒涘缓鐨勪竴涓熀浜庡師鍨嬬殑鏍蜂緥椤圭洰銆?br>Suffice it to say for now that a plugin is a collection of goals with a general common purpose.
鍙璇翠竴涓彃浠舵槸涓€鎵规湁鍏卞悓鐩殑鐨勭洰鏍囧氨澶熶簡銆?br>For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".
渚嬪jboss-maven-plugin鎻掍欢锛屽畠鐨勭洰鐨勬槸澶勭悊浼楀鐨刯boss椤圭洰銆?br>
Build the Project
鏋勫缓涓€涓」鐩?br>
mvn package

The command line will print out various actions, and end with the following:
鍛戒护琛屽皢杈撳嚭璁稿鍔ㄤ綔锛屾渶鍚庝互杩欐牱缁撳熬锛?br>聽...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package.
涓嶅儚绗竴娆″彧鎯宠兘澶熺殑鍛戒护锛坅rchetype:generate锛?浣犱篃璁镐細鍙戠幇绗簩娆℃墽琛屽彧鏄竴涓畝鍗曠殑鍗曡瘝-package銆?br>Rather than a goal, this is a phase.
鑰屼笉鏄竴涓猤oal锛岃繖鏄竴涓樁娈点€?br>A phase is a step in the build lifecycle, which is an ordered sequence of phases.
涓€涓幆鑺傛槸鍦ㄦ瀯寤轰竴涓瀯寤虹敓鍛藉懆鏈熸湁搴忔楠ょ殑涓€姝ャ€?br>When a phase is given, Maven will execute every phase in the sequence up to and including the one defined.
褰撲竴涓幆鑺傞渶瑕佹墽琛岋紝Maven灏嗘湁椤哄簭鐨勬墽琛屾槑瀹氫箟涓寘鍚瘡涓€涓瓙鐜妭銆?br>For example, if we execute the compile phase, the phases that actually get executed are:
渚嬪锛屽鏋滄垜浠鎵ц缂栬瘧鐜妭锛岃繖涓幆鑺傚疄闄呬笂灏嗘墽琛岋細

聽聽聽 validate
聽聽聽 楠岃瘉
聽聽聽 generate-sources
聽聽聽 鐢熸垚婧愪唬鐮?br>聽聽聽 process-sources
聽聽聽 澶勭悊婧愪唬鐮?br>聽聽聽 generate-resources
聽聽聽 鐢熸垚璧勬簮
聽聽聽 process-resources
聽聽聽 澶勭悊璧勬簮
聽聽聽 compile
聽聽聽 缂栬瘧

You may test the newly compiled and packaged JAR with the following command:
浣犲彲浠ラ€氳繃涓嬮潰鐨勫懡浠ゆ祴璇曟柊缂栬瘧鍜屾墦鍖呯殑jar

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

Which will print the quintessential:
灏嗚緭鍑虹粡鍏哥殑锛?br>
Hello World!

Running Maven Tools
杩愯 Maven宸ュ叿
Maven Phases
Maven 闃舵
Although hardly a comprehensive list, these are the most common default lifecycle phases executed.
铏界劧鍑犱箮娌℃湁涓€涓叏闈㈢殑娓呭崟,浣嗚繖浜涙槸鏈€甯歌鐨勭己鐪佺敓鍛藉懆鏈熼樁娈垫墽琛屻€?br>
聽聽聽 validate: validate the project is correct and all necessary information is available
聽聽聽 validate锛氶獙璇佽繖涓」鐩槸姝g‘鐨勫苟涓斿叏閮ㄤ富瑕佺殑淇℃伅閮芥槸鍙敤
聽聽聽 compile: compile the source code of the project
聽聽聽 compile: 缂栬瘧杩欎釜椤圭洰鐨勬簮浠g爜
聽聽聽 test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
聽聽聽 test:鐢ㄥ悎閫傜殑娴嬭瘯妗嗘灦娴嬭瘯缂栬瘧鍚庣殑浠g爜
聽聽聽 package: take the compiled code and package it in its distributable format, such as a JAR.
聽聽聽 package: 鍙栫紪璇戝悗鐨勪唬鐮佸苟鎸夌収鍙垎閰嶇殑鏍煎紡杩涜鎵撳寘锛屽jar
聽聽聽 integration-test: process and deploy the package if necessary into an environment where integration tests can be run
聽聽聽 integration-test:濡傛灉闇€瑕佺殑璇濓紝澶勭悊鍜岄儴缃查」鐩寘鍒伴泦鎴愭祴璇曠幆澧冧腑銆?br>聽聽聽 verify: run any checks to verify the package is valid and meets quality criteria
聽聽聽 verify: 杩愯涓€浜涙鏌ユ潵楠岃瘉杩欎釜鍖呮槸鏈夋晥鐨勮川閲忔娴嬨€?br>
聽聽聽 install: install the package into the local repository, for use as a dependency in other projects locally
聽聽聽 install锛氬畨瑁呰繖涓寘鍒版湰鍦板簱涓紝涓虹敤浣滄湰鍦板叾浠栭」鐩殑渚濊禆銆?br>
聽聽聽 deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
聽聽聽 deploy: 鍦ㄩ泦鎴愭垨鑰呭彂甯冪幆澧冨鐞嗭紝鎷疯礉杩欎釜鏈€缁堢殑鍖呭埌杩滅▼搴撶敤鏉ュ拰鍏朵粬鐨勫紑鍙戜汉鍛樻垨鑰呴」鐩墍鍒嗕韩

There are two other Maven lifecycles of note beyond the default list above. They are
杩樻湁鍙﹀涓や釜Maven鐢熷懡鍛ㄦ湡瓒呭嚭浜嗕笂闈㈢己鐪佸懡浠ゅ垪琛紝瀹冧滑鏄細
聽聽聽 clean: cleans up artifacts created by prior builds
聽聽聽 clean锛?br>
聽聽聽 site: generates site documentation for this project
聽聽聽 site锛氫负椤圭洰鐢熸垚璁剧疆鏂囨。

Phases are actually mapped to underlying goals.
杩欎簺鐜妭瀹為檯涓婂簲璁惧湪鐩爣搴曞眰銆?br>The specific goals executed per phase is dependant upon the packaging type of the project.
涓嶅悓鐨刧oals 鎵ц鐨勬瘡涓€涓幆鑺傚彇鍐充簬椤圭洰鐨勬墦鍖呯被鍨?br>For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.
渚嬪锛屾墦鍖呮墽琛宩ar鍛戒护濡傛灉杩欎釜椤圭洰鏄被鍨嬫槸JAR锛屽拰war鍛戒护锛屽鏋滀綘鐚滄祴杩欎釜椤圭洰鐨勭被鍨嬫槸war锛?br>
An interesting thing to note is that phases and goals may be executed in sequence.
涓€涓湁瓒g殑浜嬫儏闇€瑕佹敞鎰忕殑鏄紝鐜妭鍜岀洰鏍囨槸鎸夌収涓€涓『搴忔墽琛岀殑銆?br>
mvn clean dependency:copy-dependencies package

This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).
杩欎釜鍛戒护鍙互娓呴櫎杩欎釜椤圭洰锛屽鍒朵緷璧栨枃浠讹紝鎵撳寘杩欎釜椤圭洰锛堟墽琛屽埌鎵撳寘鍏ㄩ儴鐨勭幆鑺傦級

Generating the Site

mvn site

This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.
杩欎釜鐜妭浠ラ」鐩殑POM鏂囦欢涓哄熀纭€鐢熸垚涓€涓枃妗o紝浣犲彲浠ユ煡鐪嬭繖涓敓鎴愭枃妗i€氳繃target/site.

Conclusion
缁撹

We hope this quick overview has piqued your interest in the versatility of Maven.
鎴戜滑甯屾湜杩欏揩閫熸杩版縺璧蜂簡浣犲Maven澶氬姛鑳芥€х殑鍏磋叮銆?br>Note that this is a very truncated quick-start guide.
娉ㄦ剰杩欏彧鏄竴涓潪甯哥畝鐭殑蹇€熷紑濮嬩粙缁嶃€?br>Now you are ready for more comprehensive details concerning the actions you have just performed.
鐜板湪锛屼綘鍙互鍘讳簡瑙e叧浜庝綘鍒氭墠鎵ц鐨勫姩浣滅殑鏇村鐨勭粏鑺傚唴瀹广€?br>Check out the Maven Getting Started Guide.
鏌ョ湅Maven鍏ラ棬鎸囧崡銆?/p>