繁体   English   中英

Maven Profile用于测试

[英]Maven Profile for testing

我正在开发一个java Web应用程序(No Spring)。我想使用单独的db进行生产和测试。我在src / main / resources中有两个文件--env.properties和env.test.properties。 我已经在https://maven.apache.org/guides/mini/guide-building-for-different-environments.html中提到了pom.xml中的配置文件。

 <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 test -Ptest运行测试时,我看到我的测试是从env.properties中使用db执行的,然后在测试完成后,配置文件切换发生。 我也有一个建立测试和部署的jebkins管道。 我在这里错过了什么吗? 从env.test.properties读取属性的正确方法是什么(激活配置文件并运行测试)?

非常感谢。

你这么做很难。

删除配置文件并将文件从src/main/resources/environment.test.propertiessrc/test/resources/environment.properties

当执行单元测试时,将在src/main/resources之前找到并加载src/test/resources/ src/main/resources

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM