`

Maven 试用手记----开始一个新的项目并编译和测试

阅读更多

开始一个新的项目并编译和测试

      这里将要创建第一个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文件内容如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"    
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    
  4. http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   <modelVersion>4.0.0</modelVersion>  
  6.   <groupId>com.maventest.pro</groupId>  
  7.   <artifactId>my</artifactId>  
  8.   <packaging>jar</packaging>  
  9.   <version>1.0-SNAPSHOT</version>  
  10.   <name>Maven Quick Start Archetype</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   <dependencies>  
  13.     <dependency>  
  14.       <groupId>junit</groupId>  
  15.       <artifactId>junit</artifactId>  
  16.       <version>3.8.1</version>  
  17.       <scope>test</scope>  
  18.     </dependency>  
  19.   </dependencies>  
  20. </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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics