Maven, Jenkins - how to create a project for different test environments?

I have a Java project containing junit tests that need to be run in different test environments (Dev, Staging, etc.) through Jenkins.

How to set up building a project in different environments and how to pass the url, username and password for maven?

Can I use maven 3 profiles to read the environment URL, username and password from the properties file?

Edit: I added profiles to Project POM:

<profiles> <profile> <id>Integration</id> </profile> <profile> <id>Staging</id> </profile> <profile> <id>PP1</id> </profile> <profile> <id>PP2</id> </profile> <profile> <id>PP3</id> </profile> </profiles> 

How do I pass the url, username and password to these profiles?

Currently, tests get test environment details from the properties file:

  public class BoGeneralTest extends TestCase { protected WebDriver driver; protected BoHomePage boHomePage; protected static Properties systemProps; String url = systemProps.getProperty("Url"); String username = systemProps.getProperty("Username"); String password = systemProps.getProperty("Password"); int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements")); static { systemProps = new Properties(); try { systemProps.load(new FileReader(new File("src/test/resources/environment.properties"))); } catch (Exception e) { e.printStackTrace(); } } 

Edit 2:

Change made in test runner class:

 public class BoGeneralTest extends TestCase { protected WebDriver driver; protected BoHomePage boHomePage; protected static Properties systemProps; String url = systemProps.getProperty("Url"); String username = systemProps.getProperty("Username"); String password = systemProps.getProperty("Password"); int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements")); String regUsername = RandomStringUtils.randomAlphabetic(5); final static String appConfigPath = System.getProperty("appConfig"); static { systemProps = new Properties(); try { systemProps.load(new FileReader(new File(appConfigPath))); } catch (Exception e) { e.printStackTrace(); } } 
+6
source share
5 answers

I would not add any properties to the POM, but instead would use an external properties file for the environment, at least you would not need to touch the POM when changing the properties.

In your POM, specify a profile that refers to a properties file with your properties:

 <profiles> <profile> <id>staging</id> <properties> <app.config>/your/path/to/app.staging.properties</app.config> </properties> </profile> </profile> 

Then you can pass this to your Surefire configuration:

 <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemPropertyVariables> <appConfig>${app.config}</appConfig> </systemPropertyVariables> </configuration> </plugin> </plugins> 

As part of your tests, you can load the contents of the properties file, for example:

 final String appConfigPath = System.getProperty("appConfig"); // Load properties etc... 

In fact, you could take it one more step ... completely reset Maven profiles and just specify -DappConfig=/your/path/to/app.staging.properties in your Jenkins build configuration.

+4
source

You can set up maven profiles and choose which one is active using the -P flag

0
source

Why not use Maven build profiles , as you can specify <properties> for each profile to indicate different build hosts, etc. Just do

 $ mvn -Pstaging 

(say) to include an intermediate profile.

See the Sonatype Build Profiles Guide for more information.

0
source

Here I use bash scripts to deploy the Jenkins artifact to Glassfish.

 sudo /usr/share/glassfish3/glassfish/bin/asadmin --user admin --passwordfile /root/.asadminpass undeploy PROJECT_NAME sudo /usr/share/glassfish3/glassfish/bin/asadmin --user admin --passwordfile /root/.asadminpass deploy $WORKSPACE/PROJECT_NAME/target/PROJECT_NAME.war 
0
source

DO NOT use Maven Build Profiles for this!

This forces you to actually change your build for different environments.

Instead, do your tests and maybe your application is tuned. The basic idea would be to read the configuration details (e.g. database URLs) from some of the properties of the system. This, in turn, can be easily specified in Jenkins Job configurations.

For more complex scenarios, you can specify the classes that will be used to assign the target from the system properties, for example, if you want to create a MockedImplementation for Dev environment and the real thing in QA.

If you use Spring, look at Spring Profiles that very well support this type of material.

If you need it only in tests, you should be able to encapsulate this type of material in JUnit rules.

0
source

All Articles