如何从maven java项目加载外部属性文件

Jos*_*thy 3 java junit properties maven

我有一个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
Run Code Online (Sandbox Code Playgroud)

值将加载到此文件中

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

}
Run Code Online (Sandbox Code Playgroud)

单元测试如下

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());

    }

}    
Run Code Online (Sandbox Code Playgroud)

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

She*_*tyh 6

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

    <configuration>
      <archive>
        <manifestEntries>
          <Class-Path>.</Class-Path>
        </manifestEntries>
      </archive>
    </configuration>
Run Code Online (Sandbox Code Playgroud)

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


Jos*_*thy 6

用两个建议解决了这个问题,再加上对 pom.xml 的一些更改。谢谢你们俩。

在pom中,将resource目录下的properties文件复制到target目录下,从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>
Run Code Online (Sandbox Code Playgroud)

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

myProperties.load(getClass().getResourceAsStream("/myProps.properties"));
Run Code Online (Sandbox Code Playgroud)

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

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