我有一個包含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();
}
}
謝謝!這非常有幫助。我正在努力改變加載屬性文件的代碼。任何建議將受到歡迎:弓。 – 2013-03-19 13:32:25
@AtanasKanchev查看[本教程的加載屬性](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html)瞭解一些指針。我希望有所幫助。 :-) – Jonathan 2013-03-19 14:38:31
謝謝喬納森我已經實現了你的方法,它工作正常。雖然有一個缺點 - 現在單個測試不能單獨運行,因爲他們期望從maven接收appConfig,並且我得到NullPointerException – 2013-03-19 18:08:49