You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot自定义配置文件加载失败:无法找到外部YML文件

问题分析与解决方案

你的判断完全正确:outputDirectory配置为${project.build.directory}/config时,Maven打包成Jar后,该目录的文件不会被包含在Jar内部——因为Spring Boot Jar默认只打包target/classes下的内容,而你把配置文件复制到了target/config,所以Jar里没有这个文件,启动时用file:前缀查找的是当前工作目录下的外部文件,自然找不到。

下面提供两种针对性解决方案:

方案一:将配置文件打包进Jar内部

这种方式适合把配置作为应用内置的一部分,无需外部额外放置文件。

  1. 修改maven-resources-plugin配置
    将outputDirectory改为${project.build.directory}/classes/config,这样复制的配置文件会进入classes目录,打包时自动被包含进Jar:
<executions>
    <execution>
        <id>copy-config</id>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <phase>generate-resources</phase>
        <configuration>
            <outputDirectory>${project.build.directory}/classes/config</outputDirectory>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/etc/config/</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
  1. 修改配置类的@PropertySource路径
    因为文件现在在Jar的classpath下,将前缀从file:改为classpath::
@ConfigurationProperties(prefix = "information")
@PropertySource(value = "classpath:config/advanced/information-properties.yml", ignoreResourceNotFound = true, factory = YamlPropertyLoaderFactory.class)
public class InformationPropertiesConfiguration {
    private ApplicationConfig application;

    @Data
    public class ApplicationConfig{
        private String applicationConfigPath;
    }
}

方案二:保持配置文件外部化(不打包进Jar)

这种方式适合需要随时修改配置、无需重新打包的场景。

  1. 调整Maven复制路径
    将outputDirectory改为项目根目录的config,这样编译后配置文件会出现在项目根目录,打包后你可以手动将这个config目录复制到Jar文件的同级目录:
<executions>
    <execution>
        <id>copy-config</id>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <phase>generate-resources</phase>
        <configuration>
            <outputDirectory>${project.basedir}/config</outputDirectory>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/etc/config/</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
  1. 确保启动时能找到文件
  • 启动Jar时,保证config目录和Jar文件在同一级目录下,此时file:config/advanced/information-properties.yml的路径就能正确找到文件。
  • 或者通过启动参数指定更明确的路径,避免工作目录影响:
java -jar your-app.jar --spring.config.location=file:./config/advanced/information-properties.yml

额外注意事项

确保你的YamlPropertyLoaderFactory实现正确,Spring默认的PropertySource不支持YAML格式,需要自定义工厂类加载:

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null) {
            return super.createPropertySource(name, resource);
        }
        YamlPropertiesFactoryLoader loader = new YamlPropertiesFactoryLoader();
        Properties properties = loader.load(resource.getResource());
        return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
    }
}

内容的提问来源于stack exchange,提问作者datta900

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 04:43:27