開始一個新的項目并編譯和測試
這里將要創建第一個Maven項目。我們將要用到的是Maven的原型機制(archetype mechanism)原型被定義為一個在所有其他性質相同的事物的原始式樣或模型(archetype is defined as an original pattern or model from which all other things of the same kind are made)在Maven中,一個原型是一個已經被使用者按需剪裁組合的Maven項目模板,下面我們就來看一下Maven的原型機制是如何工作的。
ps:如果想了解什么是Archetypes請訪問:
http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
下面來建立一個最簡單的項目、執行下面的語句:
mvn archetype:create -DgroupId=com.maventest.pro -DartifactId=my
執行上面的語句之前請保證你已經聯入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>
一旦你執行后、會注意到發生了幾件事情
1、在MavenTest目錄下出現了一個名為my的新項目工程。而且里面包含一個名字為pom.xml
的文件。pom.xml和src是同級目錄。
my
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- maventest
| `-- pro
| `-- App.java
`-- test
`-- java
`-- com
`-- maventest
`-- pro
`-- AppTest.java
這個pom.xml文件內容如下:
<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>
這里我們看到兩個目錄分別為應用程序源和測試源,這是個標準的Maven項目布局。而這個結構布局通常都是手工創建的。
[faq]
Q:什么是POM?
A:Project Object Model 項目對象模型、Maven的基本單元。詳細了解訪問:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
2、就是Maven在首次運行的時候要連接Internet獲取相關的文件。
pom.xml文件中的標簽元素說明:
·project 這是所有pom.xml的頂層
·modelVersion 這個元素指出POM現在使用的對象模型版本,除非開發者強制性的為了
確保穩定性而進行的必要改變、一般都是不發生變化的。
·groupId 這個就是指出被被創建項目的唯一標識、通常使用域名。
·artifactId 這個元素指出了生成項目時的唯一主要的的名字、通常這個名字被作為
Jar的名字。
·packaging 打包類型 (e.g. JAR, WAR, EAR, etc.). 這個不僅僅意味著打包的產品類型
也指出了一個建造過程中的詳細生命周期。
·version 這個元素指出在項目被生成的時候的版本,通常你所看到的
SNAPSHOT指的是開發進行的狀態
·name 這個元素指出項目相識的名字、通常被用在Maven生成的文檔中
·url 這個元素指出在哪里可以找到這個項目的站點、通常也被用到Maven的
文檔中。
·description 這個元素為你的項目提供一個基本的描述,通常被用在Maven生成的文
檔中。
下面我們就項目進行測試和編譯
編譯:
執行: 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] ---------------------------------------------------------------------------
測試:
執行: 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] ----------------------------------------------------------------------------
這個時候觀察一下my 下的目錄結構。會發現生成了target 目錄,這個就是Maven對程序進行編譯和測試而生成的目標文件夾。
請注意這里compile和test是分別對兩個類進行編譯的。compile是對App.java進行編譯,而test是對AppTest.java進行編譯和測試。
如果單單是想編譯AppTest.java的話可以使用下面的命令:
mvn test-compile
后面我講下如何在eclipse導入Maven構建的項目,待續…………
前篇:Maven 安裝
posted on 2006-01-06 09:39
水狐 閱讀(20945)
評論(4) 編輯 收藏 所屬分類:
Maven