Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Profile for testing

I am developing a java web application (No Spring).I wanted to use separate db for production and testing.I have two files in src/main/resources - env.properties and env.test.properties. I have defined the 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 test by maven test -Ptest, I see that my test is getting executed with the db from env.properties and then after completion of test , the profile switching happens. I am also having a jebkins pipeline which builds tests and deploys. Am I missing something here ? What is the correct way to read the properties from env.test.properties(activate the profile and run test) ?

Thanks a lot.

like image 723
Varbi Avatar asked Jun 21 '17 02:06

Varbi


People also ask

How do I run a test on a Maven profile?

You will create what is called a ‘Maven profile’ (which has a few settings which i will describe in a second), and you will run the following command from the command line: ‘mvn test -Pacceptance ‘.

What is the difference between test and production configuration in Maven?

test configuration when test profile is used. production configuration when prod profile is used. In the following example, we will attach maven-antrun-plugin:run goal to test the phase. This will allow us to echo text messages for different profiles.

What is a Maven build profile?

To address these circumstances, Maven 2.0 introduces the concept of a build profile. Profiles are specified using a subset of the elements available in the POM itself (plus one extra section), and are triggered in any of a variety of ways.

How to give Maven profile names for test suites in Jenkins?

You can refer below screenshots for giving Maven Profile Names for Test Suites. Note: Profile Name should always be written inside <id> tag. Once Maven Profiles are built for all testng.xml files then we are good to execute from Command Prompt or from Jenkins.


1 Answers

You're doing this the hard way.

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

Resources in src/test/resources/ will be found and loaded before those in src/main/resources when unit tests are being executed.

like image 95
Steve C Avatar answered Oct 08 '22 17:10

Steve C