Maven 自定義archeType

1. 什麼是archeType

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

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

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

2. 建立archetype

假如本身已經有了一個maven項目,想給該項目建立一個archeType模板。bash

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

mvn archetype:create-from-project 
複製代碼

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

3. 生成archetype模板

cd target/generated-sources/archetype/

而後執行 mvn install 
複製代碼

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

mvn archetype:crawl
複製代碼

來看一看它裏面的內容:ui

[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模板中建立項目。spa

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
複製代碼

項目建立成功!3d

固然,也能夠使用IDEA來幫咱們用圖形界面使用archeType模板建立項目:

圖1
圖2
圖3

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



若是你喜歡個人文章,那麻煩請關注個人公衆號,公衆號重點分析架構師技術,該公衆號還處於初始階段,謝謝你們的支持。

關注公衆號,回覆 java架構獲取架構視頻資源(後期還會分享不一樣的優質資源噢)。
相關文章
相關標籤/搜索