簡體   English   中英

Maven,Jenkins - 如何將項目構建到不同的測試環境?

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

我有一個包含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

您可以設置maven配置文件,並使用-P標志選擇哪一個處於活動狀態

為什么不使用Maven構建配置文件 ,因為您可以為每個配置文件指定<properties>以指定不同的構建主機等等

$ mvn -Pstaging

(比如說)啟用暫存配置文件。

有關更多信息,請參閱構建配置文件中的Sonatype手冊。

在這里,我使用bash腳本將Jenkins構建的工件部署到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

不要為此使用Maven構建配置文件!

它會強制您實際更改不同環境的構建。

而是進行測試,可能是您的應用程序可配置。 基本思想是從一些系統屬性中讀取配置詳細信息(例如數據庫URL)。 這反過來可以在Jenkins作業配置中輕松指定

對於更復雜的場景,您可以從系統屬性中指定用於給定目的的類,例如,如果您需要針對Dev環境的MockedImplementation和QA中的真實事物。

如果您碰巧使用Spring,請查看Spring Profiles,它可以很好地支持這種類型的東西。

如果你只需要在測試中使用它,你應該能夠在JUnit規則中封裝這種東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM