随着项目越来越大,模块越来越多,分模块必不可少,今天我写了一个小项目,把遇到的一些问题小记一下。
创建项目
创建一个 SpringBoot 项目非常的简单,简单到这里根本不用再提。你可以在使用 IDEA 新建项目时直接选择 Spring Initlalize 创建一个 Spring Boot 项目,也可以使用 Spring 官方提供的 Spring Boot 项目生成页面得到一个项目。
下面介绍一下使用 Spring 官方生成的方式,如果你已经有了一个 Spring Boot 项目,这部分可以直接跳过。
1、打开 https://start.spring.io/
2、点击 Generate 按钮下载项目。
3、将项目导入IDEA中
其中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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
4、调整目录结构添加controller和entity 用于待会测试
Controller内容如下
/**
* @Author snail
* @Description TODO
* @Date 2023/3/7 15:48
* @Version 1.0
*/
@RequestMapping("/people")
@RestController
public class PeopleController {
/**
* 获取列表
*
* @return
*/
@GetMapping("/list")
public Map list() {
// 模拟查询逻辑
com.example.peopleweb.entity.People people = new com.example.peopleweb.entity.People();
people.setName("张三");
people.setAge(18);
people.setSex(true);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("code", 200);
resultMap.put("message", "成功");
resultMap.put("data", Arrays.asList(people));
return resultMap;
}
}
entity内容如下
public class People {
private String name ;
private int age;
private boolean sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
5、启动项目出现我们最爱的日志。
项目分模块
以上准备工作已经全部完成,下面我们开始分模块操作。
借助 IDEA 工具可以快速的把项目改造成 maven 多模块,这里我们把准备测试 demo 拆分为 common 和 web 两个模块,common 模块存放实体类。web 模块存放 controller 层(拆分只是为了方便演示,请勿纠结合理性问题)。
1、拆分common 模块
项目直接 new -> module。
选择 maven -> next,填写模块名称。
继续 next 完成模块创建。
2、拆分web 模块
创建步骤和common 大差不差,这里不再展示
3、查看主pom.xml文件 其中自动会将生成的两个module 自动添加进去,若没有添加,则需要手动添加进去。还需要设置pom.xml的打包方式
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>people-common</module>
<module>people-web</module>
</modules>
4、将代码移动到指定的模块中,entity 移动到 people common
其他文件移动到peopleweb。移动完后代码结构如下:
到这里分模块结构上已经完成。
项目依赖管理
你发现了代码里的红色警告,不过你也瞬间想到了是因为把 people 类移动到了 product-common 模块,导致这里引用不到了。所以很自然我们应该去web模块pom.xml添加他的依赖。
<dependency>
<groupId>com.example.peoplecommon</groupId>
<artifactId>people-common</artifactId>
</dependency>
刷新maven后你会发现依然抱错,
这时候我们才想起,添加依赖没有指定版本号。于是我们继续修改 pom.xml文件
最后发现maven 已经不报错了。
编译成功,运行启动类,又看见了我们熟悉的 Spring logo。
项目打包编译
到了最后一步,我们满心欢喜的已经期待成功了,可是现实却总是有点曲折。mvn package 后直接控制台出现了错误
[INFO] Final Memory: 22M/87M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.5.RELEASE:repackage (repackage) on project product-common: Execution repackage of goal org.springframework.boot:spring-boot-m
aven-plugin:2.2.5.RELEASE:repackage failed: Unable to find main class -> [Help 1]
[ERROR]
ERROR 让你伤心了,但是你还是从报错中寻找到了一些蛛丝马迹,你看到是 spring-boot-maven-plugin 报出的错误。重新审视你的主 pom 发现 编译插件用到了 spring-boot-maven-plugin。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
略加思索后将这段移动到 web 模块的 pom,因为这是 Spring Boot 的打包方式,现在放在主 pom 中所有的模块都会继承到,那么对于 common 模块来说是肯定不需要的。于是移动到web模块
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
移动后重新打包,不管你是运行命令 mvn package 还是双击 IDEA 中的 maven 管理中的 package ,想必这时候你都已经打包成功了
可以看到已经打包成功了。
在 web 模块下的目录 target 里也可以看到打包后的 jar 文件 people-web-0.0.1-SNAPSHOT.jar。可以使用 java 命令直接运行 。
java -jar people-web-0.0.1-SNAPSHOT.jar
可以看到项目已经成功启动。
我们继续测试接口,很显然是能调用成功的,整个项目分模块就此完成。