大家好,我是文和.今天给大家分享一下,如何用maven快速创建javaWeb项目.
进入到要创建项目的路径(不建议直接创建在eclipse得工作空间)
mvn archetype:generate -DgroupId=org.wenhe.java -DartifactId=wh-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false -DarchetypeCatalog=local
创建结束后会有几个询问,没有特殊要求的话直接"回车"就好了
运行过程如下:
zhihuzheyedeMacBook-Pro:command-text-demo jking$ mvn archetype:generate -DgroupId=org.wenhe.java -DartifactId=wh-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = org.wenhe.java
[INFO] Using property: artifactId = wh-web-app
Define value for property 'version': 1.0-SNAPSHOT: :
[INFO] Using property: package = org.wenhe.java
Confirm properties configuration:
groupId: org.wenhe.java
artifactId: wh-web-app
version: 1.0-SNAPSHOT
package: org.wenhe.java
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/jking/work/JAVA/Maven/command-text-demo
[INFO] Parameter: package, Value: org.wenhe.java
[INFO] Parameter: groupId, Value: org.wenhe.java
[INFO] Parameter: artifactId, Value: wh-web-app
[INFO] Parameter: packageName, Value: org.wenhe.java
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/jking/work/JAVA/Maven/command-text-demo/wh-web-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.443 s
[INFO] Finished at: 2016-11-13T17:12:49+08:00
[INFO] Final Memory: 13M/155M
[INFO] ------------------------------------------------------------------------
zhihuzheyedeMacBook-Pro:command-text-demo jking$
其中:
groupId: 代表组织和整个项目的唯一标志,也就是包名。比如说所有的Maven组件的groupId都是org.apache.maven
artifactId: 具体项目的名称,它于groupId共同确定一个项目在maven repo中的位置,例如,groupId=org.wenhe.app, artifactId=my-project的项目,在maven repo中的位置为:$M2_REPO/org/wenhe/app/my-project
注意: 这里面有一个大坑~ 额~
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
命令行会一直卡在"Generating project in Interactive mode",详见另一篇文章.
现在基于maven 的javaweb项目已经创建完成,
下一步将项目转为eclipse项目
进入刚创建的项目跟路径,敲入下面命令
mvn eclipse:eclipse -Dwtpversion=1.0
zhihuzheyedeMacBook-Pro:wh-web-app jking$ mvn eclipse:eclipse -Dwtpversion=1.0
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building wh-web-app Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) > generate-resources @ wh-web-app >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) < generate-resources @ wh-web-app <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ wh-web-app ---
[INFO] Adding support for WTP version 1.0.
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] File /Users/jking/work/JAVA/Maven/command-text-demo/wh-web-app/.project already exists.
Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed.
[INFO] Wrote Eclipse project for "wh-web-app" to /Users/jking/work/JAVA/Maven/command-text-demo/wh-web-app.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.072 s
[INFO] Finished at: 2016-11-13T17:30:51+08:00
[INFO] Final Memory: 12M/220M
[INFO] ------------------------------------------------------------------------
(也可以不使用这条命令,直接在eclipse中导入maven项目, 但很多时候是导入别人开发好的项目,里面pom.xml文件会引入很多包,eclipse在下载jar文件时,由于网络问题,很多jar包总下载失败,所以建议最好使用命令,先把jar下载在.m2路径下后,并自动转为eclipse项目,再导入eclipse...)
之后就可以导入eclipse了
大功告成~
之后就是添加maven依赖,愉快的编写代码了