Apache Maven 工程模版

2022-08-30 16:54 更新

Maven 使用原型(Archetype)概念為用戶提供了大量不同類型的工程模版(614 個(gè))。Maven 使用下面的命令幫助用戶快速創(chuàng)建 java 項(xiàng)目。

mvn archetype:generate

什么是原型?

原型是一個(gè) Maven 插件,它的任務(wù)是根據(jù)模板創(chuàng)建一個(gè)項(xiàng)目結(jié)構(gòu)。我們將使用 quickstart 原型插件創(chuàng)建一個(gè)簡(jiǎn)單的 java 應(yīng)用程序。


使用工程模板

讓我們打開命令控制臺(tái),跳轉(zhuǎn)到 C:\ > MVN 目錄并執(zhí)行以下 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 選擇默認(rèn)選項(xiàng) (203:maven-archetype-quickstart)

Maven 將詢問(wèn)原型的版本

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 選擇默認(rèn)選項(xiàng) (6:maven-archetype-quickstart:1.1)

Maven 將詢問(wèn)工程細(xì)節(jié)。按要求輸入工程細(xì)節(jié)。如果要使用默認(rèn)值則直接按 Enter 鍵。你也可以輸入自己的值。

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 將要求確認(rèn)工程細(xì)節(jié)。按 enter 或按 Y

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

現(xiàn)在 Maven 將開始創(chuàng)建工程結(jié)構(gòu),顯示如下:

[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:\MVN\health
[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] -----------------------------------------------------------------------

創(chuàng)建的項(xiàng)目

現(xiàn)在轉(zhuǎn)到 C:\ > MVN 目錄。你會(huì)看到一個(gè)名為 health 的 java 應(yīng)用程序項(xiàng)目,就像在項(xiàng)目創(chuàng)建的時(shí)候建立的 artifactId 名稱一樣。 Maven 將創(chuàng)建一個(gè)有標(biāo)準(zhǔn)目錄布局的工程,如下所示:


創(chuàng)建 POM.xml

Maven 為工程生成一個(gè) 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>

創(chuàng)建 App.java

Maven 生成一個(gè) 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!" );
    }
}

創(chuàng)建 AppTest.java

Maven 生成一個(gè) 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 );
  }
}  

就這樣?,F(xiàn)在你可以看到 Maven 的強(qiáng)大之處。你可以用 maven 簡(jiǎn)單的命令創(chuàng)建任何類型的工程,并且可以啟動(dòng)您的開發(fā)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)