SpringBoot + Maven父子模块项目
- student-web: 项目启动类和一些配置文件
- student-setting: 增删改查
- student-util: 存放一些工具类,供student-setting等模块使用
1.创建父project
1.1 创建student项目
File -> New -> Maven Project -> maven-archetype-quickstart
1.2 修改父亲项目的结构:删除src和target目录,只留下pom.xml文件
删除前
删除后
1.3 修改父亲项目的pom.xml文件
- 把打包方式修改为pom: <packaging>pom</packaging>
- 引入SpringBoot相关配置,把项目配置为SpringBoot项目: 添加<parent></parent>
- 把依赖放入: <dependencyManagement></dependencyManagement>
完整的pom.xml文件如下
<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 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath /> <!-- lookup parent from repository --> </parent> <!-- project --> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <name>student</name> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- properties --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- dependencyManagement --> <dependencyManagement> <dependencies> </dependencies> </dependencyManagement> </project>
2.创建student-web模块
2.1 创建student-web 模块
File -> New -> Maven Module -> maven-archetype-quickstart
这是整个项目的启动类和配置文件,这里注意web模块的package, 定义最外层的package, 要把启动类放在最外层的package, 因为springboot扫描组件时,会扫描启动类所在的package和它的子package
2.2 查看 各层级 pom.xml文件
student-web/pom.xml, 里面定义了<parent></parent>
<parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
student-web 项目创建成功后,可以看到student/pom.xml文件会多了如下配置信息
<modules> <module>student-web</module> </modules>
2.3 配置 各层级 pom.xml文件
- 配置student-web/pom.xml 文件:添加配置 <packaging>war</packaging>, 把student-web定义为项目启动模块
student-web/pom.xml, 添加
<packaging>war</packaging>
附上配置后的完整student-web/pom.xml 文件
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>student-web</artifactId> <name>student-web</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> </project>
2.4 创建项目启动类
student-web/pom.xml 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
添加启动类
package com.demo.student; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication(scanBasePackages = { "com.demo.student" },exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class }) public class StudentApplication { public static void main(String[] args) { SpringApplication.run(StudentApplication.class, args); } }
3.创建student-setting模块
3.1 创建student-setting模块
File -> New -> Maven Module -> maven-archetype-quickstart
3.2 查看 各层级 pom.xml文件
- student-setting/pom.xml 有<parent>节点
- student/pom.xml 文件多了<module>
看下student-setting/pom.xml
<parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
student-setting 模块创建成功后,再看下student/pom.xml 。 这里可以手动调整下顺序
<modules> <module>student-setting</module> <module>student-web</module> </modules>
3.3 配置 各层级 pom.xml文件
因为student-setting 打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖
- 配置student-setting/pom.xml 文件: 添加配置 <packaging>jar</packaging>, 把student-setting 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用.
- 在student/pom.xml添加管理依赖
- 在student-web/pom 引入依赖
student-setting/pom.xml : 配置打包方式
<packaging>jar</packaging>
student/pom.xml: 管理依赖
<properties> <student.version>0.0.1-SNAPSHOT</student.version> </properties> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> <version>${student.version}</version> </dependency>
student-web/pom.xml: 添加依赖
<dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> </dependency>
附上修改后完成的pom.xml
student/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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath /> <!-- lookup parent from repository --> </parent> <!-- project --> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <name>student</name> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- properties --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <student.version>0.0.1-SNAPSHOT</student.version> </properties> <!-- dependencyManagement --> <dependencyManagement> <dependencies> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> <version>${student.version}</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>student-setting</module> <module>student-web</module> </modules> </project>
student-web/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>student-web</artifactId> <name>student-web</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
student-setting/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>student-setting</artifactId> <name>student-setting</name> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> </project>
3.4 测试:
在student-setting/pom.xml添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
student-setting项目 添加测试类
package com.demo.student.setting; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/setting") public class StudentSettingController { @GetMapping("/save") public String save() { System.out.println("This is student-setting save function"); return "This is student-setting save function"; } }
启动student-web项目,访问http://localhost:8080/api/setting/save
4.创建student-util模块
4.1 创建student-util 模块
File -> New -> Maven Module -> maven-archetype-quickstart
4.2 查看各层级pom.xml
student/pom.xml 多了如下配置
<modules> <module>student-util</module> <module>student-setting</module> <module>student-web</module> </modules>
student-util/pom.xml 有如下<parent>配置
<parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
4.3 修改各层级pom.xml 配置
因为student-utils 需要打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖
- 配置student-utils/pom.xml 文件: 添加配置 <packaging>jar</packaging>, 把student-utils 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用.
- 在student/pom.xml添加管理依赖
- 在student-web/pom 引入依赖
student-util/pom.xml 添加
<packaging>jar</packaging>
student/pom.xml 添加
<dependency> <groupId>com.demo.student</groupId> <artifactId>student-util</artifactId> <version>${student.version}</version> </dependency>
student-web/pom.xml 添加
<dependency> <groupId>com.demo.student</groupId> <artifactId>student-util</artifactId> </dependency>
附上修改后的各级pom.xml
student/pom.xml
<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 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath /> <!-- lookup parent from repository --> </parent> <!-- project --> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <name>student</name> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- properties --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <student.version>0.0.1-SNAPSHOT</student.version> </properties> <!-- dependencyManagement --> <dependencyManagement> <dependencies> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> <version>${student.version}</version> </dependency> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-util</artifactId> <version>${student.version}</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>student-util</module> <module>student-setting</module> <module>student-web</module> </modules> </project>
student-web/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>student-web</artifactId> <name>student-web</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-util</artifactId> </dependency> <dependency> <groupId>com.demo.student</groupId> <artifactId>student-setting</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
student-uitl/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo.student</groupId> <artifactId>student</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>student-util</artifactId> <name>student-util</name> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> </project>
4.4 测试
student-util 模块 添加一个工具类DateUtil
package com.demo.student.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { public static String yesterday() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR, -1); String yesterday = simpleDateFormat.format(calendar.getTime()); return yesterday; } public static String today() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String yesterday = simpleDateFormat.format(new Date()); return yesterday; } }
student-setting/pom.xml 添加依赖, 就可以把student-util模块引入到student-setting模块 ,这样在student-setting模块可以使用这个工具类DateUtil,
<dependency> <groupId>com.demo.student</groupId> <artifactId>student-util</artifactId> </dependency>
修改StudentSettingController
package com.demo.student.setting; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.demo.student.util.DateUtil; @RestController @RequestMapping("/api/setting") public class StudentSettingController { @GetMapping("/save") public String save() { System.out.println("This is student-setting save function"); String today = DateUtil.today(); String yesterday = DateUtil.yesterday(); return "This is student-setting save function today is " + today + " yesterday is " + yesterday; } }
测试
启动student-web项目,访问http://localhost:8080/api/setting/save
5.编译运行
student/pom.xml 配置plugin
<build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build>
student-web/pom.xml 配置plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
在student/pom.xml同级目录打开cmd,执行命令mvn clean package -Dmaven.test.skip=true,最后的编译过程如下,打好的war包在 student\student-web\target\ 文件夹内
在 student\student-web\target\ cmd 执行 java -jar student-web-0.0.1-SNAPSHOT.war
访问 http://localhost:8080/api/setting/save