如何从另一个Maven模块读取配置文件?
解决方案:跨Maven模块读取配置文件(不添加模块依赖)
可以在不添加project-config依赖到project-algo的前提下实现需求,推荐以下两种可靠方案:
方案一:Maven资源自动复制(推荐)
通过Maven的maven-resources-plugin,在构建测试阶段自动将project-config中的配置文件复制到project-algo的测试类路径下,无需修改核心读取逻辑,还能保证配置文件自动同步。
步骤1:修改project-algo的pom.xml
添加资源复制插件配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-configs</id> <phase>process-test-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <!-- 复制到测试类输出目录,保证能被类路径读取 --> <outputDirectory>${project.build.testOutputDirectory}</outputDirectory> <resources> <resource> <!-- 父项目下project-config的configs目录路径 --> <directory>${project.parent.basedir}/project-config/configs</directory> <includes> <include>application_config.yaml</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>
步骤2:优化YamlReader的读取逻辑
将原来基于文件系统相对路径的读取方式改为类路径读取,更稳定且适配Maven构建环境:
public static <C extends ConfigParent> C readConfiguration(Class<C> configClass, String yamlFileName) throws IOException { // 通过类加载器读取类路径下的资源 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(yamlFileName); if (inputStream == null) { throw new FileNotFoundException("配置文件 " + yamlFileName + " 未在类路径中找到"); } YAMLMapper mapper = new YAMLMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); return mapper.readValue(inputStream, configClass); }
之后运行project-algo的测试时,Maven会自动复制配置文件,调用readConfiguration("application_config.yaml", ...)即可直接读取。
方案二:通过系统属性传递配置文件路径
如果不想修改资源复制配置,可以通过Maven传递配置文件的绝对路径,让代码读取该路径下的文件。
步骤1:修改project-algo的pom.xml
在maven-surefire-plugin中添加系统属性,传递配置文件路径:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <systemPropertyVariables> <config.path>${project.parent.basedir}/project-config/configs/application_config.yaml</config.path> </systemPropertyVariables> </configuration> </plugin> </plugins> </build>
步骤2:修改YamlReader读取逻辑
读取系统属性中的路径:
public static <C extends ConfigParent> C readConfiguration(Class<C> configClass) throws IOException { String configPath = System.getProperty("config.path"); if (configPath == null || configPath.isBlank()) { throw new IllegalArgumentException("未配置系统属性 config.path"); } InputStream inputStream = new FileInputStream(new File(configPath)); YAMLMapper mapper = new YAMLMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); return mapper.readValue(inputStream, configClass); }
原尝试失败原因说明
- 直接使用
./project-config/configs/...的相对路径:Maven运行测试时的工作目录是project-algo模块的根目录,而非整个项目的根目录,因此找不到上级目录的文件。 - ModuleLayer读取方式:
project-config未被加入project-algo测试的模块路径(无依赖关系),因此无法通过模块层获取资源。
内容的提问来源于stack exchange,提问作者Paul Marcelin Bejan
相关产品推荐
相关产品推荐

