博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 搭建多模块调用以及打包执行
阅读量:4287 次
发布时间:2019-05-27

本文共 10136 字,大约阅读时间需要 33 分钟。

一 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

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

2.2 创建需要的各个子模块

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

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

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

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 父模块

4.0.0
huadian-api
huadian-common
huadain-service
huadian-domain
huadian-dao
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.huadian
huadian-multil-model
pom
1.0-SNAPSHOT
huadian-multil-model
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public/
true
org.apache.maven.plugins
maven-compiler-plugin
3.1
${java.version}
${java.version}
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
true

2.4.2 api子模块

4.0.0
com.huadian
huadian-multil-model
1.0-SNAPSHOT
../pom.xml
com.huadian
huadian-api
1.0-SNAPSHOT
huadian-api
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.huadian
huadian-domain
1.0-SNAPSHOT
com.huadian
huadian-common
1.0-SNAPSHOT
com.huadian
huadain-service
1.0-SNAPSHOT
com.huadian
huadian-dao
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
com.huadian.App
ZIP
repackage

2.4.3  service子模块

4.0.0
com.huadian
huadian-multil-model
1.0-SNAPSHOT
../pom.xml
com.huadian
huadain-service
1.0-SNAPSHOT
huadain-service
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
com.huadian
huadian-domain
1.0-SNAPSHOT
com.huadian
huadian-common
1.0-SNAPSHOT
com.huadian
huadian-dao
1.0-SNAPSHOT

2.4.4 dao子模块

4.0.0
com.huadian
huadian-multil-model
1.0-SNAPSHOT
../pom.xml
com.huadian
huadian-dao
1.0-SNAPSHOT
huadian-dao
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
com.huadian
huadian-domain
1.0-SNAPSHOT

2.4.5 common子模块

4.0.0
com.huadian
huadian-multil-model
1.0-SNAPSHOT
../pom.xml
com.huadian
huadian-common
jar
1.0-SNAPSHOT
huadian-common
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.huadian
huadian-domain
1.0-SNAPSHOT

2.4.6 domain子模块

4.0.0
huadian-api
huadian-common
huadain-service
huadian-domain
huadian-dao
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.huadian
huadian-multil-model
pom
1.0-SNAPSHOT
huadian-multil-model
http://www.example.com
UTF-8
1.8
1.8
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public/
true
org.apache.maven.plugins
maven-compiler-plugin
3.1
${java.version}
${java.version}
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
true

2.5 测试

三  打包

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

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

参考:

源代码见github

 

转载地址:http://satgi.baihongyu.com/

你可能感兴趣的文章
论文笔记|Open-world Learning and Application to Product Classification
查看>>
论文笔记 _ ELECTRA_ Pre-training Text Encoders as Discriminators Rather than Generators
查看>>
【论文笔记】
查看>>
论文笔记:Exploring Pre-trained Language Models for Event Extraction and Generation
查看>>
论文解读 | QANET: COMBINING LOCAL CONVOLUTION WITH GLOBAL SELF-ATTENTION FOR READING COMPREHENSION
查看>>
论文笔记|Get To The Point: Summarization with Pointer-Generator Networks
查看>>
linux安装MySQL主从同步(一主两从)搭建与配置
查看>>
linux上mysql从库故障恢复步骤(删除数据重新同步)
查看>>
linux上安装jdk
查看>>
linux 安装nginx
查看>>
linux 搭建rocketmq集群
查看>>
Linux 安装maven
查看>>
linux 安装reids集群
查看>>
linux 安装zookeeper集群
查看>>
RocketMq单机安装(Windows)
查看>>
Windows 上安装 MySQL
查看>>
CentOS-7搭建Gitlab服务器
查看>>
软件架构设计原则(我们为什么要学习)
查看>>
eclipse 的mybatis中mapper.xml文件标签没有提示的解决方法
查看>>
linux 上一主两从mysql集群中某台数据库宕机解决方法
查看>>