教你如何使用Maven模板生成一个项目

gretaguo 2012-11-06

Maven带有超过40+个Maven模板,让开发者快速开始一个新的Java项目。这一章,演示怎样使用Maven的命令“mvn archetype:generate”生成一个新的Java Web项目。 

下面是使用Maven模板生成项目的步骤。

1、mvnarchetype:generate命令

定位到要生成的项目的目录下,如“D:\workspace\maven-test”。在命令行窗口,执行“mvnarchetype:generate-DarchetypeCatalog=internal”命令以生成一个新的项目。Maven将列出已经存在的40+Maven模板项目供我们选择。在这个例子中,选择19以创建一个简单的Javaweb应用程序项目模板(这里使用的是Maven3.03版本,Maven2可能有所不同):

使用Maven创建项目过程1

D:\workspace\maven-test>mvnarchetype:generate-DarchetypeCatalog=internal

[INFO]Scanningforprojects...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO]BuildingMavenStubProject(NoPOM)1

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]>>>maven-archetype-plugin:2.0:generate(default-cli)@standalone-pom>>>

[INFO]

[INFO]<<<maven-archetype-plugin:2.0:generate(default-cli)@standalone-pom<<<

[INFO]

[INFO]---maven-archetype-plugin:2.0:generate(default-cli)@standalone-pom---

[INFO]GeneratingprojectinInteractivemode

[INFO]Noarchetypedefined.Usingmaven-archetype-quickstart(org.apache.maven.

archetypes:maven-archetype-quickstart:1.0)

Choosearchetype:

1:internal->appfuse-basic-jsf(AppFusearchetypeforcreatingawebapplicationwithHibernate,SpringandJSF)

2:internal->appfuse-basic-spring(AppFusearchetypeforcreatingawebapplicationwithHibernate,SpringandSpringMVC)

3:internal->appfuse-basic-struts(AppFusearchetypeforcreatingawebapplicationwithHibernate,SpringandStruts2)

4:internal->appfuse-basic-tapestry(AppFusearchetypeforcreatingawebapplicationwithHibernate,SpringandTapestry4)

5:internal->appfuse-core(AppFusearchetypeforcreatingajarapplicationwithHibernateandSpringandXFire)

6:internal->appfuse-modular-jsf(AppFusearchetypeforcreatingamodularapplicationwithHibernate,SpringandJSF)

7:internal->appfuse-modular-spring(AppFusearchetypeforcreatingamodularapplicationwithHibernate,SpringandSpringMVC)

8:internal->appfuse-modular-struts(AppFusearchetypeforcreatingamodularapplicationwithHibernate,SpringandStruts2)

9:internal->appfuse-modular-tapestry(AppFusearchetypeforcreatingamodularapplicationwithHibernate,SpringandTapestry4)

10:internal->makumba-archetype(ArchetypeforasimpleMakumbaapplication)11:internal->maven-archetype-j2ee-simple(AsimpleJ2EEJavaapplication)

12:internal->maven-archetype-marmalade-mojo(AMavenplugindevelopmentprojectusingmarmalade)

13:internal->maven-archetype-mojo(AMavenJavaplugindevelopmentproject)

14:internal->maven-archetype-portlet(Asimpleportletapplication)

15:internal->maven-archetype-profiles()

16:internal->maven-archetype-quickstart()

17:internal->maven-archetype-site-simple(Asimplesitegenerationproject)

18:internal->maven-archetype-site(Amorecomplexsiteproject)

19:internal->maven-archetype-webapp(AsimpleJavawebapplication)

20:internal->data-app(AnewDatabinderapplicationwithsourcesandresources.)

21:internal->camel-archetype-component(CreatesanewCamelcomponent)

22:internal->camel-archetype-activemq(CreatesanewCamelprojectthatconfiguresandinteractswithActiveMQ)

23:internal->camel-archetype-java(CreatesanewCamelprojectusingJavaDSL)

24:internal->camel-archetype-scala(CreatesanewCamelprojectusingScalaDSL)

25:internal->camel-archetype-spring(CreatesanewCamelprojectwithaddedSpringDSLsupport)

26:internal->camel-archetype-war(CreatesanewCamelprojectthatdeploystheCamelWebConsole,RESTAPI,andyourroutesasaWAR)

27:internal->jini-service-archetype(ArchetypeforJiniserviceprojectcreation)

28:internal->jbosscc-seam-archetype(MavenArchetypetogenerateaSeamApplication-Documentation)

29:internal->softeu-archetype-seam(JSF+Facelets+SeamArchetype)

30:internal->softeu-archetype-seam-simple(JSF+Facelets+Seam(nopersistence)Archetype)

31:internal->softeu-archetype-jsf(JSF+FaceletsArchetype)

32:internal->jpa-maven-archetype(JPAapplication)

33:internal->spring-osgi-bundle-archetype(Spring-OSGiarchetype)

34:internal->bamboo-plugin-archetype(AtlassianBamboopluginarchetype)

35:internal->confluence-plugin-archetype(AtlassianConfluencepluginarchetype)

36:internal->jira-plugin-archetype(AtlassianJIRApluginarchetype)

37:internal->maven-archetype-har(HibernateArchive)

38:internal->maven-archetype-sar(JBossServiceArchive)

