如何让我的ClassLoader资源使用我的maven属性?

时间:2015-06-24 14:11:49

标签: java java-ee environment-variables maven-3

我在Java EE中有一个BaseTask,它提供了一个到其他类的URL。此网址会根据我们是否为devtestprod构建而更改。

在我的src/main/resources我有一个名为server.properties的文件。该文件有一个条目,如:

urls.main=${urls.main}

接下来,我们在dev.properties中有三个名为test.propertiesprod.propertiessrc/main/filters的文件。

每个文件都有一个条目,但作为示例,dev.properties包含如下条目:

urls.main=http://localhost/somepath

现在,我的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>

好的,现在我可以执行以下操作:

mvn -Pdev package

并且EAR文件与server.properties文件一起构建,其中包含一个条目,如:

urls.main=http://localhost/somepath

因此实际文件在EAR文件中正确呈现。但是,当我尝试从Java加载属性时,它使用${urls.main}代替。

我在Java中用来加载属性的代码是:

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;
}

我确定我错过了一些愚蠢的事情,但我做错了什么?

感谢。

修改

顺便说一句,这不是所有的代码,我关闭了流,等等。我只是没有发布所有内容,以便我可以让帖子更容易阅读。 : - )

0 个答案:

没有答案
相关问题