加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Maven,Jenkins – 如何将项目构建到不同的测试环境?

发布时间:2020-12-15 00:54:49 所属栏目:Java 来源:网络整理
导读:我有一个包含junit测试的 Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行. 如何将项目的构建设置为不同的环境以及如何将URL,用户名和密码传递给maven? 我可以使用maven 3配置文件从属性文件中读取环境URL,用户名和密码吗? 编辑:我已将配
我有一个包含junit测试的 Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行.

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

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

编辑:我已将配置文件添加到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>

如何将URL,用户名和密码传递给这些配置文件?

目前,测试是从属性文件中获取测试环境详细信息:

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:

测试运行器类中实现的更改:

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.

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

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

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

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

从您的测试中,您可以加载属性文件的内容,例如:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读