1.前言
Java开发规范遵循约定大于配置的潜规则,请在学习或上手不熟悉的东西时,不要乱搞!先搜教程再弄!
2.创建一个SpringBoot父工程
2.1 创建项目
2.2 填写信息
这里就不多说了,重点说一句,最好以SpringBoot模板创建父工程。其它信息填写如果不知道建议回顾SpringBoot教程或者更基础的教程。
2.3 勾选依赖
按需勾选依赖,我这里只弄了web依赖
2.4 修改pom依赖
勾选依赖,创建完成之后,等待项目的maven构建。这里maven构建完成后需要修改一些地方,如下所示:
这里父项目打包方式为pom,添加modules是为了能够后续添加子模块。修改后如下所示:
父项目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.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zrz</groupId>
<artifactId>ps-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ps-learning</name>
<description>ps-learning</description>
<properties>
<java.version>17</java.version>
</properties>
<modules>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<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>
</plugin>
</plugins>
</build>
</project>
3.创建子工程
3.1 创建模块
这里同样是用SpringBoot模板创建
这里可以不用勾选依赖,只勾选插件
3.2 配置父子工程
此时的子工程pom.xml文件为:
这里修改3个地方,第一个,修改parent。第二个,修改打包方式。第三个,去掉多余的依赖。修改后的子工程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>com.zrz</groupId>
<artifactId>ps-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.zrz</groupId>
<artifactId>s-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>s-learning</name>
<description>s-learning</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
</dependencies>
</project>
3.3 在父项目pom.xml中添加模块
<modules>
<module>s-learning</module>
</modules>
4.测试启动是否成功
4.1 写一个Controller
@RestController
public class HelloController {
@GetMapping("hello")
public String hello(){
return "hello";
}
}
4.2 访问
5.补充
5.1 pom.xml中,dependencyManagement 与 dependencies
dependencyManagement标签是管理包所规定的一个标签,多用于父子项目。该标签先定义,但是懒加载。在子项目当中要声明使用的时候,才加载jar包。子项目中可以不用声明版本号使用,在父项目的dependencyManagement中定义版本号,子项目当中引用GA坐标。dependencies直接可以被子项目继承父项目的jar包。因此要注意他俩区别。具体例子在dubbo项目的构建中可以提现:
父项目
<!-- Dubbo -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
子项目
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-spring-boot-interface</artifactId>-->
<!-- <version>${project.parent.version}</version>-->
<!-- </dependency>-->
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.zrz</groupId>
<artifactId>provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>