springboot如何用jar包启动,同时为不同机房设置不同的配置文件

1、首先先把配置文件从jar中抽离

示例代码:

复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <excludes>
            <exclude>**/spring-xxx.xml</exclude>
        </excludes>
    </configuration>
</plugin>

2、把抽离的配置文件,放到conf目录下

利用maven-assembly-plugin,抽取配置文件到conf目录下,

示例代码:

复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>


assembly.xml内容如下:
<assembly>
  <id>assembly</id>
  <formats>
    <format>zip</format>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${basedir}/src/bin</directory>
      <outputDirectory>bin</outputDirectory>
      <fileMode>0755</fileMode>
    </fileSet>
  </fileSets>
  <files>
    <file>
      <source>${project.build.directory}/${project.build.finalName}.jar</source>
      <outputDirectory>lib</outputDirectory>
    </file>
    <file>
      <source>${basedir}/../xxx/target/classes/spring/spring-xxx.xml</source>
      <outputDirectory>conf</outputDirectory>
    </file>
    <file>
      <source>${basedir}/../xxx/target/classes/spring/spring-xxx.xml</source>
      <outputDirectory>conf</outputDirectory>
    </file>
  </files>

</assembly>

最终效果如下:

3、修改maven打包配置将conf加入到classpath中

示例代码如下:

复制代码
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
 <manifestEntries>
                            <Class-Path>../conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

最终达到的效果是:

将springboot的jar包解压后,可以看到.MF文件中加了一个类路径 ../conf

特别注意:

java -jar XX

使用-jar启动java进程的,-classpath不会生效了,如果要加类路径,只能通过改maven的打包参数,从而使得.MF文件加了Class-Path属性后,才可以!!!!

4、到部署平台上新建conf文件夹,将要覆盖的配置文件加入进去

这样,部署平台的配置就会覆盖maven打包出来的配置文件

5、测试是否生效

在部署平台上,将部署平台上的配置文件里的,rpc框架的服务别名设置为:xxx

代码里的服务别名是yyy,然后通过测试发现生效的别名是xxx。

因为,springboot启动后,使用的是conf下配置文件,然后conf下的配置文件会被部署平台上新建的配置文件覆盖,

这样为不同的机房新建不同的配置文件,这样也就实现了springboot的项目如何既要用jar包启动,同时还可以为不同的机房设置不同的配置文件

作者:京东科技 李意文

来源:京东云开发者社区 转载请注明来源

相关推荐
下次再写3 小时前
深入浅出微服务架构:从理论到Spring Boot实战
java·微服务·springboot·springcloud·架构设计·后端开发·分布式系统
牛奶咖啡135 小时前
CI/CD——在jenkins中构建流程实现springboot项目的自动化构建与部署
java·ci/cd·k8s·jenkins·springboot·springboot制作镜像·使用源码项目制作镜像
凤山老林6 小时前
慢SQL治理:索引优化实战指南——从定位到优化的完整解决方案
java·sql·springboot·慢sql治理·sql 性能优化
哆啦A梦15882 天前
01, 前端vue3框架的快速搭建以及项目工程的讲解
前端·vue3·springboot
桃花键神3 天前
【2026精品项目】基于SpringBoot3+Vue3的校园小卖铺系统(包含源码+项目文档+SQL脚本+部署教程)
数据库·sql·vue·毕业设计·springboot
洛阳泰山4 天前
Maxkb4j集成sqlbot MCP实现企业智能问数智能体
java·ai·springboot·agent·智能问数
zc.z5 天前
基于 LangChain4j 的 RAG 工作流智能体实战
langchain·大模型·springboot·rag智能体
suweijie7685 天前
Nacos配置读取异常排查与解决指南
微服务·nacos·springboot·配置中心·问题排查
程序员老邢6 天前
【产品底稿 12】工程架构最终定型:完整模块拆分、分包规范、层级依赖与开发规约全清单
微服务·架构·springboot·多模块·技术债务
abcnull6 天前
Springboot+Vue2的Web项目小白入门Demo快速学习!
java·elementui·vue·maven·springboot·web·小白