2016-01-11 85 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文件。它與這個問題無關,應該被刪除,對於造成的任何混淆抱歉 –

相關問題