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:
pom - 引入SpringBoot相关配置,把项目配置为SpringBoot项目: 添加
- 把依赖放入:
完整的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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.5.0version> <relativePath /> parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <name>studentname> <version>0.0.1-SNAPSHOTversion> <packaging>pompackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <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> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent>
student-web 项目创建成功后,可以看到student/pom.xml文件会多了如下配置信息
<modules> <module>student-webmodule> modules>
2.3 配置 各层级 pom.xml文件
- 配置student-web/pom.xml 文件:添加配置
war , 把student-web定义为项目启动模块
student-web/pom.xml, 添加
<packaging>warpackaging>
附上配置后的完整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.0modelVersion> <parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent> <artifactId>student-webartifactId> <name>student-webname> <packaging>warpackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> dependencies> project>
2.4 创建项目启动类
student-web/pom.xml 添加依赖
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> 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(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 有
节点 - student/pom.xml 文件多了
看下student-setting/pom.xml
<parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent>
student-setting 模块创建成功后,再看下student/pom.xml 。 这里可以手动调整下顺序
<modules> <module>student-settingmodule> <module>student-webmodule> modules>
3.3 配置 各层级 pom.xml文件
因为student-setting 打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖
- 配置student-setting/pom.xml 文件: 添加配置
jar , 把student-setting 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用. - 在student/pom.xml添加管理依赖
- 在student-web/pom 引入依赖
student-setting/pom.xml : 配置打包方式
<packaging>jarpackaging>
student/pom.xml: 管理依赖
<properties> <student.version>0.0.1-SNAPSHOTstudent.version> properties> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> <version>${student.version}version> dependency>
student-web/pom.xml: 添加依赖
<dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> 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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.5.0version> <relativePath /> parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <name>studentname> <version>0.0.1-SNAPSHOTversion> <packaging>pompackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <student.version>0.0.1-SNAPSHOTstudent.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> <version>${student.version}version> dependency> dependencies> dependencyManagement> <modules> <module>student-settingmodule> <module>student-webmodule> 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.0modelVersion> <parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent> <artifactId>student-webartifactId> <name>student-webname> <packaging>warpackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> 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.0modelVersion> <parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent> <artifactId>student-settingartifactId> <name>student-settingname> <packaging>jarpackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> dependencies> project>
3.4 测试:
在student-setting/pom.xml添加依赖
org.springframework.boot spring-boot-starter-web
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-utilmodule> <module>student-settingmodule> <module>student-webmodule> modules>
student-util/pom.xml 有如下
<parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent>
4.3 修改各层级pom.xml 配置
因为student-utils 需要打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖
- 配置student-utils/pom.xml 文件: 添加配置
jar , 把student-utils 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用. - 在student/pom.xml添加管理依赖
- 在student-web/pom 引入依赖
student-util/pom.xml 添加
<packaging>jarpackaging>
student/pom.xml 添加
<dependency> <groupId>com.demo.studentgroupId> <artifactId>student-utilartifactId> <version>${student.version}version> dependency>
student-web/pom.xml 添加
<dependency> <groupId>com.demo.studentgroupId> <artifactId>student-utilartifactId> 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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.5.0version> <relativePath /> parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <name>studentname> <version>0.0.1-SNAPSHOTversion> <packaging>pompackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <student.version>0.0.1-SNAPSHOTstudent.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> <version>${student.version}version> dependency> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-utilartifactId> <version>${student.version}version> dependency> dependencies> dependencyManagement> <modules> <module>student-utilmodule> <module>student-settingmodule> <module>student-webmodule> 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.0modelVersion> <parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent> <artifactId>student-webartifactId> <name>student-webname> <packaging>warpackaging> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-utilartifactId> dependency> <dependency> <groupId>com.demo.studentgroupId> <artifactId>student-settingartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> 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.0modelVersion> <parent> <groupId>com.demo.studentgroupId> <artifactId>studentartifactId> <version>0.0.1-SNAPSHOTversion> parent> <artifactId>student-utilartifactId> <name>student-utilname> <packaging>jarpackaging> <properties> <project.build.sourceEncoding>UTF-8project.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.studentgroupId> <artifactId>student-utilartifactId> 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.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> pluginManagement> build>
student-web/pom.xml 配置plugin
<build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> 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
原文:https://www.cnblogs.com/dreamstar99/p/15292884.html