在eclipse中使用maven配置文件 - 资源流NPE

时间:2014-07-15 17:20:02

标签: java maven properties nullpointerexception null

我正在尝试使用mavens配置文件来区分我的开发帐户和生产帐户 - 到目前为止,我在我的pom.xml文件中声明了以下两个配置文件

<profiles>
    <profile>
        <!-- When built in OpenShift the openshift profile will be used when invoking 
            mvn. -->
        <!-- Use this profile for any OpenShift specific customization your app 
            will need. -->
        <!-- By default that is to put the resulting archive into the deployments 
            folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>openshift</id>
        <build>
        <resources>
            <resource>
                <directory>src/main/resources/PROD/</directory>
            </resource>
        </resources>
            <finalName>TomcatHotOrNot</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <outputDirectory>deployments</outputDirectory>
                        <warName>TomcatHotOrNot</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
<profile>
        <id>openshift-dev</id>
        <build>
        <resources>
            <resource>
                <directory>src/main/resources/DEV/</directory>
            </resource>
        </resources>
            <finalName>TomcatHotOrNot</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <outputDirectory>deployments</outputDirectory>
                        <warName>TomcatHotOrNot</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这意味着它应该指向dev / prod属性文件,具体取决于我在eclipse菜单中选择的内容。要指向我创建的配置文件,我已完成以下操作;

右键单击pom.xml - &gt;选择maven个人资料 - &gt;选择/勾选openshift-dev

在我的代码中,然后尝试加载像这样的属性文件;

String resourceName = "connection.properties";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
} catch (IOException e) {
    e.printStackTrace();
    logger.error("Unable to load resources file", e);
}

我面临的问题是抛出空指针异常,因为资源流为空。我缺少哪些配置以确保eclipse可以正确加载属性文件?

由于

0 个答案:

没有答案