springboot 搭建多模块调用以及打包执行

一 springboot搭建多模块规则

1.1 规则描述

1.父pom.xml 打包方式,jar要更改为pom,build 需要更改

2.不需要打包的模块pom.xml文件中不要写<build>,全删掉,例如有些工程中的common模块,domain模块,dao模块,service模  块都不需要打包

3.在子模块中需要声明父工程,填写父工程位置<relativePath>../pom.xml</relativePath>

4.关于applicatin.properties配置文件,只需要在启动的模块中配置就可以了

5.关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!
6.对象之间的调用是使用@Autowired 注解 来实现注入。

1.2 打比方案例说明

多模块搭建,首先肯定得有一个父工程,还有若干个子模块,子模块可以理解为,可以继承父模块

整个工程你就当作一个公司,父工程(退休了什么也不干)只需要声明有几个儿子(子模块)就完事了,

假设让子模块api作为程序启动模块,子模块api声明父工程是谁,就当他是大儿子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它子模块协助。

它子模块声明父工程是谁,之间关系都是兄弟,不需要打包,哪里需要去哪里!

二 操作案例

2.1 首先创建一个父模块

1.首先创建一个父模块:huadian-multil-model

springboot 搭建多模块调用以及打包执行

2.将工程中的src进行删除

2.2 创建需要的各个子模块

1.创建api,domain,common,dao,service等模块

选中父工程,右键 new module,如下图:

springboot 搭建多模块调用以及打包执行

2.最后创建的工程如下:

springboot 搭建多模块调用以及打包执行

2.3 各个模块的关系

父模块:  huadian-multil-model

子模块: huadian-api       (页面交互接收、传递数据,唯一有启动类的模块)

              huadian-common   一些常用工具类

             huadian-domain    (实体类)

             huadian-services   (处理业务逻辑)

            huadian-dao   (用于持久化数据跟数据库交互)

调用关系: huadian-api 依赖 huadian-common、huadian-domain、 huadian-services、huadian-dao

               huadian-services  依赖 huadian-common、huadian-domain、 huadian-dao

                huadian-dao   依赖    huadian-common、huadian-domain

                huadian-common 依赖   huadian-domain

2.4 各个模块的pom文件

2.4.1 父模块

<?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>
  <modules>
    <module>huadian-api</module>
    <module>huadian-common</module>
    <module>huadain-service</module>
    <module>huadian-domain</module>
      <module>huadian-dao</module>
  </modules>
  <!--指定项目的spring boot的版本-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <!--版本号-->
  <groupId>com.huadian</groupId>
  <artifactId>huadian-multil-model</artifactId>
  <packaging>pom</packaging> <!--父类为pom-->
  <version>1.0-SNAPSHOT</version>

  <name>huadian-multil-model</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <!--指定项目中的公有依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>


  </dependencies>

  <!-- 阿里云maven仓库 -->
  <repositories>
    <repository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.4.2 api子模块

<?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>
    <groupId>com.huadian</groupId>
    <artifactId>huadian-multil-model</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <!-- api子模块 -->
  <groupId>com.huadian</groupId>
  <artifactId>huadian-api</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>huadian-api</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--依赖domain模块-->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!-- <version>${project.parent.version}</version>  -->
    </dependency>
    <!-- 依赖common模块 -->
    <dependency>
    <groupId>com.huadian</groupId>
    <artifactId>huadian-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- serverice子模块 -->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadain-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- 依赖dao模块 -->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-dao</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!-- 指定该Main Class为全局的唯一入口 -->
          <mainClass>com.huadian.App</mainClass>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

2.4.3  service子模块

<?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>
    <groupId>com.huadian</groupId>
    <artifactId>huadian-multil-model</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <!-- serverice子模块 -->
  <groupId>com.huadian</groupId>
  <artifactId>huadain-service</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>huadain-service</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!--依赖domain模块-->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!-- <version>${project.parent.version}</version>  -->
    </dependency>
    <!-- 依赖common模块 -->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- 依赖dao模块 -->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-dao</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

2.4.4 dao子模块

<?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>
    <groupId>com.huadian</groupId>
    <artifactId>huadian-multil-model</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <!-- 子模块 -->
  <groupId>com.huadian</groupId>
  <artifactId>huadian-dao</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>huadian-dao</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--依赖domain模块-->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!-- <version>${project.parent.version}</version>  -->
    </dependency>
  </dependencies>


</project>

2.4.5 common子模块

<?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>
    <groupId>com.huadian</groupId>
    <artifactId>huadian-multil-model</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <!-- common 子模块 -->
  <groupId>com.huadian</groupId>
  <artifactId>huadian-common</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>huadian-common</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--依赖domain模块-->
    <dependency>
      <groupId>com.huadian</groupId>
      <artifactId>huadian-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!-- <version>${project.parent.version}</version>  -->
    </dependency>

  </dependencies>


</project>

2.4.6 domain子模块

<?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>
  <modules>
    <module>huadian-api</module>
    <module>huadian-common</module>
    <module>huadain-service</module>
    <module>huadian-domain</module>
      <module>huadian-dao</module>
  </modules>
  <!--指定项目的spring boot的版本-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <!--版本号-->
  <groupId>com.huadian</groupId>
  <artifactId>huadian-multil-model</artifactId>
  <packaging>pom</packaging> <!--父类为pom-->
  <version>1.0-SNAPSHOT</version>

  <name>huadian-multil-model</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <!--指定项目中的公有依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>


  </dependencies>

  <!-- 阿里云maven仓库 -->
  <repositories>
    <repository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.5 测试

springboot 搭建多模块调用以及打包执行

springboot 搭建多模块调用以及打包执行

三  打包

1.不能在api模块package,需要在父模块下,一起打包,如下图

springboot 搭建多模块调用以及打包执行

2. 这个时候api模块已经打好包,进入target下执行java -jar

springboot 搭建多模块调用以及打包执行

springboot 搭建多模块调用以及打包执行

参考:https://blog.csdn.net/baidu_41885330/article/details/81875395

源代码见github

 

上一篇:在Springboot用apache commons包的ServerFileUpload.parseRequest接收文件上传踩坑经历


下一篇:org.apache.commons.beanutils.ConversionException: Default conversion to