java – Maven,Jenkins – 如何将项目构建到不同的测试环境?

前端之家收集整理的这篇文章主要介绍了java – Maven,Jenkins – 如何将项目构建到不同的测试环境?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含junit测试的 Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行.

如何将项目的构建设置为不同的环境以及如何将URL,用户名和密码传递给maven?

我可以使用maven 3配置文件属性文件中读取环境URL,用户名和密码吗?

编辑:我已将配置文件添加到Project POM:

  1. <profiles>
  2. <profile>
  3. <id>Integration</id>
  4. </profile>
  5. <profile>
  6. <id>Staging</id>
  7. </profile>
  8. <profile>
  9. <id>PP1</id>
  10. </profile>
  11. <profile>
  12. <id>PP2</id>
  13. </profile>
  14. <profile>
  15. <id>PP3</id>
  16. </profile>
  17. </profiles>

如何将URL,用户名和密码传递给这些配置文件

目前,测试是从属性文件获取测试环境详细信息:

  1. public class BoGeneralTest extends TestCase {
  2.  
  3. protected WebDriver driver;
  4. protected BoHomePage boHomePage;
  5. protected static Properties systemProps;
  6. String url = systemProps.getProperty("Url");
  7. String username = systemProps.getProperty("Username");
  8. String password = systemProps.getProperty("Password");
  9. int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
  10.  
  11. static {
  12. systemProps = new Properties();
  13. try {
  14. systemProps.load(new FileReader(new File("src/test/resources/environment.properties")));
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }

编辑2:

测试运行器类中实现的更改:

  1. public class BoGeneralTest extends TestCase {
  2.  
  3. protected WebDriver driver;
  4. protected BoHomePage boHomePage;
  5. protected static Properties systemProps;
  6. String url = systemProps.getProperty("Url");
  7. String username = systemProps.getProperty("Username");
  8. String password = systemProps.getProperty("Password");
  9. int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
  10. String regUsername = RandomStringUtils.randomAlphabetic(5);
  11.  
  12. final static String appConfigPath = System.getProperty("appConfig");
  13.  
  14. static {
  15. systemProps = new Properties();
  16. try {
  17.  
  18. systemProps.load(new FileReader(new File(appConfigPath)));
  19.  
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }

解决方法

我不会在POM中包含任何属性,但会在每个环境中使用外部属性文件,至少在属性更改时您不需要触摸POM.

在您的POM中,指定一个引用属性文件配置文件,其属性位于:

  1. <profiles>
  2. <profile>
  3. <id>staging</id>
  4. <properties>
  5. <app.config>/your/path/to/app.staging.properties</app.config>
  6. </properties>
  7. </profile>
  8. </profile>

然后你可以将它传递给你的Surefire配置:

  1. <plugins>
  2. <plugin>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <configuration>
  5. <systemPropertyVariables>
  6. <appConfig>${app.config}</appConfig>
  7. </systemPropertyVariables>
  8. </configuration>
  9. </plugin>
  10. </plugins>

从您的测试中,您可以加载属性文件内容,例如:

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

实际上,您实际上可以更进一步…完全转储Maven配置文件,并在Jenkins构建配置中指定-DappConfig = / your / path / to / app.staging.properties.

猜你在找的Java相关文章