用于测试的 Maven 配置文件

Maven Profile for testing

我正在开发一个 java 网络应用程序(否 Spring)。我想使用单独的数据库进行生产并且 testing.I 在 src/main/resources 中有两个文件 - env.properties 和 env.test.properties。 我已经在 pom.xml 中定义了配置文件,如 https://maven.apache.org/guides/mini/guide-building-for-different-environments.html.

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

非常感谢。

你这样做很辛苦。

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

在执行单元测试时,src/test/resources/ 中的资源将在 src/main/resources 中的资源之前被找到并加载。