39:internal->wicket-archetype-quickstart(AsimpleApacheWicketproject)

40:internal->quickstart(AsimpleApacheTapestry5Project)

41:internal->scala-archetype-simple(Asimplescalaproject)

42:internal->lift-archetype-blank(Ablank/emptyliftwebproject)

43:internal->lift-archetype-basic(Thebasic(liftweb)project)

44:internal->cocoon-22-archetype-block-plain(http://cocoon.apache.org/2.2/maven-plugins/)

45:internal->cocoon-22-archetype-block(http://cocoon.apache.org/2.2/maven-plugins/)

46:internal->cocoon-22-archetype-webapp(http://cocoon.apache.org/2.2/maven-plugins/)

47:internal->myfaces-archetype-helloworld(AsimplearchetypeusingMyFaces)

48:internal->myfaces-archetype-helloworld-facelets(AsimplearchetypeusingMyFacesandfacelets)

49:internal->myfaces-archetype-trinidad(AsimplearchetypeusingMyfacesandTrinidad)

50:internal->myfaces-archetype-jsfcomponents(AsimplearchetypeforcreatecustomJSFcomponentsusingMyFaces)

51:internal->gmaven-archetype-basic(Groovybasicarchetype)

52:internal->gmaven-archetype-mojo(Groovymojoarchetype)

53:internal->struts2-archetype-starter(Struts2StarterArchetype)

54:internal->tynamo-archetype(CreatesaCRUDwebapplicationbasedonTapestry5andTynamo,amodel-drivenJavawebframework)

55:internal->tellurium-junit-archetype(TelluriumJUnitprojectarchetypefortheTelluriumAutomatedTestingFramework)

56:internal->tellurium-testng-archetype(TelluriumTestNGprojectarchetypefortheTelluriumAutomatedTestingFramework)

57:internal->scalate-archetype-empty(GeneratesaScalateemptywebapplication)

58:internal->scalate-archetype-guice(GeneratesaScalateJogwebapplication)

Chooseanumber:16:

选择19,Maven会自动下载所需的依赖库文件,这可能会耗费一些时间。

2、项目详细信息

接下来,会让填写项目的详细信息:

填写项目详细信息

Definevalueforproperty'groupId'::com.xuejava

Definevalueforproperty'artifactId'::com.xuejavaweb

Definevalueforproperty'version':1.0-SNAPSHOT::1.0

Definevalueforproperty'package':com.xuejava::

完整的过程如下:

项目生成过程2

……

Chooseanumber:16:19

Downloading:http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc

hetype-webapp/1.0/maven-archetype-webapp-1.0.jar

Downloaded:http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arch

etype-webapp/1.0/maven-archetype-webapp-1.0.jar(4KBat1.7KB/sec)

Downloading:http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc

hetype-webapp/1.0/maven-archetype-webapp-1.0.pom

Downloaded:http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arch

etype-webapp/1.0/maven-archetype-webapp-1.0.pom(533Bat0.1KB/sec)

Definevalueforproperty'groupId'::com.xuejava

Definevalueforproperty'artifactId'::com.xuejavaweb

Definevalueforproperty'version':1.0-SNAPSHOT::1.0

Definevalueforproperty'package':com.xuejava::

Confirmpropertiesconfiguration:

groupId:com.xuejava

artifactId:com.xuejavaweb

version:1.0

package:com.xuejava

Y::y

[INFO]----------------------------------------------------------------------------

[INFO]UsingfollowingparametersforcreatingprojectfromOld(1.x)Archetype:

maven-archetype-webapp:RELEASE

[INFO]----------------------------------------------------------------------------

[INFO]Parameter:groupId,Value:com.xuejava

[INFO]Parameter:packageName,Value:com.xuejava

[INFO]Parameter:package,Value:com.xuejava

[INFO]Parameter:artifactId,Value:com.xuejavaweb

[INFO]Parameter:basedir,Value:D:\workspace\maven-test

[INFO]Parameter:version,Value:1.0

[INFO]*********************EndofdebuginfofromresourcesfromgeneratedPOM***********************

[INFO]projectcreatedfromOld(1.x)Archetypeindir:D:\workspace\maven-test\com.xuejavaweb

[INFO]------------------------------------------------------------------------

[INFO]BUILDSUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Totaltime:1:13.922s

[INFO]Finishedat:SunMay2213:51:03CST2011

[INFO]FinalMemory:6M/16M

[INFO]------------------------------------------------------------------------

D:\workspace\maven-test>

创建结束,会生成如下的文件和目录结构:

3、pom.xml

在Maven中,pom.xml文件是项目配置的核心,它包含构建项目所需的主要信息。在上面的示例中,自动生成了pom.xml文件,其中的标签“packaging”为“war”—注意这里不是“jar”。当使用Maven构建项目时,该项目将自动打成war包用来部署。

pom.xml

<projectxmlns="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.0http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.xuejava</groupId>

<artifactId>com.xuejavaweb</artifactId>

<packaging>war</packaging>

<version>1.0</version>

<name>com.xuejavawebMavenWebapp</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>

<build>

<finalName>com.xuejavaweb</finalName>

</build>

</project>

在Maven,完成所有这一切,只需要一个命令,就可以创建标准目录结构,马上开始开发工作 。

转自:http://www.k6567.com

http://www.d9732.com

相关推荐