2013-03-19 72 views
6

我有一個包含junit測試的Java項目,它需要通過Jenkins在不同的測試環境(Dev,Staging等)上運行。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。

在你的POM中指定它引用您的屬性的屬性文件中的配置文件:

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

然後你就可以進入你的神火配置如下:

<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... 

A實際上,你可以更進一步......將Maven配置文件徹底轉儲,並在你的Jenkins構建配置中指定-DappConfig=/your/path/to/app.staging.properties

+0

謝謝!這非常有幫助。我正在努力改變加載屬性文件的代碼。任何建議將受到歡迎:弓。 – 2013-03-19 13:32:25

+0

@AtanasKanchev查看[本教程的加載屬性](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html)瞭解一些指針。我希望有所幫助。 :-) – Jonathan 2013-03-19 14:38:31

+0

謝謝喬納森我已經實現了你的方法,它工作正常。雖然有一個缺點 - 現在單個測試不能單獨運行,因爲他們期望從maven接收appConfig,並且我得到NullPointerException – 2013-03-19 18:08:49

0

你可以設置maven配置文件,並使用-P標誌選擇哪個活動使用-P標誌

+0

好吧,我已經添加了以下配置文件的POM \t \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 2013-03-19 11:45:21

0

爲什麼不使用Maven build profiles,因爲你可以指定<properties>每個配置文件來指定不同的構建主機等只要做

$ mvn -Pstaging 

(說),使升級配置文件。

有關更多信息,請參閱Build Profiles上的Sonatype手冊。

+0

如何將配置文件傳遞給JUnit測試 - 他們需要的環境網址,用戶名和密碼 – 2013-03-19 11:47:10

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 
0

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

它迫使你實際改變你的構建不同的環境。

取而代之的是進行測試,可能是您的應用程序可配置。基本思想是從一些系統屬性中讀取配置細節(例如數據庫URL)。這又可以在Jenkins Job配置中輕鬆地指定

對於更復雜的場景,您可以指定要用於系統屬性的賦予目的的類,例如,如果您希望開發環境的MockedImplementation和QA中的真實事物。

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

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

+0

我不知道這件事。我認爲構建本身可能取決於配置文件。任何人都同意? – ecbrodie 2013-03-19 12:03:54

相關問題