Java项目分模块打jar包

您所在的位置:网站首页 打包java项目为jar包 Java项目分模块打jar包

Java项目分模块打jar包

2023-07-13 07:12| 来源: 网络整理| 查看: 265

这段时间接到了一个任务,就是将项目按照模块打jar包。这样的目的是:

1.按照模块分别打包,方便升级

2.模块分包之后,哪个模块修改了,就去单独更新对应模块,不用担心整体更新之后,由于其他模块的修改导致项目运行异常

在网上搜寻了很久没找到了一些相关文章,但是没有详细介绍按照模块打jar包的相关文章。经过我这几天的摸索,实现了相关功能。

首先说下思路:

我们之前接触到的打包方式基本有两种:1.将整个项目打包成一个完整jar包运行 2.把项目打包成war包,丢到tomcat中运行。

multi-module-很显然,这两种方式都满足不了上述需求。于是,我开始另辟蹊径,将原来的项目分成3个module(multi-module-core:主要是依赖包, multi-module-service:各种接口实现 ,,ulti-module-api:项目配置文件等)。这样就将项目分成了三大块,每大块相互独立。multi-module-service模块中涉及到各个接口实现,如果有需要的话,还可以将此模块继续细分成多个module进行依赖。

打包完成之后,multi-module-service被单独打成了一个jar,在后续开发过程中,直接在发布包中替换这个jar就可以完成局部更新了,是不是很神奇呢,哈哈!

具体的依赖关系如下图所示:

打包之后的文件如下图所示,后续更新只需要替换multi-module-core和multi-module-service的jar包即可:

全局pom文件,这里主要是标签中需要声明当前项目中包含的module:

4.0.0 com.multimodule multi-module-parent 1.0.0 pom org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE multi-module-service multi-module-core multi-module-api UTF-8 1.8 UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.apache.maven.plugins maven-compiler-plugin 3.1 ${java.version} ${java.version} org.apache.maven.plugins maven-surefire-plugin 2.19.1 true

multi-module-core:

这里的重点是:

1.添加springboot相关依赖

2.添加诸如mysql等相关依赖

multi-module-parent com.multimodule 1.0.0 4.0.0 multi-module-core org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.springframework.boot spring-boot-starter-web

multi-module-service:

这里的重点是:

1.声明依赖核心module:multi-module-core

4.0.0 multi-module-parent com.multimodule 1.0.0 com.multimodule multi-module-service 1.0.0 multi-module-service multi-module-service 1.8 1.2.9 org.springframework.boot spring-boot-configuration-processor true com.multimodule multi-module-core 1.0.0

multi-module-api:

这里的重点是:

1.   标签中需要声明主启动类

2.声明依赖service module:multi-module-service

multi-module-parent com.multimodule 1.0.0 war 4.0.0 multi-module-api com.multimodule multi-module-service 1.0.0 org.springframework.boot spring-boot-maven-plugin com.multi.moduleapi.MultiModuleServiceApplication ZIP repackage

上述是配置文件的相互依赖,下面介绍下重点,启动类:

其中需要声明:

1.@MapperScan("com.multi.module.mapper"):这个的主要功能是声明数据库mapper所在位置 2.@ComponentScan(basePackages = {"com.multi.module.*"}):这个声明的主要功能是加载各种类

package com.multi.moduleapi; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @MapperScan("com.multi.module.mapper") @ComponentScan(basePackages = {"com.multi.module.*"}) public class MultiModuleServiceApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(MultiModuleServiceApplication.class, args); } }

接口测试:

我在multi-module-service module中写了个测试接口:

package com.multi.module.test; import org.springframework.web.bind.annotation.*; /** * @Author: Jiang * @Description: 字典表管理 * @Date: 2022-03-04 10:29 * @Version: 1.0 * @Update: */ @RestController @RequestMapping("/Operating") public class MultiModuleController { /** * 测试接口 * * @return */ @GetMapping("Test") public String test() { return "Test"; } }

测试结果:

 打包方式:

首先点击右侧栏的mavean,接着展开multi-module-parent,找到package按钮,即可执行打包

 打包后的文件:

此后只需要更新multi-module-core以及multi-module-service两个jar包即可实现项目升级:

资源传送门:项目地址



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3