使用Maven模板建立專案


在本教學中,我們將向你展示如何使用mvn archetype:generate從現有的Maven模板列表中生成專案。在Maven 3.3.3,有超過1000+個模板,Maven 團隊已經過濾掉一些無用的模板。

通常情況下,我們只需要使用下面的兩個模板:

  1. maven-archetype-webapp – Java Web Project (WAR)
  2. 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

參考

  1. Newbie: maven archetype:generate
  2. Guide to Creating Archetypes
  3. Maven Archetype Plugin – Usage
  4. personal site for Rugal Bernstein