V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lwj0821
V2EX  ›  Java

有什么思路可以使打 jar 包的时候自定义配置文件

  •  
  •   lwj0821 · 2019-05-23 11:45:22 +08:00 · 2392 次点击
    这是一个创建于 1772 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前是用 maven 的 profile 实现多数据源配置文件,但是需求要运维人员可以实现自己输入数据库的连接地址! 有类似 mvn package -自定义配置 jdbc:oracle:thin:@127.0.0.1:1521/dev 的打包方式么?

    14 条回复    2019-05-30 21:47:25 +08:00
    lululau
        1
    lululau  
       2019-05-23 11:49:43 +08:00
    配置放在 jar 的外面,让你的程序支持从指定的文件读取配置,比如 spring-boot 就是支持的
    yinzhili
        2
    yinzhili  
       2019-05-23 13:11:41 +08:00
    类似 --spring.config.location=/path-to/config.properties 这样指定外部配置就可以了
    lwj0821
        3
    lwj0821  
    OP
       2019-05-23 13:55:15 +08:00
    @lululau 这样的话放在服务器上是没问题的,办事本地开发的时候就要在改配置指向自己的目录,需求要本地开发,运维都不需要改配置。。。
    HuHui
        4
    HuHui  
       2019-05-23 13:57:08 +08:00
    直接 config server 一步到位吧
    eefnrowe
        5
    eefnrowe  
       2019-05-23 17:03:57 +08:00
    推荐 apollo
    gaius
        6
    gaius  
       2019-05-23 17:18:41 +08:00
    hosts 吧,简单
    bianjp
        7
    bianjp  
       2019-05-23 23:20:51 +08:00
    xuanbg
        8
    xuanbg  
       2019-05-24 02:20:22 +08:00
    配置中心搞起来
    guixiexiezou
        9
    guixiexiezou  
       2019-05-24 09:53:35 +08:00
    把配置文件放 jar 外面一层不就可以了。这年头真把配置文件放 jar 里面,真的是稳定啊
    lwj0821
        10
    lwj0821  
    OP
       2019-05-24 10:23:37 +08:00
    @guixiexiezou,已经解决了,在运维发布时读取外部配置文件,开发时读取内部配置文件。。。对于多配置文件开发运维切换也是麻烦的事。
    pmispig
        11
    pmispig  
       2019-05-24 11:27:24 +08:00
    @Component
    @PropertySource(value = "file:d:/java/object/application.properties",ignoreResourceNotFound = true,encoding="utf-8")
    @PropertySource(value = "file:/app/config/application.properties",ignoreResourceNotFound = true,encoding="utf-8")

    这样子就行了,开发和线上都能读外部
    lwj0821
        12
    lwj0821  
    OP
       2019-05-24 14:49:34 +08:00
    @pmispig 因为项目用的 hibernate,我在 xml 里配置
    <!--本地配置-->
    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:config/config.properties</value>
    </list>
    </property>
    </bean>
    <!--线上配置-->
    <context:property-placeholder location="file:xxx/config/config.properties" order="1" ignore-resource-not-found="true" ignore-unresolvable="true" />

    这个首先会走线上,如果找不到配置文件会走本地的配置
    lazyfighter
        13
    lazyfighter  
       2019-05-30 18:51:07 +08:00
    模板文件,上线的时候 ci 负责渲染
    lwj0821
        14
    lwj0821  
    OP
       2019-05-30 21:47:25 +08:00
    这是我后续的解决方法:直接修改 jar 包内文件
    ( 1 )使用 jar tvf jar 名称 | grep 目标文件名 查询出目标文件在 jar 包中的目录
    :jar tvf service-0.0.1-SNAPSHOT.jar |grep config.properties
    ( 2 )使用 jar xvf jar 名称 目标文件名(copy 上面查出的全路径) 将目标文件及所在 jar 包中的目录解压到当前路径
    :jar xvf service-0.0.1-SNAPSHOT.jar BOOT-INF/classes/config/config.properties
    ( 3 )修改目标文件的内容,或者将要新的目标文件替换掉提取出来的目标文件
    :vim BOOT-INF/classes/config/config.properties

    :cp config/config.properties BOOT-INF/classes/config/config.properties
    ( 4 )使用 jar uvf jar 名称 目标文件名(和步骤( 2 )中的目标文件名相同) 将新目标文件替换到 jar 包中
    :jar uvf service-0.0.1-SNAPSHOT.jar BOOT-INF/classes/config/config.properties
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5395 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 07:05 · PVG 15:05 · LAX 00:05 · JFK 03:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.