1. 程式人生 > 其它 >Spring Boot 自定義Maven Archetype快速建立工程專案

Spring Boot 自定義Maven Archetype快速建立工程專案

一、Maven Archetype是啥

ArchetypeMaven 的工程模板,使用 Archetype 可以快速建立一個統一結構的工程,提高效率。Maven 有一些自帶的 Archetype,但是很多時候不能滿足我們的需求,因此需要自定義 Archetype 以生成滿足我們需求的工程。

本文介紹自定義Archetype的建立方法,以一個工程為基礎,生成 Archetype模板,使用模板快速建立目標工程,涉及三個工程。

  • spring-boot-original: 基礎工程,用這個生成 Archetype 模板工程
  • spring-boot-archetype: 自定義 Archetype
    模板工程
  • spring-boot-target: 使用自定義 Archetype 生成的目標工程

二、自定義Archetype

1、首先找一個已有的工程 spring-boot-original,以此為基礎,目錄結構如下

spring-boot-original
│  .gitignore
│  pom.xml
│  README.md
│  
└─src
    └─main
        ├─java
        │  └─com
        │      └─rkyao
        │          └─spring
        │              └─boot
        │                  └─original
        │                          Application.java
        │                          
        └─resources
                application.properties

2、在 spring-boot-original 的根目錄下執行如下命令,生成 Archetype

mvn clean archetype:create-from-project -Dmaven.test.skip=true

# 上面命令執行時如果報錯,指定maven配置檔案,配置檔案路徑改成自己的
mvn clean archetype:create-from-project --settings D:\\Mysoftware\\apache-maven-3.5.2\\conf\\settings.xml -Dmaven.test.skip=true

target\generated-sources\archetype

目錄會生成 Archetype 的相關內容,把這個目錄下的內容複製出來,放到一個新目錄 spring-boot-archetype 裡,這樣就形成了一個新的模板工程

spring-boot-archetype
│  pom.xml
│  
└─src
    ├─main
    │  └─resources
    │      ├─archetype-resources
    │      │  │  .gitignore
    │      │  │  pom.xml
    │      │  │  README.md
    │      │  │  
    │      │  └─src
    │      │      └─main
    │      │          ├─java
    │      │          │      Application.java
    │      │          │      
    │      │          └─resources
    │      │                  application.properties
    │      │                  
    │      └─META-INF
    │          └─maven
    │                  archetype-metadata.xml
    │                  
    └─test
        └─resources
            └─projects
                └─basic
                        archetype.properties
                        goal.txt

3、spring-boot-archetype 裡面可以自定義一些模板變數,例如將服務埠和路徑定義為變數

application.properties 裡新增 ${serverPort}${contextPath} 變數

#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
spring.application.name=${artifactId}

server.port=${serverPort}
server.servlet.context-path=/${contextPath}

archetype-metadata.xml 裡新增兩個變數的宣告

  <requiredProperties>
    <requiredProperty key="serverPort">
      <defaultValue>8080</defaultValue>
    </requiredProperty>
    <requiredProperty key="contextPath">
      <defaultValue>/</defaultValue>
    </requiredProperty>
  </requiredProperties>

archetype.properties 裡也要新增一下,設定個預設值

serverPort=8080
contextPath=/

4、在 spring-boot-archetype 根目錄下執行,安裝 Archetype 到本地倉庫(這裡只是為了演示,也可上傳到私服供其它開發人員使用)

mvn clean install

5、使用 Archetype 建立一個工程,執行如下命令

mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=com.rkyao -DarchetypeArtifactId=spring-boot-archetype -DarchetypeVersion=0.0.1-SNAPSHOT -DgroupId=com.rkyao -DartifactId=spring-boot-target -Dpackage=com.rkyao.spring.boot.target -Dversion=0.0.1-SNAPSHOT -DserverPort=9000 -DcontextPath=target_service

引數說明:

  • -DarchetypeGroupId: 自定義archetype的groupId
  • -DarchetypeArtifactId: 自定義archetype的artifactId
  • -DarchetypeVersion: 自定義archetype的版本號
  • -DgroupId: 要生成的工程的groupId
  • -DartifactId: 要生成的工程的artifactId
  • -Dpackage: 要生成的工程的包名
  • -Dversion: 要生成的工程的版本號
  • -DserverPort: 自定義變數,服務埠
  • -DcontextPath: 自定義變數,服務路徑

三、GitHub原始碼地址

https://github.com/yaorongke/spring-boot-demos/tree/main/spring-boot-archetype-sample