2017-06-21 60 views
2

我正在開發一個java web應用程序(No Spring)。我想用單獨的數據庫進行生產和測試。我在src/main/resources中有兩個文件--env.properties和env.test.properties 。 我已經在https://maven.apache.org/guides/mini/guide-building-for-different-environments.html中提到過在pom.xml中定義了配置文件。Maven Profile for testing

<profiles> 
    <profile> 
    <id>test</id> 
    <build> 
     <plugins> 
     <plugin> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <executions> 
      <execution> 
       <phase>test</phase> 
       <goals> 
       <goal>run</goal> 
       </goals> 
       <configuration> 
       <tasks> 
        <delete file="${project.build.outputDirectory}/environment.properties"/> 
        <copy file="src/main/resources/environment.test.properties" 
         tofile="${project.build.outputDirectory}/environment.properties"/> 
       </tasks> 
       </configuration> 
      </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
      <skip>true</skip> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-jar-plugin</artifactId> 
      <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
       <goal>jar</goal> 
       </goals> 
       <configuration> 
       <classifier>test</classifier> 
       </configuration> 
      </execution> 
      </executions> 
     </plugin> 
     </plugins> 
    </build> 
    </profile> 

然而,當我Maven的測試-Ptest運行測試,我看到我的測試,得到來自env.properties數據庫執行,然後測試完成後,配置文件切換髮生。 我也有一個建立測試和部署的jebkins管道。 我在這裏錯過了什麼嗎?從env.test.properties讀取屬性的正確方法是什麼(激活配置文件和運行測試)?

非常感謝。

+0

我可以給的一個提示。如果開始考慮在Maven構建中使用Maven-ant-run,通常會做錯某些事情。在這一點上,請在這裏詢問SO或詢問Maven用戶列表... https://maven.apache.org/mail-lists.html – khmarbaise

回答

3

你這樣做是艱難的。

擺脫的個人資料,並src/main/resources/environment.test.properties文件移動到src/test/resources/environment.properties

資源src/test/resources/會被發現和那些在src/main/resources時正在執行單元測試之前加載。