简体   繁体   English

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

[英]Maven, Jenkins - how to build project to different test environments?

I have a Java project containing junit tests that needs to be run on different test environments (Dev, Staging, etc.) via Jenkins. 我有一个包含junit测试的Java项目,需要通过Jenkins在不同的测试环境(Dev,Staging等)上运行。

How can I setup the building of the project to the different environments and how to pass the url, username and the password to maven? 如何设置项目的构建到不同的环境以及如何将URL,用户名和密码传递给maven?

Can I use maven 3 profiles to read the environment url, username and password from a property file? 我可以使用maven 3配置文件从属性文件中读取环境URL,用户名和密码吗?

Edit: I've added the profiles to the Project POM: 编辑:我已将配置文件添加到Project 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>

How to pass the url, username and the password to these profiles? 如何将URL,用户名和密码传递给这些配置文件?

Currently the tests are acquiring the test environment details from a property file: 目前,测试是从属性文件中获取测试环境详细信息:

 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();
        }
    }

Edit 2: 编辑2:

The change implemented in the test runner class: 测试运行器类中实现的更改:

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();
        }
    }

I wouldn't include any properties in the POM but would use an external property file per environment instead, at least then you wouldn't need to touch the POM when properties change. 我不会在POM中包含任何属性,但会在每个环境中使用外部属性文件,至少在属性更改时您不需要触摸POM。

In your POM specify a profile which references a property file with your properties in: 在您的POM中,指定一个配置文件,该配置文件引用属性文件,其属性位于:

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

Then you can pass this into your Surefire configuration: 然后你可以将它传递给你的Surefire配置:

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

From within you tests you can then load the contents of the property file, eg: 在您的测试中,您可以加载属性文件的内容,例如:

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

Actually, you could actually take this one step further... dump the Maven profiles completely and just specify -DappConfig=/your/path/to/app.staging.properties in you Jenkins build configuration. 实际上,您实际上可以更进一步...完全转储Maven配置文件,并在Jenkins构建配置中指定-DappConfig=/your/path/to/app.staging.properties

您可以设置maven配置文件,并使用-P标志选择哪一个处于活动状态

Why not use the Maven build profiles , since you can specify <properties> per profile to specify different build hosts etc. Just do 为什么不使用Maven构建配置文件 ,因为您可以为每个配置文件指定<properties>以指定不同的构建主机等等

$ mvn -Pstaging

(say) to enable the staging profile. (比如说)启用暂存配置文件。

See the Sonatype manual on Build Profiles for much more info. 有关更多信息,请参阅构建配置文件中的Sonatype手册。

Here I use bash scripts to deploy the Jenkins built artifact to Glassfish. 在这里,我使用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

Do NOT use Maven Build Profiles for this! 不要为此使用Maven构建配置文件!

It forces you to actually change your build for different environments. 它会强制您实际更改不同环境的构建。

Instead make your tests, and probably your application configurable. 而是进行测试,可能是您的应用程序可配置。 The basic idea would be to read the configuration details (eg database urls) from some system properties. 基本思想是从一些系统属性中读取配置详细信息(例如数据库URL)。 Which in turn can easily be specified in Jenkins Job configurations 这反过来可以在Jenkins作业配置中轻松指定

For more complex scenarios you can specify classes to be used for a give purpose from system properties, for example if you want a MockedImplementation for Dev environment and the real thing in QA. 对于更复杂的场景,您可以从系统属性中指定用于给定目的的类,例如,如果您需要针对Dev环境的MockedImplementation和QA中的真实事物。

If you happen to use Spring, have a look at Spring Profiles, which support this kind of stuff very well. 如果您碰巧使用Spring,请查看Spring Profiles,它可以很好地支持这种类型的东西。

If you only need this in tests, you should be able to encapsulate this kind of stuff in JUnit Rules. 如果你只需要在测试中使用它,你应该能够在JUnit规则中封装这种东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM