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.
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 ‘.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With