2016-01-11 90 views
3

我有一个Maven Java项目,在src/main/resources目录中有一个属性文件。我已经打包的jar无属性的JAR文件,因此它可以被部署到不同的环境使用不同的设置,但单元测试失败如何从Maven Java项目加载外部属性文件

这个项目的结构

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

    } 

}  

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

+2

'myProperties.load(的getClass()的getResourceAsStream( “/ myProps.properties”));' – MadProgrammer

回答

5

我希望你使用的是maven jar插件。如果是这样使用的jar-插件

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

这里面的配置然后瓶子将采取从性质jar文件的根目录中的文件。

3

解决了这两个建议,再加上对pom的一些更改。 谢谢你们两位。

在POM,属性从目录复制到目标目录中的资源文件,并且从

<?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加载由POM创建的jar文件中排除().getResourceAsStream()方法

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

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

我上传生成的项目在这里github上:https://github.com/tetsujin1979/ExternalProperties

+0

感谢。但我不明白'lib /'和'MainClass'部分。你能解释一下吗? – FaithReaper

+0

MainClass是在使用“java -jar myJar.jar”时默认执行的类,请参阅https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html “lib /“指示JRE在lib目录中查找额外的jar文件。它与这个问题无关,应该被删除,对于造成的任何混淆抱歉 –

相关问题