简体   繁体   English

在多模块Maven项目中使用配置文件

[英]Using Configuration files in multi-module maven project

I am working on multi-module maven project (newbe to multi-module maven projects). 我正在从事多模块Maven项目(新到多模块Maven项目)。

I have Module A with the directory structure /ModuleA/ConfigFiles/config.properties . 我的模块A具有目录结构/ModuleA/ConfigFiles/config.properties

And Module B which uses the Module A using maven dependency. 还有模块B ,它使用maven依赖关系来使用模块A。

Now Module A source file retrieves the config file as 现在, 模块A源文件将配置文件检索为

String configFile = System.getProperty("user.dir")+"\\ConfigFiles\\config.properties";

So when I call the Module A function from Module B , it is using Module B local project directory instead of Module A 's project directory. 因此,当我从模块B调用模块A函数时,它正在使用模块B的本地项目目录而不是模块A的项目目录。

Is there any solution/workaround to this in multi-module project ? 在多模块项目中是否有任何解决方案/变通办法?

I think that using resources mechanics here is the best approach. 我认为在这里使用资源机制是最好的方法。 Just place your config file somewhere in src/main/resources directory of Module A . 只需将您的配置文件放在Module A的 src/main/resources目录中的某个位置即可 Next change your code that read config file respectively ( here is some information how to read from resource files). 接下来更改您的代码分别读取配置文件( 这里是一些信息,如何从资源文件中读取)。

After that change you will read config from classpath, not from exact directory and there will be no difference whether you call your function from Module A or Module B . 更改之后,您将从类路径读取config,而不是从确切目录读取配置,并且从模块A还是模块B调用函数都没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM