简体   繁体   English

如何在多个独立的Maven项目之间共享configuration.properties文件?

[英]How to share configuration.properties file across multiple independent maven projects?

I have multiple maven projects. 我有多个Maven项目。 One of this is the core project which contains many classes used by other projects. 其中之一是核心项目,其中包含许多其他项目使用的类。

- Core project
  - config.properties file
  - SingletonClass
- Project A
  - ClassXX
- Project B
  - ClassYY

I have a properties.config file inside my core project. 我的核心项目中有一个properties.config文件。 In this core project I have a Singleton class which is used to load the properties.config file elements. 在这个核心项目中,我有一个Singleton类,该类用于加载properties.config文件元素。

Singleton class of core project loads properties file using the following relative path: 核心项目的Singleton类使用以下相对路径加载属性文件:

"/config.properties"

This means that it will search this file inside the current working dir. 这意味着它将在当前工作目录中搜索该文件。 This also mean that if I use the singleton class, the current working dir will be different. 这也意味着,如果我使用单例类,则当前的工作目录将有所不同。 For example, if project A uses my singleton class to load a config value, it will try to find config.properties file inside its root folder and so it will not be able to find it, becouse the real path is different (root folder of core project). 例如,如果项目A使用我的单例类加载配置值,它将尝试在其根文件夹中查找config.properties文件,因此将无法找到它,因为实际路径不同(根目录为核心项目)。

So my question is: 所以我的问题是:

Is there any way to share a config.properties file across multiple maven projects? 有什么方法可以在多个Maven项目之间共享config.properties文件?

EDIT: thanks to davidxxx, the best way was to use 编辑:感谢davidxxx,最好的方法是使用

InputStream is = MySingleton.class.getResourceAsStream("/conf/singleton.properties");

To let it works i have also create a resource folder(conf) and set it in build path. 为了使它起作用,我还创建了一个资源文件夹(conf)并将其设置在构建路径中。 I also have had to specify what to package in my core project pom.xml as follow: 我还必须指定要打包到我的核心项目pom.xml中的内容,如下所示:

<resources>
    <resource>
        <directory>src/conf</directory>
        <includes>
            <include>*.properties</include>
        </includes>
    </resource>
</resources>

What you can do is to use the Properties Maven plugin here . 您可以在这里使用Properties Maven插件。 This will let you define your properties in an external file, and the plugin will read this file. 这将使您可以在外部文件中定义属性,并且插件将读取该文件。

With this configuration : 使用此配置:

<build>
<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>read-project-properties</goal>
                </goals>
                <configuration>
                    <files>
                        <file>my-file.properties</file>
                    </files>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

and if you have, in your properties file the following lines: 如果有的话,在属性文件中包含以下几行:

spring-version=1.0
mysql-version=4.0.0

then it's the same thing as if you wrote, in your pom.xml, the following lines: 就像是在pom.xml中写了以下几行一样:

<properties>
  <spring-version>1.0</spring-version>
  <mysql-version>4.0.0</mysql-version>
</properties>

Using this plugin, you will have several benefits: 使用此插件,您将获得以下好处:

  1. Set easily a long list of properties 轻松设置一长串属性
  2. Modify the values of these properties without modifying the parent pom.xml. 修改这些属性的值而不修改父pom.xml。

This mean that it will search this file inside the current working dir. 这意味着它将在当前工作目录中搜索该文件。 This also mean that if i use the singleton class, the current working dir will be different. 这也意味着如果我使用单例类,则当前的工作目录将有所不同。

The problem is the way you are using to retrieve the properties file. 问题是您用来检索属性文件的方式。
If your singleton needs to be exported as a dependency in a JAR, you have not to use the current working directory to load the properties file. 如果需要将单例作为JAR中的依赖项导出,则不必使用当前工作目录来加载属性文件。
The properties file should be in the classpath and the singleton class could use its classloader to get the resource. 属性文件应位于类路径中,并且单例类可以使用其类加载器来获取资源。

For example suppose the properties is located in the conf folder that is added to the runtime classpath, in your singleton class you could write something like that to load the resources : 例如,假设属性位于添加到运行时类路径的conf文件夹中,则在您的singleton类中,您可以编写类似的内容来加载资源:

InputStream is = MySingleton.class.getResourceAsStream("/conf/singleton.properties");

With this way of doing, this will work in both cases. 通过这种方式,这将在两种情况下均有效。

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

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