0

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!

Community
  • 1
  • 1
zorglub76
  • 4,852
  • 7
  • 37
  • 46
  • what happens if you move properites file to main source root instead of test ? i mean move test.properties to /src/main/resources – smajlo Nov 20 '13 at 09:01
  • Tried that. Even with `src\test\resources` file gets copied to `target\classes` directory, but it could not be found. I also have the same file (but with ${application.root.url} not changed) in `target\test-classes`, but still getting 'file not found' exception.... – zorglub76 Nov 20 '13 at 09:37
  • 1
    ` src/test/resources true ` – MariuszS Nov 20 '13 at 10:11

1 Answers1

2

change

${project.application.root.url}

to

${application.root.url}
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39