`
zscomehuyue
  • 浏览: 402457 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用Maven2快速创建项目

阅读更多
使用Maven2快速创建项目
源地址:http://www.intellij.org.cn/portal/index.php?option=com_content&task=view&id=26&Itemid=28
1、构建单个工程
Maven2在项目管理方面影响越来越大,很多项目都使用Maven2。下面我们就介绍如何使用Maven2快速创建项目原型,然后在此基础上进行项目开发。下面我们就看一下如何使用Maven2创建典型的三类项目:
普通的Java项目,如基础包等:
   mvn archetype:create   -DgroupId=com.yourcompany  -DartifactId=myproject  -DarchetypeArtifactId=maven-archetype-quickstart
普通的Web项目,如一个Web项目:
    mvn archetype:create  -DgroupId=com.yourcompany -DartifactId=myproject  -DarchetypeArtifactId=maven-archetype-webapp
Appfuse中的Struts 2.0项目:
    mvn archetype:create -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=appfuse-basic-struts -DremoteRepositories=http://static.appfuse.org/repository -DarchetypeVersion=2.0-m4-SNAPSHOT -DgroupId=com.mycompany -DartifactId=myproject
使用上述命令后,你可以很快捷地生成你想要的项目原型。接下来我们只需使用下面命令,生成IDEA的项目文件,然后打开就可以在IDEA下进行项目开发啦。
     mvn idea:idea -DdownloadSources=true -DdownloadJavadocs=true -DjdkLevel=1.5
当pom.xml文件发生变化时,我们只需使用下面命令重新生成module文件即可,新生成的module文件会和原来module文件进行很好的合并,通常是依赖的package发生变化啦。
    mvn idea:module

2、构建Maven父子工程

1. 先单独构建各模块为一个独立的项目。
jar项目:
Dos代码

   1. mvn archetype:create -DgroupId=org.sonatype.mavenbook.weather -DartifactId=simple-weather 



war项目:
Dos代码

   1. mvn archetype:create -DgroupId=org.sonatype.mavenbook.web -DartifactId=simple-webapp  
   2. -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId=maven-archetype-webapp 



2. 构建父项目,并将各子模块文件拷贝到父项目路径下。
Dos代码

   1. mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch06 -DartifactId=simple-parent  
   2. -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId=maven-archetype-webapp 



3. 修改父项目pom.xml与各子模块pom.xml配置对应关系。

父项目的pom.xml

<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom </packaging>
<version>1.0</version>
<name>simple-parent Maven Webapp</name>
<url>http://maven.apache.org</url>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules >



子项目的pom.xml

<parent>
        <groupId>com.your-company.xxxx</groupId>
        <artifactId>xxxx</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
</parent>

4. 在父项目路径下执行:mvn eclipse:m2eclipse

5.在配有m2eclipse 插件的eclipse中导入父项目路径下的项目,导入为m2eclipse project。

------------------------------------------------------------------------------------------------------------------
mvn  archetype:create -DgroupId=org.cjj.site -DartifactId=org.cjj.site  -DpackageName=org.cjj.site -DarchetypeArtifactId=maven-archetype-webapp
重要的
Script代码
archetypeArtifactId
不是吗?
但是,当我们创建spring项目或者struts项目,到哪里去查他们的archetypeArtifactId呢?
我google了一下,发现一个好资源:http://cwiki.apache.org/confluence/display/WW/Struts+2+Maven+Archetypes
呵呵,今天发现原来可以这样:
Script代码
mvn archetype:generate
接下来的事情,很舒服,选择一个类别吧.
Java代码
E:\workspace>mvn archetype:generate
[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
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating  a web application with Hibernate, Spring and Tapestry 4)
5: internal -> appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)
6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)
7: internal -> appfuse-modular-spring (AppFuse archetype for creating  a modular application with Hibernate, Spring and Spring MVC)
8: internal -> appfuse-modular-struts (AppFuse archetype for creating  a modular application with Hibernate, Spring and Struts 2)
9: internal -> appfuse-modular-tapestry (AppFuse archetype for  creating a modular application with Hibernate, Spring and Tapestry 4)
10: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application)
11: internal -> maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
    15: internal -> maven-archetype-quickstart ()
16: internal -> maven-archetype-site-simple (A simple site generation project)
17: internal -> maven-archetype-site (A more complex site project)
18: internal -> maven-archetype-webapp (A simple Java web application)
19: internal -> jini-service-archetype (Archetype for Jini service project creation)
20: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype)
21: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype)
22: internal -> softeu-archetype-jsf (JSF+Facelets Archetype)
23: internal -> jpa-maven-archetype (JPA application)
24: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype)
25: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archetype)
26: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype)
27: internal -> maven-archetype-har (Hibernate Archive)
28: internal -> maven-archetype-sar (JBoss Service Archive)
29: internal -> wicket-archetype-quickstart (A simple Apache Wicket project)
30: internal -> scala-archetype-simple (A simple scala project)
31: internal -> lift-archetype-blank (A blank/empty liftweb project)
32: internal -> lift-archetype-basic (The basic (liftweb) project)
33: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
34: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
35: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
36: internal -> myfaces-archetype-helloworld (A simple archetype using MyFaces)
37: internal -> myfaces-archetype-helloworld-facelets (A simple archetype using MyFaces and facelets)
38: internal -> myfaces-archetype-trinidad (A simple archetype using Myfaces and Trinidad)
39: internal -> myfaces-archetype-jsfcomponents (A simple archetype for create custom JSF components using MyFaces)
40: internal -> gmaven-archetype-basic (Groovy basic archetype)
41: internal -> gmaven-archetype-mojo (Groovy mojo archetype)
Choose a number:  (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41)  15: :
选择41个类型,有你所要的吗?选择一个数字即可.
下面就剩下你根据提示来填空了.




将maven2创建的web项目转为myEclipse项目
关键字: maven2, myeclipse

在配置好maven2的基础上:

1.maven2创建web工程:

mvn archetype:create -DgroupId=com.myWork -DartifactId=Work -DarchetypeArtifactId=maven-archetype-webapp

2.将Maven项目转为Eclipse项目,具体操作为将dos命令窗口切换到Maven项目的目录下,

输入命令: mvn eclipse:eclipse
3. 进入eclipse,将这个项目导入工作空间
4. 在该项目上点右键Maven->Enable
5. 在该项目上点右键Build Path->Configure Build Path->Java Build Path->Libraries->去掉Maven添加的变量路径
6. 在该项目上点右键MyEclipse->Add Web Capabilities->修改Web root地址(点【浏览】按钮指定为当前工作空间下的src/main/webapp文件夹)
7. 配置完毕,可以用MyEclipse插件配置服务器等开始正常开发,自动部署,调试等操作了。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics