随着领域驱动的兴起,很多人都想学习如何进行ddd的项目开发,那ddd的项目结构是怎么样的?又是如何结合SpringBoot呢?那么针对这个问题,笔者使用maven的archetype封装一个相对通用的ddd的项目目录,方便一键生成DDD项目目录。
项目地址在文末
archetype项目
archetype项目各种依赖版本要求
1、JDK-17
2、maven-3.9.6
3、idea-2024.1
4、git-2.44.0
archetype项目结构
archetype-metadata.xml内容
<?xml version="1.0" encoding="UTF-8" ?>
<archetype-descriptorxsi: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="archetype-multi-module"><requiredProperties><requiredProperty key="groupId"><defaultValue>gold.muxi</defaultValue></requiredProperty><requiredProperty key="artifactId"><defaultValue>wanjinyou</defaultValue></requiredProperty><requiredProperty key="version"><defaultValue>1.0.0-SNAPSHOT</defaultValue></requiredProperty><requiredProperty key="package"><defaultValue>gold.muxi.wanjinyou</defaultValue></requiredProperty><requiredProperty key="startClassName"><defaultValue>StartApplication</defaultValue></requiredProperty><requiredProperty key="springbootVersion"><defaultValue>3.2.5</defaultValue></requiredProperty></requiredProperties><modules><module id="${rootArtifactId}-start" name="${rootArtifactId}-start" dir="__rootArtifactId__-start"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet><fileSet filtered="true" encoding="UTF-8" packaged="false"><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></fileSet></fileSets></module><module id="${rootArtifactId}-application" name="${rootArtifactId}-application" dir="__rootArtifactId__-application"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet></fileSets></module><module id="${rootArtifactId}-common" name="${rootArtifactId}-common" dir="__rootArtifactId__-common"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet><fileSet filtered="true" encoding="UTF-8" packaged="false"><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></fileSet></fileSets></module><module id="${rootArtifactId}-interfaces" name="${rootArtifactId}-interfaces" dir="__rootArtifactId__-interfaces"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet></fileSets></module><module id="${rootArtifactId}-infrastructure" name="${rootArtifactId}-infrastructure" dir="__rootArtifactId__-infrastructure"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet><fileSet filtered="true" 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></fileSets></module><module id="${rootArtifactId}-domain" name="${rootArtifactId}-domain" dir="__rootArtifactId__-domain"><fileSets><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/main/java</directory><includes><include>**/*.*</include></includes></fileSet><fileSet filtered="true" encoding="UTF-8" packaged="true"><directory>src/test/java</directory><includes><include>**/*.*</include></includes></fileSet></fileSets></module></modules>
</archetype-descriptor>
requiredProperties 解析
groupId:表示根据这个archetype生成的项目的 的值,如不传递这个值,默认值是:gold.muxi
artifactId:表示根据这个archetype生成的项目的的值,如不传递这个值,默认值是:wanjinyou
version:表示根据这个archetype生成的项目的的值,如不传递这个值,默认值是:1.0.0-SNAPSHOT
package:表示根据这个archetype生成的项目的包名的值,如不传递这个值,默认值是:gold.muxi.wanjinyou
startClassName:表示根据这个archetype生成的项目的启动类的类名,如不传递这个值,默认值是:startClassName
springbootVersion:表示根据这个archetype生成的项目的项目的springboot版本,如不传递这个值,默认值是:3.2.5
archetype项目使用说明
# 克隆本项目代码到本地
git clone https://gitee.com/Sanliren/wanjinyou-archetype.git
# 进入项目目录
cd wanjinyou-archetype
# 安装脚手架项目到本地maven仓库
mvn clean install
# 根据脚手架生成项目
mvn archetype:generate -B -DarchetypeGroupId=gold.muxi -DarchetypeArtifactId=wangjinyou-archetype -DarchetypeVersion=1.0 -DgroupId=com.wanliangjin -DartifactId=demo -Dversion=1.0.0-SNAPSHOT -Dpackage=com.wanliangjin.demo -X -DarchetypeCatalog=local
mvn archetype:generate 命令解析
-B:表示不用在命令执行过程中交互输入
-DarchetypeGroupId=gold.muxi :指定脚手架的 groupId ,不用修改
-DarchetypeArtifactId=wangjinyou-archetype :指定脚手架的 artifactId ,不用修改
-DarchetypeVersion=1.0-SNAPSHOT # 指定脚手架的版本,不用修改
-DgroupId=com.wanliangjin :新项目的 groupId,根据自己的进行修改
-DartifactId=demo :新项目的 artifactId ,根据自己的进行修改
-Dversion=1.0.0-SNAPSHOT :新项目的 version , 根据自己的进行修改
-Dpackage=com.wanliangjin.demo :新项目的包名,根据自己的进行修改
-X -DarchetypeCatalog=local :指定从本地获取该脚手架的位置
生成的项目demo目录结构
总结
至此,根据maven的脚手架archetype生成多模块的ddd项目目录结构就完成了,以后再需要生成新项目的时候就可以直接用命令生成,不需要重新又搭建一遍目录;而且在微服务下,多个微服务也可以都根据这个脚手架生成每个服务,就不用那么麻烦了。(^-^)
脚手架项目地址
https://gitee.com/Sanliren/wanjinyou-archetype记得 start ( •̀ ω •́ )✧