Maven profile for testing

I am developing a java application (No Spring). I wanted to use a separate db for production and testing. I have two files in src / main / resources - env.properties and env.test.properties. I have defined a profile in pom.xml as mentioned in 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> 

      

However, when I run the maven test -Ptest, I see that my test is being executed using the db from env.properties, and then after the test completes, the profile switch happens. I also have a jebkins pipeline that builds tests and deploys. Am I missing something? What is the correct way to read properties from env.test.properties (activate profile and run test)?

Many thanks.

+3


source to share


1 answer


You don't do it easily.

Get rid of the profiles and move the file from src/main/resources/environment.test.properties

tosrc/test/resources/environment.properties



Resources in src/test/resources/

will be found and loaded before those found in src/main/resources

the unit tests.

+4


source







All Articles