记一起Spring Boot读取外部配置失败问题

299 阅读1分钟

外部配置文件读取失败问题

  • 可执行zip包的结构如下:

    • *.zip

      • *.jar

        • license:

          • license.dat
    • license:

      • license.dat
  • 期望:程序启动,读取Jar外部的license/license.dat

  • 结果:程序启动,读取了*.jar文件内部的license/license.dat

  • 解决:

    1. 打包时,不要将license/license.dat打进Jar包
    2. 指定路径,保证外部的license/license.dat能够被读取

打包不包含license/license.dat

pom.xml中,添加如下配置

    <build>
        <plugins>
        	<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <excludes>
                        <exclude>license/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

指定外部路径

application.yml中增加如下配置:

server:
#  address: ${spring.cloud.client.ip-address}
  port: 7090
  tongweb:
    license:
      type: file
      # license文件所在路径
      path: ./license/license.dat

指向相对于Jar文件的相对路径,而不使用原先的classpath:license/license.dat读取上下文中的文件。