Orazio Contarino
Orazio Contarino

Reputation: 103

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

I have multiple maven projects. 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. In this core project I have a Singleton class which is used to load the properties.config file elements.

Singleton class of core project loads properties file using the following relative path:

"/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).

So my question is:

Is there any way to share a config.properties file across multiple maven projects?

EDIT: thanks to davidxxx, the best way was to use

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. I also have had to specify what to package in my core project pom.xml as follow:

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

Upvotes: 0

Views: 1939

Answers (2)

davidxxx
davidxxx

Reputation: 131326

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.
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 :

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

With this way of doing, this will work in both cases.

Upvotes: 1

Anshul Sharma
Anshul Sharma

Reputation: 3522

What you can do is to use the Properties Maven plugin here. 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:

<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.

Upvotes: 1

Related Questions