当前位置: 首页 > news >正文

如何利用maven更优雅的打包

最近在客户现场部署项目,有两套环境,无法连接互联网,两套环境之间也是完全隔离,于是问题就来了,每次都要远程到公司电脑改完代码,打包,通过网盘(如果没有会员,上传下载慢)或者远程工具传输,再用u盘拷贝到客户电脑,一个包400多Mb(所有的代码和依赖包以及配置文件都打在了一个jar包里面),传输一次10分钟,效率太低了,于是翻越资料找到一中更优雅的打包方式,每次只需要更新改过的包和配置文件即可,大大减少传输大小。

我们先看看nacos的压缩包解压开来的效果:

可以看到是一种很标准的打包方式,解压开来的文件夹都能够见名知意,下面是我改完maven打包方式之后的效果,实际过程中,也可以根据需要把脚本放到bin目录里面,只是我图简单,把停止和启动都写在了一个脚本里面,这么打包的好处是,后续改了代码和配置只需要替换相应的jar包和配置文件即可,而不用全量更新,我们自己写的代码jar包通常都是kb单位,远程桌面传输起来也很快:

conf:配置文件目录里面包含了logback配置文件,项目依赖的application.yml等配置文件:

lib:依赖的jar,包括第三方依赖包和自己项目里面的common,api等:

gisadmin-boot.jar:springboot启动类所在的jar包,start.sh启动脚本里面启动命令指定的就是这个jar

start.sh:启动脚本:

ps -ef | grep gisadmin-boot | grep -v grep | awk '{print $2}' | xargs -r kill -9# 等待几秒以确保进程被正确杀死
sleep 2# 确保日志目录存在
mkdir -p ./logs# 绝对路径启动 Java
nohup java -Duser.timezone=Asia/Shanghai -Dfile.encoding=utf-8 -cp ./gisadmin-boot.jar com.daspatial.gis.EarthApplication  > ./logs/gisadmin.log 2>&1 &sleep 2

下面就是关键的maven配置了(只写关键变更部分):

1、不同的环境,激活不同的配置:在maven打包命令执行的时候指定,比如mvn clean compile package -Pdev -DskipTests,其中-Pdev就是激活dev环境的配置

    <profiles><profile><id>dev</id><properties><profileActive>dev</profileActive></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>wutiegaw</id><properties><profileActive>wutiegaw</profileActive></properties></profile><profile><id>wutie</id><properties><profileActive>wutie</profileActive></properties></profile><profile><id>prod</id><properties><profileActive>prod</profileActive></properties></profile></profiles>

2、build节点配置:

<build><!--最终打出来的包名--><finalName>${project.artifactId}</finalName><!--java代码源文件目录--><sourceDirectory>${basedir}/src/main/java</sourceDirectory><!--资源文件目录,主要是配置文件,也就是src/main/resources目录--><resources><resource><directory>${basedir}/src/main/java</directory><filtering>true</filtering><excludes><exclude>**/*.java</exclude></excludes></resource><resource><directory>${basedir}/src/main/resources</directory><excludes><exclude>**/assembly.xml</exclude></excludes></resource><resource><directory>${basedir}/src/main/env/${profileActive}</directory></resource></resources><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifestEntries><!--配置文件独立输出到conf目录--><Class-Path>conf/</Class-Path></manifestEntries><manifest><!--依赖包独立输出到lib目录--><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix></manifest></archive><excludes><!--这里是为了防止配置文件被打进启动类所在的jar里面--><exclude>**/*.yml</exclude><exclude>**/*.xml</exclude></excludes></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>2.6</version><configuration><appendAssemblyId>false</appendAssemblyId><descriptors><!--assembly插件配置文件--><descriptor>${basedir}/src/main/resources/assembly.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins></build>

3、assembly插件配置文件assembly.xml:

<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"><id>${version}</id><formats><!-- zip,tar,tar.gz,tar.bz2,jar,dir,war --><format>tar.gz</format></formats><includeBaseDirectory>true</includeBaseDirectory><fileSets><fileSet><!--源代码中的bin目录输出到压缩包根目录--><directoryMode>0777</directoryMode><directory>${basedir}/src/bin</directory><fileMode>0777</fileMode><outputDirectory>.</outputDirectory><lineEnding>unix</lineEnding></fileSet><fileSet><!--激活的profile配置文件输出到conf目录--><directory>${basedir}/src/main/env/${profileActive}</directory><outputDirectory>conf</outputDirectory></fileSet></fileSets><files><file><!--logback配置文件输出到conf目录--><source>${basedir}/src/main/resources/logback.xml</source><outputDirectory>conf</outputDirectory></file><file><source>${basedir}/src/main/resources/banner.txt</source><outputDirectory>conf</outputDirectory></file><file><!--启动类jar输出到压缩包根目录--><source>${basedir}/target/${project.artifactId}.jar</source><outputDirectory>.</outputDirectory></file></files><dependencySets><dependencySet><useProjectArtifact>true</useProjectArtifact><outputDirectory>lib</outputDirectory><excludes><exclude>${project.groupId}:${project.artifactId}</exclude></excludes></dependencySet></dependencySets>
</assembly>

项目目录结构如下:

改完之后可能会出现spring无法加载配置文件,只需要右键选中dev环境加入到资源目录即可,也就是默认激活dev环境配置:

http://www.lryc.cn/news/533822.html

相关文章:

  • 音频进阶学习十二——Z变换一(Z变换、收敛域、性质与定理)
  • cursor指令工具
  • MySQL 主从读写分离实现方案(一)—MariaDB MaxScale实现mysql8读写分离
  • 阿里云 | DeepSeek人工智能大模型安装部署
  • LLAMA-Factory安装教程(解决报错cannot allocate memory in static TLS block的问题)
  • STM32 CUBE Can调试
  • MySQL数据存储- 索引组织表
  • 基于STM32设计的仓库环境监测与预警系统
  • VSCode便捷开发
  • 理解 Maven 的 pom.xml 文件
  • docker数据持久化的意义
  • opentelemetry-collector 配置elasticsearch
  • ASP.NET Core JWT Version
  • 【ArcGIS】R语言空间分析、模拟预测与可视化技术
  • 日常知识点之面试后反思遗留问题汇总
  • 链表(LinkedList) 1
  • Qt:Qt Creator项目创建
  • windows11上,使用pipx安装Poetry,Poetry的安装路径是什么?
  • 详解状态模式
  • 能否通过蓝牙建立TCP/IP连接来传输数据
  • uniapp mqttjs 小程序开发
  • 爬虫工程师分享:获取京东商品详情SKU数据的技术难点与攻破方法
  • 数据库操作与数据管理——Rust 与 SQLite 的集成
  • LeetCode 0063.不同路径 II:动态规划 - 原地使用地图数组,几乎无额外空间开销
  • elementui:el-table支持搜索、切换分页多选功能,以及数据回显
  • 深度整理总结MySQL——索引正确使用姿势
  • 使用LLaMA Factory踩坑记录
  • 亚博microros小车-原生ubuntu支持系列:25 二维码控制运动
  • 基于深度学习的人工智能量化衰老模型构建与全流程应用研究
  • 【医院运营统计专题】2.运营统计:医院管理的“智慧大脑”