Question

Imagine a Java project built using Maven for which I have:

  • some fast-running unit tests that:
    • developers should run before committing
    • my CI server (Hudson, FWIW) should run upon detecting a new commit, giving almost instant feedback in case of failures
  • some slow-running automated acceptance tests that:
    • developers can run if they choose, e.g. to reproduce and fix failures
    • my CI server should run after successfully running the unit tests

This seems like a typical scenario. Currently, I'm running:

  • the unit tests in the "test" phase
  • the acceptance tests in the "verify" phase

There are two CI jobs configured, both pointing to the project's VCS branch:

  1. "Commit Stage", which runs "mvn package" (compile and unit test the code, build the artifact), which if successful, triggers:
  2. "Automated Acceptance Tests", which runs "mvn verify" (set up, run, and tear down the acceptance tests)

The problem is that job 2 unit tests and builds the artifact-under-test all over again (because the verify phase automatically invokes the package phase). This is undesirable for several reasons (in decreasing importance):

  • the artifact created by job 2 might not be identical to that created by job 1 (e.g. if there has been a new commit in the meantime)
  • lengthens the feedback loop to the developer who made the commit (i.e. takes longer for them to find out they broke the build)
  • wastes resources on the CI server

So my question is, how can I configure job 2 to use the artifact created by job 1?

I realise I could just have one CI job that runs "mvn verify", which would create the artifact only once, but I want to have the separate CI jobs described above in order to implement a Farley-style deployment pipeline.


In case it helps anyone, here's the full Maven 2 POM of "project 2" in the accepted answer:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.cake</groupId>
    <artifactId>cake-acceptance</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>Cake Shop Acceptance Tests</name>
    <description>
        Runs the automated acceptance tests for the Cake Shop web application.
    </description>
    <build>
        <plugins>
            <!-- Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!-- Suppress the normal "test" phase; there's no unit tests -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!-- Cargo (starts and stops the web container) -->
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.0.5</version>
                <executions>
                    <execution>
                        <id>start-container</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-container</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- Don't wait for CTRL-C after starting the container -->
                    <wait>false</wait>

                    <container>
                        <containerId>jetty7x</containerId>
                        <type>embedded</type>
                        <timeout>20000</timeout>
                    </container>

                    <configuration>
                        <properties>
                            <cargo.servlet.port>${http.port}</cargo.servlet.port>
                        </properties>
                        <deployables>
                            <deployable>
                                <groupId>${project.groupId}</groupId>
                                <artifactId>${target.artifactId}</artifactId>
                                <type>war</type>
                                <properties>
                                    <context>${context.path}</context>
                                </properties>
                            </deployable>
                        </deployables>
                    </configuration>
                </configuration>
            </plugin>
            <!-- Failsafe (runs the acceptance tests) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                    <skipTests>false</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
            <!-- Add your tests' dependencies here, e.g. Selenium or Sahi,
                with "test" scope -->
        <dependency>
            <!-- The artifact under test -->
            <groupId>${project.groupId}</groupId>
            <artifactId>${target.artifactId}</artifactId>
            <version>${target.version}</version>
            <type>war</type>
        </dependency>
    </dependencies>
    <properties>
        <!-- The artifact under test -->
        <target.artifactId>cake</target.artifactId>
        <target.version>0.1.0-SNAPSHOT</target.version>
        <context.path>${target.artifactId}</context.path>
        <http.port>8081</http.port>
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Note that even though this "tests" project doesn't create an artifact, it has to use some kind of packaging (I used "jar" here), otherwise no tests are run in the verify phase.

Was it helpful?

Solution

Try two maven projects. The first one contains the build and unit tests. You install the artifacts in your local repository. The second job runs the second maven project which declares the artifacts of the first project as dependencies and runs the functional tests.

Not sure if the scenario I just described is possible, but I think it is.

For a quick improvement you can bypass the unit test with -Dmaven.test.skip=true. If you pass the revision number of your code in your scm to the second job, you should be able to checkout the same source code.

You can also check into the Clone Workspace SCM plugin. This might offer you some additional options.

OTHER TIPS

I know it's been a long time, but this is well-indexed and none of the answers do what was asked, but I found something that works:

mvn failsafe:integration-test

This runs the tests directly, without going through all the intermediate steps of building the project. You may want to add failsafe:verify after it.

So my question is, how can I configure job 2 to use the artifact created by job 1?

You can't.

You don't need to. The Maven Build lifecycle is setup in a way that sounds like it will meet your needs. The test lifecycle will only run the fast junits. Package build your end state without running the verification.

You only need one CI job. When the CI runs your the maven deploy/install life cycle, if the junits fail the build fails, the verification scripts will not execute.

You can define a Maven profile that will be used to execute only the integration tests. I do this a lot.

Something like this:

<profiles>
    <profile>
        <id>integration</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId><version>2.17</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId><version>2.4</version>
                    <configuration>
                        <outputDirectory>/tmp</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

You would invoke this with:

mvn verify -Pintegration

Unfortunately the WAR plugin can't be skipped, but you can send its output to somewhere out of the way (I've used /tmp). If you really want to save milliseconds, you could also make it ignore the web resources (except web.xml, it won't work without that).

You can also use the profile to skip any other plugins that you might be running, eg the assembly plugin, Cobertura, PMD, etc.

Maven profile that executes only the integration tests (as suggested here) is not sufficient. You also need to make sure that the configuration of maven-compiler-plugin has useIncrementalCompilation = false. Running the profile this way, will not automatically re-compile, e.g.:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top