从IDE加载.properties文件,也从JAR外部加载.properties文件

时间:2015-04-23 12:35:17

标签: java maven properties jar

我的maven项目中的app.properties文件位于resources文件夹下,如下所示(简化):

myApp
  |----src
  |     |
  |     |--main
  |         |--java
  |         |    |--ApplicationInitializer.java
  |         |
  |         |--resources
  |              |--app.properties
  |
  |---target
        |--myApp.jar
        |--app.properties     

ApplicationInitializer类中,我想使用以下代码从app.properties文件加载属性:

Properties props = new Properties();

String path = "/app.properties";

try {
    props.load(ApplicationInitializer.class.getResourceAsStream(path));
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println(props.getProperty("property"));

这段代码在我从IDE内部运行时正确加载属性但是失败并带有异常

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at cz.muni.fi.fits.ApplicationInitializer.main(ApplicationInitializer.java:18)

尝试以JAR文件运行时。

为了创建一个jar文件,我使用了 maven-shade-plugin maven-jar-plugin 的组合(用于排除JAR之外的属性文件)和< b> maven-resources-plugin (用于将属性文件复制到特定文件夹)在pom.xml文件中,如下所示:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>cz.muni.fi.fits.ApplicationInitializer</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <excludes>
                <exclude>**/*.properties</exclude>
            </excludes>
        </configuration>
    </plugin>

    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
            <execution>
                <id>copy-resource</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${basedir}/target</outputDirectory>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>

然后我将main方法中的代码切换到这个:

Properties props = new Properties();

String path = "./app.properties";

try (FileInputStream file = new FileInputStream(path)) {
    props.load(file);
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println(props.getProperty("property"));

并且在运行JAR时设法从文件加载属性,但这次我在IDE中运行时无法加载它们,它以与上面相同的异常结束。

所以我的问题是:如何设置文件路径(或pom.xml文件?),我将能够加载从IDE和JAR文件运行的属性?

提前致谢:)

2 个答案:

答案 0 :(得分:0)

您依赖java进程的工作目录时的工作方式。通常,这是您的命令行(也称为shell)在启动应用程序时指向的。

在大多数IDE上,您可以在启动器设置中配置此目录。 (对于eclipse,它位于第二个选项卡'Arguments')所以你需要到目标目录(对于eclipse,有一个按钮'Workspace ...')

特别是在intelliJ中,您可以在“运行/编辑配置”下找到此设置...这将打开如下窗口: enter image description here 在那里你可以编辑工作目录。你只需在最后添加目标。

编辑: 实际上你最后添加了src / main / ressources。

答案 1 :(得分:0)

在您的Java代码中,请阅读app.properties文件,如下所示:

final String ROOT_PATH = "custom.path"; // Or whatever you most like
final String PROPERTIES_FILE = "app.properties";

// start :: try-catch here to manage I/O resources
File directory = new File(System.getProperty(ROOT_PATH), "conf");
File file = new File(directory, PROPERTIES_FILE);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
// ...
// end :: try-catch here to manage I/O resources

然后你必须设置/声明custom.path

  • 将其声明为环境变量
  • 使用-Dcustom.path=/somewhere/in/your/filesystem运行时(环境属性)中设置它(我更喜欢这个,除非路径已修复和/或在不同的应用程序之间共享)

然后,最终您需要将app.properties文件复制/放入/somewhere/in/your/filesystem/conf。为什么在/conf内?因为您在声明directory字段时使用它。如果您不希望它在那里,请不要设置它并删除, "conf"部分。

此外,要在“本地”(在IDE中)运行它,请使用 VM选项设置(IntelliJ IDEA):

enter image description here

相关问题