7

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

5 に答える 5

6

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プロファイルを完全にダンプし、-DappConfig=/your/path/to/app.staging.propertiesJenkinsビルド構成を指定するだけです。

于 2013-03-19T12:04:59.920 に答える
0

Mavenプロファイルを設定し、-Pフラグを使用してアクティブなプロファイルを選択できます

于 2013-03-19T11:44:45.280 に答える
0

プロファイルごとに異なるビルドホストなどを指定できるので、 Mavenビルドプロファイルを使用しないのはなぜですか。<properties>

$ mvn -Pstaging

(たとえば)ステージングプロファイルを有効にします。

詳細については、ビルドプロファイルのSonatypeマニュアルを参照してください。

于 2013-03-19T11:45:36.960 に答える
0

ここでは、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
于 2013-03-19T11:46:52.093 に答える
0

これにはMavenビルドプロファイルを使用しないでください!

さまざまな環境に合わせてビルドを実際に変更する必要があります。

代わりに、テストを作成し、おそらくアプリケーションを構成可能にします。基本的な考え方は、いくつかのシステムプロパティから構成の詳細(データベースのURLなど)を読み取ることです。これは、Jenkinsジョブ構成で簡単に指定できます

より複雑なシナリオでは、システムプロパティから特定の目的に使用するクラスを指定できます。たとえば、MockedImplementation forDev環境とQAの実物が必要な場合などです。

Springを使用している場合は、この種のものを非常によくサポートしているSpringプロファイルを参照してください。

これがテストでのみ必要な場合は、この種のものをJUnitルールにカプセル化できるはずです。

于 2013-03-19T11:56:45.243 に答える