Java – maven, Jenkins – how to build projects into different test environments?
I have a java project with JUnit test, which needs to be run on different test environments (DEV, staging, etc.) through Jenkins
How to set up the construction of the project to a different environment and how to pass the URL, user name and password to Maven?
Can I use the Maven 3 configuration file to read the environment URL, user name and password from the properties file?
Edit: I have added a profile 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 URLs, usernames, and passwords to these profiles?
At present, the test is to obtain the 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:
Changes implemented in the 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();
}
}
Solution
I won't include any attributes in POM, but I will use external attribute files in every environment, at least you don't need to touch POM when the attributes change
In your POM, specify a profile that references a property file whose properties are located at:
<profiles>
<profile>
<id>staging</id>
<properties>
<app.config>/your/path/to/app.staging.properties</app.config>
</properties>
</profile>
</profile>
You can then pass it to your surefire configuration:
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<appConfig>${app.config}</appConfig>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
From your test, you can load the contents of the properties file, for example:
final String appConfigPath = System.getProperty("appConfig");
// Load properties etc...
In fact, you can actually go further... Dump the Maven configuration file completely and specify - dappconfig = / your / path / to / APP in the Jenkins build configuration staging. properties.
