https://www.jb51.net/article/140772.htm?proxy=1
这篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包,分享给大家,具体如下:

项目使用的工具:

  1. IntelliJ IDEA
  2. JDK 1.8
  3. apache-maven-3.3.9

项目的目录:

  1. 主项目 springboot-multi
  2. 子模块 entity、dao、service、web

一、使用IDEA创建一个SpringBoot项目 : File -> new -> Project 项目名称为springboot-multi

二、删除项目中的src目录,把pom.xml中的项目打包方式改为pom,如下:

1

2

3

4

5

<groupId>com.example</groupId>

<artifactId>springboot-multi</artifactId>

<version>0.0.1-SNAPSHOT</version>

<!-- 此处改为pom -->

<packaging>pom</packaging>

三、创建springboot-multi项目的子模块,在项目上右键单击,选择:new -> Module。

四、创建四个子模块后,删除子模块中 src/main/java、src/main/java下的所有文件(如果没有文件跳过此操作),只保留web子模块的SpringBoot的Application主启动类。

五、主项目pom.xml (注意<modules>标签是否指定了子模块)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

<modelVersion>4.0.0</modelVersion

  <groupId>com.example</groupId>

  <artifactId>springboot-multi</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <!-- 此处改为pom -->

  <packaging>pom</packaging>

 

  <name>springboot-multi</name>

  <description>Demo project for Spring Boot</description>

 

  <modules>

    <module>web</module>

    <module>service</module>

    <module>dao</module>

    <module>entity</module>

  </modules>

 

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.10.RELEASE</version>

    <relativePath/> <!-- lookup parent from repository -->

  </parent>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

  </properties>

 

  <dependencies>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

  </dependencies>

 

  <!--指定使用maven打包-->

  <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>

六、web子模块pom.xml(依赖service、dao、entity子模块)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

<modelVersion>4.0.0</modelVersion

  <groupId>com.example</groupId>

  <artifactId>web</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>web</name>

  <description>Demo project for Spring Boot</description>

 

  <parent>

    <groupId>com.example</groupId>

    <artifactId>springboot-multi</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <relativePath>../pom.xml</relativePath>

  </parent>

 

  <dependencies>

    <dependency>

      <groupId>com.example</groupId>

      <artifactId>service</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

    <dependency>

      <groupId>com.example</groupId>

      <artifactId>dao</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

    <dependency>

      <groupId>com.example</groupId>

      <artifactId>entity</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

  </dependencies>

 

  <!--spring boot打包的话需要指定一个唯一的入门-->

  <build>

    <plugins>

      <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

        <configuration>

          <!-- 指定该Main Class为全局的唯一入口 -->

          <mainClass>com.example.WebApplication</mainClass>

          <layout>ZIP</layout>

        </configuration>

        <executions>

          <execution>

            <goals>

              <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->

            </goals>

          </execution>

        </executions>

      </plugin>

    </plugins>

  </build>

七、service子模块pom.xml(依赖 dao 、entity子模块)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<modelVersion>4.0.0</modelVersion>

 

  <groupId>com.example</groupId>

  <artifactId>service</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>service</name>

  <description>Demo project for Spring Boot</description>

 

  <parent>

    <groupId>com.example</groupId>

    <artifactId>springboot-multi</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <relativePath>../pom.xml</relativePath>

  </parent>

 

  <dependencies>

    <dependency>

      <groupId>com.example</groupId>

      <artifactId>dao</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

    <dependency>

      <groupId>com.example</groupId>

      <artifactId>entity</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

  </dependencies>

八、dao子模块pom.xml (依赖entity子模块)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<modelVersion>4.0.0</modelVersion>

 

<groupId>com.example</groupId>

<artifactId>dao</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

 

<name>dao</name>

<description>Demo project for Spring Boot</description>

 

<parent>

  <groupId>com.example</groupId>

  <artifactId>springboot-multi</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <relativePath>../pom.xml</relativePath>

</parent>

 

<dependencies>

  <dependency>

    <groupId>com.example</groupId>

    <artifactId>entity</artifactId>

    <version>0.0.1-SNAPSHOT</version>

  </dependency>

</dependencies>

九、entity子模块

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<modelVersion>4.0.0</modelVersion>

 

<groupId>com.example</groupId>

<artifactId>entity</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

 

<name>entity</name>

<description>Demo project for Spring Boot</description>

 

  <parent>

    <groupId>com.example</groupId>

    <artifactId>springboot-multi</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <relativePath>../pom.xml</relativePath>

  </parent>

十、pom.xml文件中需要注意的就是:

  1. 主项目的modules标签指定的子模块是否正确
  2. 子模块之间的依赖
  3. 子模块的parent标签

十一、web子模块的Application启动类:

1

2

3

4

5

6

7

8

9

10

11

12

13

@RestController

@SpringBootApplication

public class WebApplication {

 

  public static void main(String[] args) {

    SpringApplication.run(WebApplication.class, args);

  }

 

  @RequestMapping(value = "/test",method = RequestMethod.GET)

  public String test(){

    return "test success";

  }

}

十二、执行main方法启动项目,访问localhost:8080/test,出现如下页面表示项目搭建成功:

十三、项目打包命令: mvn clean package 或者 使用右边工具栏的图形化界面打包也可以:

十四、打包成功日志:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

SpringBoot+Maven 多模块项目的构建、运行、打包实战相关推荐

  1. 解决springboot maven多模块项目打包的时候某个被依赖的模块报错找不到main class

    springboot maven 多模块项目打包的时候某个被依赖的模块报错 [ERROR] Failed to execute goal org.springframework.boot:spring ...

  2. 【sprinb-boot】maven 多模块项目:单独 spring-boot:run 某个模块

    目录 前言 假设的 maven 多模块项目 模块关系1 模块关系2 模块关系3 模块关系4 示例:模块关系1 1,my-parent1/pom.xml 文件 2,my-parent1/my-app1/ ...

  3. maven 多模块项目:单独构建某个模块

    前言 maven 3.5.0 在maven 多模块项目中,使用-pl -am -amd等参数可以单独构建某个模块. maven 多模块项目关系 单独构建模块:module1 在父级项目目录中执行如下命 ...

  4. Springboot+Mybatis+Druid+Maven多模块项目搭建遇到的各种吭

    Springboot+Mybatis+Druid+Maven多模块项目搭建 这里记录一下搭建多模块遇到的吭 首先建立一个父级空项目,在pox里修改下配置 2,建立DaoMapper层和ModelEnt ...

  5. Maven多模块项目中应用maven-tomcat-plugin热部署

    Maven多模块项目中使用maven-tomcat-plugin热部署 比如有一个父模块xfl(类型为pom的maven项目),其中的pom.xml为: <modules> <mod ...

  6. maven多模块项目,多web合并项目使用心得

    Fixflow,做中国最好的开源流程引擎!项目地址https://github.com/fixteam/fixflow 此文章适合maven初学者或想接触maven的用户,讲的只是皮毛,高手请自觉略过 ...

  7. Maven多模块项目编译失败:程序包xxx不存在

    Maven多模块项目编译失败:程序包xxx不存在 项目结构如下: parent(父类工程) | - - - - - common(通用工具类子工程) | - - - - - projectA(spri ...

  8. maven 多模块项目关系

    前言 maven 3.5.0 maven 多模块项目关系 maven 多模块项目关系有2种,分别为:继承关系和聚合关系. 继承关系 具有继承关系的多模块项目结构如下: my-project-inher ...

  9. 在 IntelliJ IDEA 中创建基本的 Maven 多模块项目

    在 IntelliJ IDEA 中创建基本的 Maven 多模块项目 笔者的环境: Maven 3.6.3 JDK 11 IntelliJ IDEA 2020.2.2 (Ultimate Editio ...

最新文章

  1. SharePoint 补丁
  2. 用友t6怎么用文件服务器设置,用友T6库存管理选项设置:专用设置
  3. SSH的端口转发:本地转发Local Forward和远程转发Remote Forward
  4. 分组,命名分组,url的命名和反向解析
  5. cesium 经纬度绘制点_炫酷大屏地图自定义绘制(一)
  6. 巧用svn create patch(打补丁)方案解决定制版需求
  7. 发现一个HTML Form提交的小问题,不知道大家是不是都注意到了
  8. 2017-3-2 数据库索引/数据类型/引擎
  9. 计算机自动设置开机,电脑定时开机怎么设置?电脑设置每天自动开机
  10. 备考OCJP认证知识点总结(三)
  11. vba 位 前 相似 筛选_Excel VBA复制筛选的当前区域可见单元格,但排除最后3列
  12. 二元函数可导与可微的关系_二元函数可导、可微与连续性的关系.pdf
  13. word表格删除空白行java_在Word中怎样批量删除空行,这些点主要注意
  14. 2012第35周国内Android应用下载动态
  15. Telephony之TelephonyRegistry(原)
  16. 构建haproxy镜像(基于alpine系统)
  17. 微型计算机原理与接口技术第六版周荷琴答案
  18. 人工智能下一个热点探讨,为什么要提出互联网大脑模型
  19. 百度的工业互联网新解,“开物”加速工业智能化升级
  20. [画质提高30%利器]暴风5本地左眼爆破

热门文章

  1. c语言中各类型所占字节
  2. 杨百万:这么多年不倒 重要的是信奉落袋为安
  3. 杨百万:中国股市是政策市 炒股要听党和政府的话
  4. 笔记本酷睿i5 1135g7相当于什么水平?i5 1135g7性能怎么样
  5. SRS流媒体服务器:服务器读取RTMP推流数据
  6. 兄dei,听说你动画很卡?
  7. java里面几种锁的区别。。
  8. 机械臂正运动学(1)——MDH下的正解(自编)
  9. 双十一攒幸运值领红包,2022年天猫双11预售活动解读
  10. 帧、关键帧和空白关键帧的作用