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

标签 java maven-3 junit4

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

如何将项目的build设置为不同的环境以及如何将 url、用户名和密码传递给 maven?

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

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

关于java - Maven,Jenkins - 如何将项目构建到不同的测试环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498486/

相关文章:

java - 代码隐藏插件的访问路径为.action?

java - 将 RCON 转换为 Java

java - 使用正则表达式匹配新行java?

java - Jtable 单元格渲染器不会更改整数值的背景

maven-2 - Maven : How to create assembly with snapshot artifacts without timestamps file name?

java - JdbcTemplate 类型中的方法 query(String, ResultSetExtractor<T>) 不适用于参数 (String, BeanPropertyRowMapper)

java - 这是 HashMap 的有效单元测试吗?

java - 子pom中不继承的额外插件

java - 通过 mvn deploy 上传到远程存储库的 pom 文件名称中可以包含分类器吗?

java - 使用 EasyMock 和 Junit 忽略方法/void 方法