開始一個(gè)新的項(xiàng)目并編譯和測試
這里將要?jiǎng)?chuàng)建第一個(gè)Maven項(xiàng)目。我們將要用到的是Maven的原型機(jī)制(archetype mechanism)原型被定義為一個(gè)在所有其他性質(zhì)相同的事物的原始式樣或模型(archetype is defined as an original pattern or model from which all other things of the same kind are made)在Maven中,一個(gè)原型是一個(gè)已經(jīng)被使用者按需剪裁組合的Maven項(xiàng)目模板,下面我們就來看一下Maven的原型機(jī)制是如何工作的。
ps:如果想了解什么是Archetypes請?jiān)L問:
http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
下面來建立一個(gè)最簡單的項(xiàng)目、執(zhí)行下面的語句:
mvn archetype:create -DgroupId=com.maventest.pro -DartifactId=my
執(zhí)行上面的語句之前請保證你已經(jīng)聯(lián)入Internet
C:\>md MavenTest
C:\>cd MavenTest
C:\MavenTest>mvn archetype:create -DgroupId=com.maventest.pro -DartifactId=my
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] org.codehaus.mojo: checking for updates from central
[INFO] artifact org.apache.maven.plugins:maven-archetype-plugin: checking for up
dates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archet
ype-plugin/1.0-alpha-3/maven-archetype-plugin-1.0-alpha-3.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugin
-parent/2.0-beta-1/maven-plugin-parent-2.0-beta-1.pom
6K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archet
ype-plugin/1.0-alpha-3/maven-archetype-plugin-1.0-alpha-3.jar
6K downloaded
[INFO] -------------------------------------------------------------------------
---
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] -------------------------------------------------------------------------
---
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archetype-core
/1.0-alpha-3/maven-archetype-core-1.0-alpha-3.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archetype/1.0-
alpha-3/maven-archetype-1.0-alpha-3.pom
638b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven/2.0-beta-1/mav
en-2.0-beta-1.pom
6K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.3/plexus-util
s-1.0.3.pom
970b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-root/1.0.3/plexus-root-
1.0.3.pom
5K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
lpha-6/plexus-container-default-1.0-alpha-6.pom
916b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-containers/1.0.2/plexus
-containers-1.0.2.pom
471b downloaded
Downloading: http://repo1.maven.org/maven2/classworlds/classworlds/1.1-alpha-2/c
lassworlds-1.1-alpha-2.pom
3K downloaded
Downloading: http://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
145b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.2/plexus-util
s-1.0.2.pom
740b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-model/2.0-beta
-1/maven-model-2.0-beta-1.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-velocity/1.0/plexus-vel
ocity-1.0.pom
943b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-components/1.0/plexus-c
omponents-1.0.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-root/1.0/plexus-root-1.
0.pom
4K downloaded
Downloading: http://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.2
/commons-logging-1.0.2.pom
810b downloaded
Downloading: http://repo1.maven.org/maven2/commons-collections/commons-collectio
ns/2.0/commons-collections-2.0.pom
171b downloaded
Downloading: http://repo1.maven.org/maven2/velocity/velocity/1.4/velocity-1.4.po
m
2K downloaded
Downloading: http://repo1.maven.org/maven2/velocity/velocity-dep/1.4/velocity-de
p-1.4.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/log4j/log4j/1.2.8/log4j-1.2.8.pom
145b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
lpha-2/plexus-container-default-1.0-alpha-2.pom
170b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-artifact/2.0-b
eta-1/maven-artifact-2.0-beta-1.pom
711b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api/2.0
-beta-1/maven-plugin-api-2.0-beta-1.pom
615b downloaded
Downloading: http://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.2
/commons-logging-1.0.2.jar
25K downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archetype-core
/1.0-alpha-3/maven-archetype-core-1.0-alpha-3.jar
14K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-velocity/1.0/plexus-velocity-1.0.jar
4K downloaded
Downloading: http://repo1.maven.org/maven2/velocity/velocity-dep/1.4/velocity-dep-1.4.jar
505K downloaded
Downloading: http://repo1.maven.org/maven2/velocity/velocity/1.4/velocity-1.4.jar
352K downloaded
Downloading: http://repo1.maven.org/maven2/commons-collections/commons-collectio
ns/2.0/commons-collections-2.0.jar
88K downloaded
Downloading: http://repo1.maven.org/maven2/log4j/log4j/1.2.8/log4j-1.2.8.jar
344K downloaded
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] **************************************************************
[INFO] Starting Jakarta Velocity v1.4
[INFO] RuntimeInstance initializing.
[INFO] Default Properties File: org\apache\velocity\runtime\defaults\velocity.pr
operties
[INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.
resource.ResourceManagerImpl)
[INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLo
aderResourceLoader
[INFO] ClasspathResourceLoader : initialization starting.
[INFO] ClasspathResourceLoader : initialization complete.
[INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.
ResourceCacheImpl)
[INFO] Default ResourceManager initialization complete.
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
[INFO] Created: 20 parsers.
[INFO] Velocimacro : initialization starting.
[INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm
[ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any
resource loader.
[INFO] Velocimacro : error using VM library template VM_global_library.vm : org
.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'V
M_global_library.vm'
[INFO] Velocimacro : VM library template macro registration complete.
[INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates
[INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT
replace previous VM definitions
[INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be glob
al in scope if allowed.
[INFO] Velocimacro : messages on : VM system will output logging messages
[INFO] Velocimacro : autoload off : VM system will not automatically reload glo
bal library macros
[INFO] Velocimacro : initialization complete.
[INFO] Velocity successfully started.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: com.maventest.pro
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc
hetype-quickstart/1.0-alpha-3/maven-archetype-quickstart-1.0-alpha-3.jar
5K downloaded
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating Archetype: maven-archetype-quicks
tart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.maventest.pro
[INFO] Parameter: outputDirectory, Value: C:\MavenTest\my
[INFO] Parameter: packageName, Value: com.maventest.pro
[INFO] Parameter: package, Value: com.maventest.pro
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: my
[INFO] ResourceManager : found archetype-resources/pom.xml with loader org.codeh
aus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] ********************* End of debug info from resources from generated POM
***********************
[INFO] ResourceManager : found archetype-resources/src/main/java/App.java with l
oader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] ResourceManager : found archetype-resources/src/test/java/AppTest.java wi
th loader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] Archetype created in dir: C:\MavenTest\my
[INFO] ----------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 4 minutes 39 seconds
[INFO] Finished at: Fri Jan 06 07:29:27 CST 2006
[INFO] Final Memory: 4M/7M
[INFO] ----------------------------------------------------------------------------
C:\MavenTest>
一旦你執(zhí)行后、會(huì)注意到發(fā)生了幾件事情
1、在MavenTest目錄下出現(xiàn)了一個(gè)名為my的新項(xiàng)目工程。而且里面包含一個(gè)名字為pom.xml
的文件。pom.xml和src是同級(jí)目錄。
my
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- maventest
| `-- pro
| `-- App.java
`-- test
`-- java
`-- com
`-- maventest
`-- pro
`-- AppTest.java
這個(gè)pom.xml文件內(nèi)容如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maventest.pro</groupId>
<artifactId>my</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
這里我們看到兩個(gè)目錄分別為應(yīng)用程序源和測試源,這是個(gè)標(biāo)準(zhǔn)的Maven項(xiàng)目布局。而這個(gè)結(jié)構(gòu)布局通常都是手工創(chuàng)建的。
[faq]
Q:什么是POM?
A:Project Object Model 項(xiàng)目對(duì)象模型、Maven的基本單元。詳細(xì)了解訪問:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
2、就是Maven在首次運(yùn)行的時(shí)候要連接Internet獲取相關(guān)的文件。
pom.xml文件中的標(biāo)簽元素說明:
·project 這是所有pom.xml的頂層
·modelVersion 這個(gè)元素指出POM現(xiàn)在使用的對(duì)象模型版本,除非開發(fā)者強(qiáng)制性的為了
確保穩(wěn)定性而進(jìn)行的必要改變、一般都是不發(fā)生變化的。
·groupId 這個(gè)就是指出被被創(chuàng)建項(xiàng)目的唯一標(biāo)識(shí)、通常使用域名。
·artifactId 這個(gè)元素指出了生成項(xiàng)目時(shí)的唯一主要的的名字、通常這個(gè)名字被作為
Jar的名字。
·packaging 打包類型 (e.g. JAR, WAR, EAR, etc.). 這個(gè)不僅僅意味著打包的產(chǎn)品類型
也指出了一個(gè)建造過程中的詳細(xì)生命周期。
·version 這個(gè)元素指出在項(xiàng)目被生成的時(shí)候的版本,通常你所看到的
SNAPSHOT指的是開發(fā)進(jìn)行的狀態(tài)
·name 這個(gè)元素指出項(xiàng)目相識(shí)的名字、通常被用在Maven生成的文檔中
·url 這個(gè)元素指出在哪里可以找到這個(gè)項(xiàng)目的站點(diǎn)、通常也被用到Maven的
文檔中。
·description 這個(gè)元素為你的項(xiàng)目提供一個(gè)基本的描述,通常被用在Maven生成的文
檔中。
下面我們就項(xiàng)目進(jìn)行測試和編譯
編譯:
執(zhí)行: mvn compile
C:\MavenTest\my>mvn compile
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype
[INFO] task-segment: [compile]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] ----------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 06 09:08:13 CST 2006
[INFO] Final Memory: 2M/5M
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 4 minutes 39 seconds
[INFO] Finished at: Fri Jan 06 07:29:27 CST 2006
[INFO] Final Memory: 4M/7M
[INFO] ---------------------------------------------------------------------------
測試:
執(zhí)行: mvn test
C:\MavenTest\my>mvn test
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype
[INFO] task-segment: [test]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test]
[INFO] Setting reports dir: C:\MavenTest\my\target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
[surefire] Running com.maventest.pro.AppTest
[surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.015 sec
Results :
[surefire] Tests run: 1, Failures: 0, Errors: 0
[INFO] ----------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Jan 06 09:07:45 CST 2006
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------------
這個(gè)時(shí)候觀察一下my 下的目錄結(jié)構(gòu)。會(huì)發(fā)現(xiàn)生成了target 目錄,這個(gè)就是Maven對(duì)程序進(jìn)行編譯和測試而生成的目標(biāo)文件夾。
請注意這里compile和test是分別對(duì)兩個(gè)類進(jìn)行編譯的。compile是對(duì)App.java進(jìn)行編譯,而test是對(duì)AppTest.java進(jìn)行編譯和測試。
如果單單是想編譯AppTest.java的話可以使用下面的命令:
mvn test-compile
后面我講下如何在eclipse導(dǎo)入Maven構(gòu)建的項(xiàng)目,待續(xù)…………
前篇:Maven 安裝
posted on 2006-01-06 09:39
水狐 閱讀(20945)
評(píng)論(4) 編輯 收藏 所屬分類:
Maven