2013-03-19 73 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

相关问题