maven-logo-black-on-white.png
說說如何使用 Maven Archetype 來創建項目模版,從項目模塊結構上,統一開發規范。
Maven Archetype
了解 maven 的同學,應該使用過如下命令來構建一個空的 maven
項目骨架
mvn archetype:generate -DgroupId=net.fabself.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
這里我們看到下面的參數就是用于指定了一種空的項目骨架原型名稱
-DarchetypeArtifactId=maven-archetype-quickstart
有興趣可以關注一下 http://repo.maven.apache.org/maven2/archetype-catalog.xml
這里是 maven 默認的原型列表
*下面開始創建一套自定義項目骨架原型,并集成 spring-boot *
定義項目原型結構
以 trade 項目為例,建立如下模塊結構:
trade
├── trade-access-launcher
├── trade-common
├── trade-dao
├── trade-schedule-launcher
├── trade-service
trade-access-launcher
springMVC 啟動模塊,用于服務接口發布
trade-schedule-launcher
調度服務啟動模塊,用于定時任務
trade-common
通用接口 & 工具模塊
trade-service
業務邏輯服務層模塊
trade-dao
數據庫訪問層模塊
基于以上項目結構,開始創建項目 Archetype
建立項目
項目目錄結構與文件
fabself-archetype-springboot
├── pom.xml
└── src
└── main
└── resources
├── META-INF
│ └── maven
│ └── archetype-metadata.xml
└── archetype-resources
├── __rootArtifactId__-access-launcher
│ ├── pom.xml
│ └── src
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│ ├── java
│ └── resources
├── __rootArtifactId__-common
│ └── pom.xml
├── __rootArtifactId__-dao
│ ├── pom.xml
│ └── src
│ ├── main
│ │ └── java
│ └── test
├── __rootArtifactId__-schedule-launcher
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
├── __rootArtifactId__-service
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
├── pom.xml
注意:
這里子模塊目錄命名以 __rootArtifactId__
為前綴,用于動態指定 artifactId
進行替換。
根 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ">
<modelVersion>4.0.0</modelVersion>
<groupId>net.fabself.archetype</groupId>
<artifactId>fabself-archetype-springboot</artifactId>
<version>1.0.0-SNAPSHOT</version>
</project>
archetype-metadata.xml 文件
archetype-metadata.xml
中用于定義這個項目骨架中元數據,文件拷貝,過濾規則
整個拷貝過來,便于大家使用:
<?xml version="1.0" encoding="UTF-8" ?>
<archetype-descriptor
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="app-root">
<requiredProperties>
<requiredProperty key="title"> <!--自定義屬性-->
<defaultValue>API</defaultValue>
</requiredProperty>
</requiredProperties>
<modules>
<module id="${rootArtifactId}-access-launcher" dir="__rootArtifactId__-access-launcher"
name="${rootArtifactId}-access-launcher">
<fileSets>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="false">
<directory>src/test/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-schedule-launcher" dir="__rootArtifactId__-schedule-launcher"
name="${rootArtifactId}-schedule-launcher">
<fileSets>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="false">
<directory>src/test/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-common" dir="__rootArtifactId__-common"
name="${rootArtifactId}-common">
<fileSets>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="false">
<directory>src/test/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-dao" dir="__rootArtifactId__-dao"
name="${rootArtifactId}-dao">
<fileSets>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="false">
<directory>src/test/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
<!--id:定義模塊 ArtifactId; dir: 模塊骨架目錄; -->
<module id="${rootArtifactId}-service" dir="__rootArtifactId__-service"
name="${rootArtifactId}-service">
<fileSets>
<!--fileter:設置是否進行${}取值匹配; package:設置是否將 package 指定目錄結構復制到代碼中 -->
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/main/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8" packaged="false">
<directory>src/main/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="true">
<directory>src/test/java</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8" packaged="false">
<directory>src/test/resources</directory>
<includes>
<include>**/*.**</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
</modules>
</archetype-descriptor>
App.java spring-boot 啟動類
package ${package};
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
*
*/
@SpringBootApplication
@EnableSwagger2
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
本地安裝 & 使用
安裝
mvn clean install
使用以下命令就可以開始創建自己的項目框架了
mvn archetype:generate -DarchetypeGroupId=net.fabself.archetype -DarchetypeArtifactId=fabself-archetype-springboot -DarchetypeVersion=1.0.0-SNAPSHOT -DarchetypeCatalog=local
接下來
在此框架下,可以繼續擴展代碼模版