What matters in life is not what happens to you but what you remember and how you remember it.
生命中真正重要的不是你遭遇了什么,而是你记住了哪些事,又是如何铭记的。
——马尔克斯《百年孤独》
在最近的项目开发中,为了满足后期将项目改造成分布式项目,探索将传统的单一工程改造成使用IntelliJ IDEA基于Maven创建多模块项目的实际开发。
最终工程结构如下图所示,聚合工程为base,base-common为通用的类module,base-core为核心业务module,base-web为控制层。
(1)通过idea的Spring Initializr快速创建一个maven工程,并删除无关的文件,只保留pom.xml文件:
(2)修改聚合工程base的pom文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.chunsoftgroupId>
<artifactId>baseartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>basename>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
<relativePath/>
parent>
<modules>
<module>base-commonmodule>
<module>base-coremodule>
<module>base-webmodule>
modules>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
同样通过Spring Initializr创建子模块:
(1)创建base-common
对着父工程右键 - New - Module - > 输入 base-common
如图所示,根据工程通用需求新建了一些包目录:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.chunsoftgroupId>
<artifactId>base-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>base-commonname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>cn.chunsoftgroupId>
<artifactId>baseartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
dependencies>
project>
(2)创建base-core
对着父工程右键 - New - Module - > 输入 base-core
如图所示,根据工程通用需求新建了一些包目录:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.chunsoftgroupId>
<artifactId>base-coreartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>base-corename>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>cn.chunsoftgroupId>
<artifactId>baseartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
dependencies>
project>
(3)创建base-web
对着父工程右键 - New - Module - > 输入 base-web
如图所示,根据工程通用需求新建了一些包目录:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.chunsoftgroupId>
<artifactId>base-webartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>base-webname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>cn.chunsoftgroupId>
<artifactId>baseartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>cn.chunsoftgroupId>
<artifactId>base-coreartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>cn.chunsoftgroupId>
<artifactId>base-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>basefinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>springloadedartifactId>
<version>${springloaded.version}version>
dependency>
dependencies>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8jvmArguments>
configuration>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
在 maven project侧边栏中,先点击base项目的clean,然后点击package或install进行打包;
如果觉得结构上存在问题欢迎大家一起讨论。