Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an external properties file from a maven java project

I have a maven java project, with a properties file in the src/main/resources directory. I've packaged the jar without the properties file in the jar, so it can be deployed to different environments with different settings, but the unit tests are failing

The project structure is

Properties Application Project
|-- pom.xml
`-- src
    |-- main
        |-- java
        |   `-- java classes
        |-- resources
        |   `-- myProps.properties
`-- target
    |--properties-app.jar
       myProps.properties

The values are loaded in this file

public class MyProperties {

    private Properties myProperties;

    public MyProperties() {

        File file = new File("./myProps.properties");
        try {

            myProperties = new Properties();
            FileInputStream myPropertiesInputStream = new FileInputStream(file);
            myProperties.load(myPropertiesInputStream);

        } catch (IOException e) {

            e.printStackTrace();

        }        

    }

    public Properties getPropertyValue() {
        return myProperties.getProperty("value");
    }

}

The unit test is as follows

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class MyPropertiesTest {

    private MyProperties myProperties;

    @Before
    public void setup() {

        myProperties = new MyProperties();

    }

    @Test
    public void testMyProperties() {

        assertNotNull(myProperties.getPropertyValue());

    }

}    

What should I use to load the properties file from the target directory so I can build it successfully from the command line?

like image 445
Joseph McCarthy Avatar asked Jan 11 '16 01:01

Joseph McCarthy


2 Answers

I hope you are using maven jar plugin. If so use this configuration inside jar-plugin

    <configuration>
      <archive>
        <manifestEntries>
          <Class-Path>.</Class-Path>
        </manifestEntries>
      </archive>
    </configuration>

Then the jar will take the properties file from root directory of jar file.

like image 141
Shettyh Avatar answered Oct 02 '22 23:10

Shettyh


Solved it with both suggestions, plus a few changes to the pom. Thank you both.

In the pom, the properties file from the resource directory is copied to the target directory, and is excluded from the jar file created by the pom

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>properties</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>properties</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <reuseForks>false</reuseForks>
                    <forkCount>1</forkCount>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.properties.MyProperties</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

In MyProperties, the file is loaded using the getClass().getResourceAsStream() method

myProperties.load(getClass().getResourceAsStream("/myProps.properties"));

All tests pass now, and the jar file runs from the command line and loads the properties file.

I've uploaded the resulting project to github here: https://github.com/tetsujin1979/ExternalProperties

like image 33
Joseph McCarthy Avatar answered Oct 02 '22 23:10

Joseph McCarthy