2017-06-21 66 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时正在执行单元测试之前加载。