简体   繁体   中英

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();
        }
    }

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, eg:

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.

您可以设置maven配置文件,并使用-P标志选择哪一个处于活动状态

Why not use the Maven build profiles , since you can specify <properties> per profile to specify different build hosts etc. Just do

$ mvn -Pstaging

(say) to enable the staging profile.

See the Sonatype manual on Build Profiles for much more info.

Here I use bash scripts to deploy the Jenkins built 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

Do NOT use Maven Build Profiles for this!

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

Instead make your tests, and probably your application configurable. The basic idea would be to read the configuration details (eg database urls) from some system properties. Which in turn can easily be specified in Jenkins Job configurations

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

If you happen to use Spring, have a look at Spring Profiles, which support this kind of stuff very well.

If you only need this in tests, you should be able to encapsulate this kind of stuff in JUnit Rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM