Maven,Jenkins - 如何将项目构建到不同的测试环境? [英] Maven, Jenkins - how to build project to different test environments?

查看:222
本文介绍了Maven,Jenkins - 如何将项目构建到不同的测试环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含junit测试的Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行。

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

如何设置项目到不同环境的构建以及如何将URL,用户名和密码传递给maven?

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?

我可以使用maven 3配置文件从属性文件中读取环境URL,用户名和密码吗?

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

编辑:我已将配置文件添加到项目POM:

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

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


推荐答案

我不会在POM中包含任何属性,而是会在每个环境中使用外部属性文件,至少在属性更改时您不需要触摸POM。

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.

在您的POM中指定一个引用属性文件的配置文件,其中包含您的属性:

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>

然后你可以将它传递给你的Surefire配置:

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...

实际上,你实际上可以迈出这一步进一步...完全转储Maven配置文件,并在Jenkins构建配置中指定 -DappConfig = / your / path / to / app.staging.properties

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,Jenkins - 如何将项目构建到不同的测试环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