Maven 自定義archeType

1. 什麼是archeType

我們在創建maven項目的時候,你會發現有這麼多的apache提供的模板。


或者使用mvn archetype:generate命令來快速創建maven項目,也會有很多個選項,讓你選擇模板序號。那每個模板之間有什麼區別呢?

每個模板裏其實就是附帶不同的依賴和插件。一般在公司私服裏都會有屬於本公司的一套archeType模板,裏面有着調試好的項目用到的依賴包和版本號。

2. 創建archetype

假如自己已經有了一個maven項目,想給該項目創建一個archeType模板。

cd 到項目根目錄下執行(pom.xml同級目錄)。

mvn archetype:create-from-project 

此時會在項目target下生成這些文件:

3. 生成archetype模板

先  cd target/generated-sources/archetype/

然後執行 mvn install 

執行成功後,執行crawl命令,在本地倉庫的根目錄生成archetype-catalog.xml骨架配置文件:

mvn archetype:crawl

來看一看它裏面的內容:

[fantj@lalala repository]$ cat archetype-catalog.xml 
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>com.fantj</groupId>
      <artifactId>my-self-defind-archtype-archetype</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <description>my-self-defind-archtype</description>
    </archetype>
  </archetypes>
</archetype-catalog>

4. 使用archetype模板

執行mvn archetype:generate -DarchetypeCatalog=local從本地archeType模板中創建項目。

 mvn archetype:generate -DarchetypeCatalog=local

然後會讓你選擇模板序號和groupId``artifactId``versionpackage信息:

Choose archetype:
1: local -> com.fantj:my-self-defind-archtype-archetype (my-self-defind-archtype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': com.fantj
Define value for property 'artifactId': my-self-defind-archetype-test
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' com.fantj: : 
Confirm properties configuration:
groupId: com.fantj
artifactId: my-self-defind-archetype-test
version: 1.0-SNAPSHOT
package: com.fantj
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: my-self-defind-archtype-archetype:0.0.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.fantj
[INFO] Parameter: artifactId, Value: my-self-defind-archetype-test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.fantj
[INFO] Parameter: packageInPathFormat, Value: com/fantj
[INFO] Parameter: package, Value: com.fantj
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.fantj
[INFO] Parameter: artifactId, Value: my-self-defind-archetype-test
[INFO] Project created from Archetype in dir: /home/fantj/IdeaProjects/maven-tutorial/my-self-defind-archetype-test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

項目創建成功!

當然,也可以使用IDEA來幫我們用圖形界面使用archeType模板創建項目:


後面的就與創建普通項目相同了,不做演示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章