Maven Profile用于测试

Var*_*rbi 4 java maven-3 maven jenkins

我正在开发一个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> 
Run Code Online (Sandbox Code Playgroud)

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

非常感谢.

Ste*_*e C 5

你这么做很难.

摆脱配置文件并将文件移动src/main/resources/environment.test.propertiessrc/test/resources/environment.properties

src/test/resources/将在src/main/resources执行单元测试之前找到并加载资源.