mercredi 24 juin 2015

How can I get my ClassLoader resource to use my maven properties?

I have a BaseTask in Java EE that provides a URL to other classes. This URL changes depending on if we are building for dev, test or prod.

In my src/main/resources I have a file called server.properties. This file has one entry like:

urls.main=${urls.main}

Next, we have three files named dev.properties, test.properties and prod.properties located in src/main/filters.

Each of those files have one entry but as an example, dev.properties contains an entry like:

urls.main=http://localhost/somepath

Now, I have the following setup in my pom:

<profiles>
    <profile>
        <id>dev</id>
        <build>
            <filters>
                <filter>src/main/filters/dev.properties</filter>
            </filters>

            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    ... repeat for "test.properties" and "prod.properties"...
</profiles>

OK, so now I can perform the following:

mvn -Pdev package

and the EAR file is built along with a server.properties file with an entry in it like:

urls.main=http://localhost/somepath

So the actual file renders correctly inside the EAR file. But, when I try to load the property from Java, it uses ${urls.main} instead.

The code I'm using in Java to load the property is:

private static final String getURL() {

    String url = "";
    try {
        url = getProperty("urls.main");     // returns "${urls.main}" instead of http://localhost/somepath
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return url;
}

I'm sure I'm missing something stupid but what am I doing wrong?

Thanks.

EDIT

BTW, This isn't all of the code, I do close out streams, etc. I just didn't post everything so that I can keep the post easier to read. :-)

Aucun commentaire:

Enregistrer un commentaire