【问题标题】:How to load an external properties file from a maven java project如何从 Maven Java 项目加载外部属性文件
【发布时间】:2016-04-15 06:02:01
【问题描述】:

我有一个 maven java 项目,在 src/main/resources 目录中有一个属性文件。我已经打包了jar,jar中没有属性文件,所以可以部署到不同设置的不同环境,但是单元测试失败了

项目结构是

Properties Application Project
|-- pom.xml
`-- src
    |-- main
        |-- java
        |   `-- java classes
        |-- resources
        |   `-- myProps.properties
`-- target
    |--properties-app.jar
       myProps.properties

值被加载到这个文件中

public class MyProperties {

    private Properties myProperties;

    public MyProperties() {

        File file = new File("./myProps.properties");
        try {

            myProperties = new Properties();
            FileInputStream myPropertiesInputStream = new FileInputStream(file);
            myProperties.load(myPropertiesInputStream);

        } catch (IOException e) {

            e.printStackTrace();

        }        

    }

    public Properties getPropertyValue() {
        return myProperties.getProperty("value");
    }

}

单元测试如下

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class MyPropertiesTest {

    private MyProperties myProperties;

    @Before
    public void setup() {

        myProperties = new MyProperties();

    }

    @Test
    public void testMyProperties() {

        assertNotNull(myProperties.getPropertyValue());

    }

}    

我应该使用什么来从目标目录加载属性文件,以便我可以从命令行成功构建它?

【问题讨论】:

  • myProperties.load(getClass().getResourceAsStream("/myProps.properties"));

标签: java maven junit properties


【解决方案1】:

我希望你使用的是 maven jar 插件。如果是这样,请在 jar-plugin 中使用此配置

    <configuration>
      <archive>
        <manifestEntries>
          <Class-Path>.</Class-Path>
        </manifestEntries>
      </archive>
    </configuration>

然后jar将从jar文件的根目录中获取属性文件。

【讨论】:

    【解决方案2】:

    通过这两个建议解决了这个问题,并对 pom.xml 进行了一些更改。 谢谢两位。

    在pom中,将资源目录下的properties文件复制到目标目录,并从pom创建的jar文件中排除

    <?xml version="1.0"?>
    <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>properties</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>properties</name>
        <url>http://maven.apache.org</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <reuseForks>false</reuseForks>
                        <forkCount>1</forkCount>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>install</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/resources</directory>
                                        <includes>
                                            <include>**/*.properties</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <excludes>
                            <exclude>**/*.properties</exclude>
                        </excludes>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.properties.MyProperties</mainClass>
                            </manifest>
                            <manifestEntries>
                                <Class-Path>.</Class-Path>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    在 MyProperties 中,使用 getClass().getResourceAsStream() 方法加载文件

    myProperties.load(getClass().getResourceAsStream("/myProps.properties"));
    

    现在所有测试都通过了,jar 文件从命令行运行并加载属性文件。

    我已将生成的项目上传到 github:https://github.com/tetsujin1979/ExternalProperties

    【讨论】:

    • 谢谢。但我不明白lib/MainClass 部分。你能解释一下吗?
    • MainClass 是使用“java -jar myJar.jar”时默认执行的类,参见docs.oracle.com/javase/tutorial/deployment/jar/appman.html “lib/”指示 JRE 在 lib 目录中查找额外的jar 文件。它与问题无关,应该已被删除,对于造成的任何混淆,我们深表歉意
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多