1. 程式人生 > >【轉】Maven項目模板

【轉】Maven項目模板

stc web res sts aps 代碼 nis 源文件 contain

http://www.yiibai.com/maven/maven_project_templates.html

maven 使用 Archetype 概念為用戶提供不同類型的項目模板,它是一個非常大的列表(614個數字)。 maven 使用下面的命令來幫助用戶快速開始構建一個新的 Java 項目。

mvn archetype:generate

什麽是Archetype

Archetype 是一個 Maven 插件,其任務是按照其模板來創建一個項目結構。在這裏我們將使用 quickstart 原型插件來創建一個簡單的 Java應用程序。

使用項目模板

讓我們打開命令控制臺,進入到 C:\>MVN

目錄,然後執行以下命令 mvn 命令,如下代碼所示:

C:\MVN>mvn archetype:generate 

Maven 開始處理,並按要求選擇所需的原型,執行結果如下圖中所示:

INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: ‘archetype‘.
[INFO] -------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] -------------------------------------------------------------------
[INFO] Preparing archetype:generate
...
600: remote -> org.trailsframework:trails-archetype (-)
601: remote -> org.trailsframework:trails-secure-archetype (-)
602: remote -> org.tynamo:tynamo-archetype (-)
603: remote -> org.wicketstuff.scala:wicket-scala-archetype (-)
604: remote -> org.wicketstuff.scala:wicketstuff-scala-archetype 
Basic setup for a project that combines Scala and Wicket,
depending on the Wicket-Scala project. 
Includes an example Specs test.)
605: remote -> org.wikbook:wikbook.archetype (-)
606: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-glassfish (-)
607: remote -> org.xaloon.archetype:xaloon-archetype-wicket-jpa-spring (-)
608: remote -> org.xwiki.commons:xwiki-commons-component-archetype 
(Make it easy to create a maven project for creating XWiki Components.)
609: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro 
(Make it easy to create a maven project for creating XWiki Rendering Macros.)
610: remote -> org.zkoss:zk-archetype-component (The ZK Component archetype)
611: remote -> org.zkoss:zk-archetype-webapp (The ZK wepapp archetype)
612: remote -> ru.circumflex:circumflex-archetype (-)
613: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
614: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
Choose a number or apply filter 
(format: [groupId:]artifactId, case sensitive contains): 203:

按 Enter 鍵選擇默認選項(203:maven-archetype-quickstart)

Maven會要求原型的特定版本

Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:

按 Enter 鍵選擇默認選項(6:maven-archetype-quickstart:1.1)

Maven會要求填寫項目細節信息。如果要使用默認值可直接按回車。也可以通過輸入自己的值覆蓋它們。

Define value for property ‘groupId‘: : com.companyname.insurance
Define value for property ‘artifactId‘: : health
Define value for property ‘version‘: 1.0-SNAPSHOT:
Define value for property ‘package‘: com.companyname.insurance:

Maven會要求確認項目的細節信息,可按回車鍵或按 Y 來確認。

Confirm properties configuration:
groupId: com.companyname.insurance
artifactId: health
version: 1.0-SNAPSHOT
package: com.companyname.insurance
Y:

現在,Maven將開始創建項目結構,並會顯示如下內容:

[INFO] -----------------------------------------------------------------------
[INFO] Using following parameters for creating project 
from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] -----------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.companyname.insurance
[INFO] Parameter: packageName, Value: com.companyname.insurance
[INFO] Parameter: package, Value: com.companyname.insurance
[INFO] Parameter: artifactId, Value: health
[INFO] Parameter: basedir, Value: C:MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:MVNhealth
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------------------------
[INFO] Total time: 4 minutes 12 seconds
[INFO] Finished at: Fri Jul 13 11:10:12 IST 2012
[INFO] Final Memory: 20M/90M
[INFO] -----------------------------------------------------------------------

創建項目

現在進入到 C:\mvn 目錄。會看到有一個 java 應用程序項目已創建了,它是在創建項目時給出 artifactId 命名:health 。 Maven 將創建一個標準的目錄結構布局,如下圖所示:

技術分享

創建pom.xml

Maven 項目中的生成如下所列出 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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.insurance</groupId>
   <artifactId>health</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>health</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>

創建App.java

Maven 示例生成 Java 源文件,App.java下面列出項目:

位置:C:\>MVN\health\src\main\java\com\companyname\insurance> App.java

package com.companyname.insurance;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

創建 AppTest.java

Maven 實例生成 Java 源測試文件,項目中的 AppTest.java 測試文件如下面列出:

位置: C:\ > MVN > health > src > test > java > com > companyname > insurance > AppTest.java

package com.companyname.insurance;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

就是這樣。現在就可以看到 Maven 的功能了。可以使用 maven 單一命令來創建任何類型的項目並開始開發。

【轉】Maven項目模板