使用Maven模板創建項目
在本教程中,我們將向你展示如何使用mvn archetype:generate從現有的Maven模板列表中生成項目。在Maven 3.3.3,有超過1000+個模板,Maven 團隊已經過濾掉一些無用的模板。
通常情況下,我們只需要使用下面的兩個模板:
- maven-archetype-webapp – Java Web Project (WAR)
- maven-archetype-quickstart – Java Project (JAR)
1. Maven 1000+ 模板
如果鍵入命令mvn archetype:generate,1000 +模板會被提示在屏幕上,你沒有辦法看到它,或者選擇什麼。爲了解決這個問題,輸出模板列表,像這樣保存爲文本文件:
C:\worksp> mvn archetype:generate > templates.txt //waiting few seconds,then exits
Press CTRL + C
C:\worksp> vim templates.txt
2. Maven archetype:generate
步驟來指導你如何從現有 Spring-Hibernate 模板來構建Web項目:
2.1 列出 Maven 的模板:
C:\worksp> mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[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: remote -> am.ik.archetype:maven-reactjs-blank-archetype (Blank Project for React.js)
2: remote -> am.ik.archetype:msgpack-rpc-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
3: remote -> am.ik.archetype:mvc-1.0-blank-archetype (MVC 1.0 Blank Project)
4: remote -> am.ik.archetype:spring-boot-blank-archetype (Blank Project for Spring Boot)
5: remote -> am.ik.archetype:spring-boot-docker-blank-archetype (Docker Blank Project for Spring Boot)
6: remote -> am.ik.archetype:spring-boot-gae-blank-archetype (GAE Blank Project for Spring Boot)
7: remote -> am.ik.archetype:spring-boot-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
8: remote -> at.chrl.archetypes:chrl-spring-sample (Archetype for Spring Vaadin Webapps)
2.2 選擇數字 「314」 來使用 ml.rugal.archetype:springmvc-spring-hibernate 模板,並填寫詳細信息:
注意,這個數字314可能在您的環境有所不同。尋找正確的數字應該看看在上面的步驟1中列出的技術。
1445: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 674: 477
Choose ml.rugal.archetype:springmvc-spring-hibernate version:
1: 0.1
2: 0.2
3: 0.3
4: 0.4
5: 0.5
6: 0.6
Choose a number: 6:
Downloading: https://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-s
pring-hibernate/0.6/springmvc-spring-hibernate-0.6.jar
Downloaded: https://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-sp
ring-hibernate/0.6/springmvc-spring-hibernate-0.6.jar (30 KB at 6.8 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-s
pring-hibernate/0.6/springmvc-spring-hibernate-0.6.pom
Downloaded: https://repo.maven.apache.org/maven2/ml/rugal/archetype/springmvc-sp
ring-hibernate/0.6/springmvc-spring-hibernate-0.6.pom (4 KB at 5.3 KB/sec)
Define value for property 'groupId': : com.yiibai.web
Define value for property 'artifactId': : myweb
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.yiibai.web: :
Confirm properties configuration:
groupId: com.yiibai.web
artifactId: myweb
version: 1.0-SNAPSHOT
package: com.yiibai.web
Y: : y
[INFO] -------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: springmvc
-spring-hibernate:0.6
[INFO] -------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.yiibai.web
[INFO] Parameter: artifactId, Value: myweb
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.yiibai.web
[INFO] Parameter: packageInPathFormat, Value: com/yiibai/web
[INFO] Parameter: package, Value: com.yiibai.web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.yiibai.web
[INFO] Parameter: artifactId, Value: myweb
[INFO] project created from Archetype in dir: C:\worksp\myweb
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:07 min
[INFO] Finished at: 2015-11-03T07:10:56+08:00
[INFO] Final Memory: 16M/176M
[INFO] ------------------------------------------------------------------------
它會生成以下項目文件夾和文件。
圖片: 生成Eclipse IDE中的項目結構。
注意,要導入項目到Eclipse中,鍵入命令mvn eclipse:eclipse,並導入它作爲一個正常的項目:
c:\worksp> cd myweb
c:\worksp>myweb> mvn eclipse:eclipse
3. 更多示例
如果您知道哪個 archetypeArtifactId 使用,只需跳過交互模式命令:
3.1 maven-archetype-quickstart (Java Project)
$ mvn archetype:generate -DgroupId=com.yiibai.core -DartifactId=ProjectName -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
3.2 maven-archetype-webapp (Java Web Project)
$ mvn archetype:generate -DgroupId=com.yiibai.web -DartifactId=ProjectName -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false