Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven, Jenkins - how to build project to different test environments?

I have a Java project containing junit tests that needs to be run on different test environments (Dev, Staging, etc.) via Jenkins.

How can I setup the building of the project to the different environments and how to pass the url, username and the password to maven?

Can I use maven 3 profiles to read the environment url, username and password from a property file?

Edit: I've added the profiles to the 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 to pass the url, username and the password to these profiles?

Currently the tests are acquiring the test environment details from a property 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:

The change 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();
        }
    }
like image 208
Atanas Kanchev Avatar asked Mar 19 '13 11:03

Atanas Kanchev


People also ask

How do I customize a Maven build for different environments?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.

Can we integrate testing and Jenkins with Maven?

Jenkins can schedule your tests to run at specific time. You can save the execution history and Test Reports. Jenkins supports Maven for building and Testing a project in continuous integration.


1 Answers

I wouldn't include any properties in the POM but would use an external property file per environment instead, at least then you wouldn't need to touch the POM when properties change.

In your POM specify a profile which references a property file with your properties in:

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

Then you can pass this into your Surefire configuration:

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

From within you tests you can then load the contents of the property file, e.g.:

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

Actually, you could actually take this one step further... dump the Maven profiles completely and just specify -DappConfig=/your/path/to/app.staging.properties in you Jenkins build configuration.

like image 174
Jonathan Avatar answered Oct 09 '22 20:10

Jonathan