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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With