Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538380
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-09 13:11:02

maven创建web项目的命令

  1. mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id}
  2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

P.S 替换 “{packaging.path}” 和“{project-id}” 的值为你想要的值

以下是一个具体的实例


1. mvn archetype:generate

首先在命令行下切换到你想要创建项目的目录,执行以下命令:

  1. N:\>cd workshop

  2. N:\workshop> mvn archetype:generate -DgroupId=net.cublog.jersey -DartifactId=helloworldjersey -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
你会看到如下输出:
  1. N:\workshop> mvn archetype:generate -DgroupId=net.cublog.jersey -DartifactId=hel
  2. loworldjersey -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=fa
  3. lse
  4. [INFO] Scanning for projects...
  5. [INFO]
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] Building Maven Stub Project (No POM) 1
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO]
  10. [INFO] >>> maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom >>
  11. >
  12. [INFO]
  13. [INFO] <<< maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom <<
  14. <
  15. [INFO]
  16. [INFO] --- maven-archetype-plugin:2.1:generate (default-cli) @ standalone-pom --
  17. -
  18. [INFO] Generating project in Batch mode
  19. [INFO] -------------------------------------------------------------------------
  20. ---
  21. [INFO] Using following parameters for creating project from Old (1.x) Archetype:
  22. maven-archetype-webapp:1.0
  23. [INFO] -------------------------------------------------------------------------
  24. ---
  25. [INFO] Parameter: groupId, Value: net.cublog.jersey
  26. [INFO] Parameter: packageName, Value: net.cublog.jersey
  27. [INFO] Parameter: package, Value: net.cublog.jersey
  28. [INFO] Parameter: artifactId, Value: helloworldjersey
  29. [INFO] Parameter: basedir, Value: N:\workshop
  30. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
  31. [INFO] project created from Old (1.x) Archetype in dir: N:\workshop\helloworldje
  32. rsey
  33. [INFO] ------------------------------------------------------------------------
  34. [INFO] BUILD SUCCESS
  35. [INFO] ------------------------------------------------------------------------
  36. [INFO] Total time: 8.325s
  37. [INFO] Finished at: Wed Nov 23 11:22:35 CST 2011
  38. [INFO] Final Memory: 11M/103M
  39. [INFO] ------------------------------------------------------------------------
  40. N:\workshop>

2. Maven生成的Web项目的结构

Maven 会生成“Maven Standard Web Application Directory Layout“. See following diagram :

A new super clean “web.xml” file is created under “N:\workshop\helloworldjersey\src\main\webapp\WEB-INF” folder.

File : web.xml

  1. <!DOCTYPE web-app PUBLIC
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3.  "" >
  4.  
  5. <web-app>
  6.   <display-name>Archetype Created Web Application</display-name>
  7. </web-app>

 3. pom.xml


Refer to the generated pom.xml file, the tag “packaging” is “war“, when build with Maven, this project will group it into a war file for deployment.

File : pom.xml

  1. <project xmlns=""
  2.   xmlns:xsi=""
  3.   xsi:schemaLocation="
  4.   ">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>com.mkyong.core</groupId>
  7.   <artifactId>mkyongweb-core</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>1.0-SNAPSHOT</version>
  10.   <name>mkyongweb-core Maven Webapp</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.   <build>
  21.     <finalName>mkyongweb-core</finalName>
  22.   </build>
  23. </project>

Done. Maven generated a standard web application project structure and a simple web.xml file.

4. mvn eclipse:eclipse -Dwtpversion=2.0

将maven生成的java项目转化为支持eclipse的java项目只需要执行以下命令 :

  1. mvn eclipse:eclipse

对于web项目,你需要额外的参数才能够让它支持Eclipse’s wtp, 所以为了将maven生成的web项目转化为支持eclipse的项目,需要执行以下命令 :
  1. mvn eclipse:eclipse -Dwtpversion=2.0


5. Eclipse WTP

Standard Eclipse’s “.classpath” and “.project” files are created. And you will noticed a new “.setting” folder is created and inside contains “org.eclipse.wst.common.component” and “org.eclipse.wst.common.project.facet.core.xml“, both files for WTP or Faces support in Eclipse.


6. Import into Eclipse IDE

Now, you have everything what a Eclipse web application want, so, you can start import your Maven based web application into Eclipse IDE.

Steps :
In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.


------------------------------------------------------------------------------
导入后需要右击项目--maven--enable maven dependence
有可能由于eclipse,wtp的版本不一样,在项目名前面显示一个红叉。
可以通过eclipse创建一个动态网站项目,然后将.setting文件夹中的文件进行替换。

谢谢访问!

阅读(4386) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~