I tried using properties in Maven following this thread
I've created a property:
<properties>
<application.root.url>http://localhost:8080</application.root.url>
</properties>
added resource and filtering:
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
put the "test.properties" file in src/test/resources directory with the following:
application.root.url=${project.application.root.url}
In Java, I have this:
public static Properties getProperties(){
Properties props = new Properties();
try {
props.load(new FileInputStream("test.properties"));
} catch (IOException e){
props = null;
e.printStackTrace();
}
return props;
}
Now, if I have new FileInputStream("test.properties")
, I get "file not found" exception, and if I put new FileInputStream("src/test/resources/test.properties")
, then it returns ${application.root.url}
as a value for the property application.root.url
What am I doing wrong?
UPDATE 1:
OK, now I've changed
${project.application.root.url}
to
${application.root.url}
and
props.load(new FileInputStream("test.properties"));
to
props.load(Util.class.getClassLoader().getResourceAsStream("test.properties"));
Now I don't get 'file not found' errors any more. But I still have problem that in target/test-classes/test.properties
value is not changed from ${application.root.url}
(and in target/classes/test.properties
it is changed...)
FINAL UPDATE:
And finally, everything got to work when I changed 'resources' to 'testResoruces':
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
Thank you all for the suggestions!