说说如何使用 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
接下来
在此框架下,可以继续扩展代码模版