2013-03-19 10 views
6

私は、Jenkins経由でさまざまなテスト環境(Dev、Stagingなど)で実行する必要があるjunitテストを含むJavaプロジェクトを持っています。Maven、Jenkins - プロジェクトをさまざまなテスト環境に構築する方法は?

さまざまな環境にプロジェクトのビルドをセットアップし、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(); 
     } 
    } 

答えて

4

POMにはプロパティは含まれませんが、環境ごとに外部プロパティファイルが使用されます。少なくとも、プロパティが変更されるとPOMに触れる必要はありません。あなたはあなたができるテスト内から

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

:次に、あなたのシュアな構成にこれを渡すことができ

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

:あなたのPOMで

であなたの特性を持つプロパティファイルを参照するプロファイルを指定プロパティファイルの内容をロードします。たとえば、

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

A実際には、このステップをさらに進めることができます... Mavenプロファイルを完全にダンプし、-DappConfig=/your/path/to/app.staging.propertiesをJenkinsのビルド構成に指定するだけです。

+0

ありがとうございます!それはとても役に立ちます。私は今、プロパティファイルの読み込みのコードを変更するためにstrugglinです。どんな提案も歓迎されます。 –

+0

@AtanasKanchevいくつかのポインタについては、[プロパティのロードに関するこのチュートリアル](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html)を参照してください。私はそれが助けて欲しい:-) – Jonathan

+0

ありがとうジョナサン私はあなたのアプローチを実装しており、うまくいきます。しかし、個々のテストは別々に実行することはできません。なぜなら、mavenからappConfigを受け取る予定であり、NullPointerExceptionを受け取っているからです。 –

0

あなたはすることができますMavenプロファイルを設定し、-Pフラグを使ってどちらがアクティブであるかを選択します。

+0

[OK]を、私はPOM \t \t \t \t \t に統合 \tステージング \t \t \t \t \t \t \t を次のプロファイルを追加しました \t \t \t \t \t PP1 \t \t \t \t \t \t \t PP2 \t \t \t \t \t \t \t PP3 \t \t \t

0

あなたはなど別のビルドホストを指定するには、プロファイルごとに<properties>を指定することができますので、なぜ、Maven build profilesを使用するだけでなく、ステージングプロファイルを有効にするには、(例えば)

$ mvn -Pstaging 

を行います。

詳細については、Build ProfilesのSonatypeマニュアルを参照してください。

+0

どのようにJUnitテストにプロファイルを渡すために - 彼らは私がこれについてはよく分からない環境のURL、ユーザ名とパスワード –

0

ここでは、Jenkinsが構築したアーティファクトをGlassfishに配置するためにbashスクリプトを使用します。

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 
0

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

実際にビルド環境を変更する必要があります。

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

複雑なシナリオでは、システム環境の目的に使用するクラスを指定できます。たとえば、Dev環境のMockedImplementationとQAの本当のことが必要な場合。

Springを使用している場合は、この種のものを非常にうまくサポートするSpring Profilesを見てください。

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

+0

が必要です。ビルド自体は設定ファイルに依存すると思う。誰か同意? – ecbrodie

関連する問題