如果需要进行单体架构的练习,或者有搭建单体多模块的需求,本文可以起到一定的参考
1、项目最外层搭建
点击新建项目,选择到maven
点击下一步
输入我们的groupId和artifactid,点击next
选择我们自己的maven仓库及配置
删除我们的src文件夹,只留下一个pom文件,pom文件我们需要修改一下,里面只要留下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.weiyi</groupId>
<artifactId>single</artifactId>
<version>1.0-SNAPSHOT</version>
<name>single Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
这样我们的最外层模块就搭建好了
2、模块搭建
点击如图,接下来按照第一步一样操作,不同的是如果你新建的的模块如果是公共模块,则只需选择quickstart
该选择用于公共模块,他的打包方式推荐使用jar,新建好之后,我们最外层的pom文件中会多出一个,这个的意思是说我下面有哪些模块,
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.weiyi</groupId>
<artifactId>single</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>common</module>//你新建的模块名(该地方只是用来说明,如粘贴请删除)
</modules>
<packaging>pom</packaging>
<name>single Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
公共依赖模块
为了统一依赖我们可以新建一个公共依赖模块,该模块不需要任何东西,只需要一个pom.xml文件
我们新建一个叫father的模块
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>//代表引用上层的pom
<groupId>com.weiyi</groupId>
<artifactId>single</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>father</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- 环境配置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
//依赖需要什么自己酌情添加,此为公共依赖
</dependencies>
<build>
<plugins>
<!-- Compiler 插件, 设定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
2.2 公共模块pom修改
我们新建了公共依赖模块,那自然得使用它,我们现在来修改common模块的pom文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>//此处代表引用father模块的依赖,并且把打包方式设置为jar,方便我们引用
<groupId>com.weiyi</groupId>
<artifactId>father</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../father/pom.xml</relativePath>
</parent>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>common</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
</project>
这是我们在common模块中使用的示例,我并没有在common模块中引入spring,却可以使用它的注解,原因就在于我在father中引入了spring的依赖,而common模块又使用了father中的依赖
3、模块之间的调用
我们新建了一个common模块,顾名思义,公共模块,里面肯定都是公共方法与常量,是需要提供给别人引用的,假如我们新建了一个front模块,用于调用common的方法,只需在front模块的的pom文件中加入
<dependency>
<groupId>com.weiyi</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
我们在common中写一个方法,方法叫useTest
接下来我们在front模块里面调用
可以看到执行成功,如果不引人common的依赖,是会报错的。
搭建示例到此结束