idea通过spring initializr 构建多模块

构建spring boot多模块没有通过maven 那么简单,需要手动修改pom.xml 配置文件,但是也不是很难。

构建spring boot多模块步骤如下:
1. 模块规划
    和上面类似,父模块:bigdata
    俩个子模块:provider和 common
2. 父模块创建
    (1) file ->new -> project -> spring initializr ,点击next 
    (2) 填写group:com.test, artifact:bigdata, 点击next 
    (3) 选择依赖项,此处选择 web,devops,lombok,点击next 
    (4) 输入project name,此处默认,点击finish 
    (5) 父模块创建完成
    
3. 子模块创建
    (1) 父模块右键 new -> module -> spring initializr ,点击next
    (2) 填写group:com.test, artifact: provider,点击next
    (3) 选择provider 需要依赖的模块,视情况而定,也可以不选,本模块此处不选,点击next
    (4) 点击finish,完成子模块创建    
    
3. 修改子模块pom.xml 文件
    修改子模块 <parent> 节点

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <parent>
    <groupId>com.test</groupId>
    <artifactId>bigdata</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>  

4. 修改父模块pom.xml 文件
    在pom.xml 文件中 添加 <modules> 节点

  <modules>
    <module>provider</module>
  </modules>

    添加打包方式

  <packaging>pom</packaging>

5. 创建其他子模块
    按照3 创建子模块,同时修改 parent节点为 bigdata
    按照4, 在modules中添加 子模块名称
    
6. 多模块构建完毕后 子模块的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>2.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.test</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>provider</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

7. 多模块构建完毕后 父模块的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>2.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.test</groupId>
  <artifactId>bigdata</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>bigdata</name>
  <description>Demo project for Spring Boot</description>

  <packaging>pom</packaging>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <modules>
    <module>provider</module>
  </modules>

  
 8. 优化模块依赖
    此处可以把子模块共同依赖的 jar包,迁移到 父 pom.xml 文件中,把重复的依赖模块删除掉即可.

发布了220 篇原创文章 · 获赞 16 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zhanggqianglovec/article/details/103880387