Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker lifecycle #433

Merged
merged 2 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@
</dependency>
</dependencies>


<build>
<plugins>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
Expand Down Expand Up @@ -240,10 +242,36 @@
</executions>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/filtered-resources/META-INF/plexus</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/META-INF/plexus</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.5.5</version>
<configuration>
<staticMetadataDirectory>${basedir}/target/filtered-resources/META-INF/plexus</staticMetadataDirectory>
</configuration>
<executions>
<execution>
<goals>
Expand Down
57 changes: 9 additions & 48 deletions samples/cargo-jolokia-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* Starts (and optionally pull) the jolokia/tomcat-7.0 container with the data container linked to it
* Waits until Tomcat is up (i.e. until it is reachable via an HTTP request)
* Runs an integration test via maven-failsafe-plugin, using rest-assured for accessing the deployed app.
* Prints out some version information about the contaner running (in order prove that's not a fake ;-)
* Prints out some version information about the container running (in order prove that's not a fake ;-)
* Stops and removes the containers.

-->

<groupId>io.fabric8</groupId>
<artifactId>cargo-jolokia-demo</artifactId>
<version>0.14.2</version>
<version>0.14.3-SNAPSHOT</version>
<packaging>docker</packaging>

<url>http://www.jolokia.org</url>

Expand Down Expand Up @@ -52,12 +53,14 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -67,6 +70,7 @@
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${project.version}</version>
<extensions>true</extensions>
<configuration>
<images>
<image>
Expand All @@ -81,34 +85,15 @@
<wait>
<!-- The plugin waits until this URL is reachable via HTTP ... -->
<http>
<url>http://localhost:${jolokia.port}/jolokia</url>
<url>http://${docker.host.address}:${jolokia.port}/jolokia</url>
</http>
<!-- ... but at max 2 seconds -->
<time>2000</time>
<!-- ... but at max 10 seconds -->
<time>10000</time>
</wait>
</run>
</image>
</images>
</configuration>
<!-- Connect this plugin to the maven lifecycle around the integration-test phase. I.e. start the container
in pre-integration-test and stop it in post-integration-test. -->
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>build</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>


Expand Down Expand Up @@ -167,20 +152,6 @@
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</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>
<systemPropertyVariables>
<!-- Needs to be repeated here (the following two lines strangely doesn't work when the next line is omitted although)
Expand All @@ -194,16 +165,6 @@
</configuration>
</plugin>


<!-- Tell surefire to skip test, we are using the failsafe plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
55 changes: 4 additions & 51 deletions samples/data-jolokia-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

<groupId>io.fabric8</groupId>
<artifactId>docker-jolokia-demo</artifactId>
<version>0.14.2</version>
<version>0.14.3-SNAPSHOT</version>
<!-- decreased boilerplate xml required when using docker packaging -->
<packaging>docker</packaging>

<url>http://www.jolokia.org</url>

Expand Down Expand Up @@ -75,6 +77,7 @@
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${project.version}</version>
<extensions>true</extensions> <!-- enables using 'docker' packaging above -->
<configuration>
<watchInterval>500</watchInterval>
<logDate>default</logDate>
Expand Down Expand Up @@ -155,52 +158,13 @@
</image>
</images>
</configuration>
<!-- Connect this plugin to the maven lifecycle around the integration-test phase. I.e. start the container
in pre-integration-test and stop it in post-integration-test. -->
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>build</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
<execution>
<id>sources</id>
<goals>
<goal>source</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- fails-safe-plugin should be used instead of surefire so that the container gets stopped even
when the tests fail -->
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</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>
<systemPropertyVariables>
<!-- Needs to be repeated here (the following two lines strangely doesn't work when the next line is omitted although)
Expand All @@ -214,17 +178,6 @@
</configuration>
</plugin>


<!-- Tell surefire to skip test, we are using the failsafe plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>docker</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<lifecycles>
<lifecycle>
<id>default</id>
<!--
phase mappings extend from jar mappings
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Default_Lifecycle_Bindings_-_Packaging_ejb__ejb3__jar__par__rar__war
-->
<phases>
<generate-source>${project.groupId}:${project.artifactId}:${project.version}:source</generate-source>
<process-resources>org.apache.maven.plugins:maven-resources-plugin:2.7:resources</process-resources>
<compile>org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile</compile>
<process-test-resources>org.apache.maven.plugins:maven-resources-plugin:2.7:testResources</process-test-resources>
<test-compile>org.apache.maven.plugins:maven-compiler-plugin:3.5.1:testCompile</test-compile>
<test>org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test</test>
<package>org.apache.maven.plugins:maven-jar-plugin:2.6:jar,${project.groupId}:${project.artifactId}:${project.version}:build</package>
<pre-integration-test>${project.groupId}:${project.artifactId}:${project.version}:start</pre-integration-test>
<integration-test>org.apache.maven.plugins:maven-failsafe-plugin:2.19.1:integration-test</integration-test>
<!-- post-integration-test is opportunity for code coverage, such as jacoco, to collect statistics from running container -->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got your intention to leave the hook free for others to kick in. However, putting it into verify has the nasty side effect that mvn integration-test wont call this phase so the container keeps running. It also feels more natural to put it into pre and post goals. Couldn't a user still bind to the post-integration-test phase so that both goals are called ?

<post-integration-test></post-integration-test>
<verify>${project.groupId}:${project.artifactId}:${project.version}:stop,org.apache.maven.plugins:maven-failsafe-plugin:2.19.1:verify</verify>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why "stop" is not in <post-integration-test> ?

I ask because if you only run mvn integration-test (without verify) a server wont be stopped.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just checked it: any extra post-integration binding goes after the default one (strange behaviour btw, would expect for pre that custom executions goes after the default and for post before).

But if you need a customization you can still bind e.g. jacoco to the integration-test phase, which then will called after the integration test but before the post-integration-phase.

I adapted the lifecycle accordingly and also removed docker:source since this is duplicated in docker:build, too (adding to the build time)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's documented that order of plugins is those specified by packaging, followed by order declared in pom.

Maven Failsafe, usually binds with integration-test and verify. Jacoco report-integration-mojo likewise binds to verify. So, current practice is that running and actually verifying integration tests requires using verify phase.

I was guided in my choices by what other container technologies use; Cargo recommends associating start goal with pre-integration-test and stop goal with post-integration-test. Tomcat maven plugin recommends associating run-war-only goal with pre-integration-test and shutdown goal with post-integration-test.

I would prefer not to bind jacoco:dump with integration-test. This goal must be called after failsafe runs the tests and before the container is stopped. It's often difficult to ensure the ordering of goals within a phase, particularly when parent poms and profiles are involved. You already understand the problem with binding to post-integration-test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was guided in my choices by what other container technologies use; Cargo recommends associating start goal with pre-integration-test and stop goal with post-integration-test. Tomcat maven plugin recommends associating run-war-only goal with pre-integration-test and shutdown goal with post-integration-test.

That is exactly now what I suggest, too.docker:start in pre-integration-test and docker:stop in post-integration-test. My problem to moving docker:stop to verify is that you can easily call mvn integration-test which won't run verify and hence doesnt stop the container.

I know there wont be a perfect solution for the lifecycle choice, but as an alternative you could use a preStop command to get data out of the container before it stops. Or you mount the the directory where jacoco dumps its data as a volume dir within target.

wdyt, would this be ok ?

Btw, I introduced two other packagings, too: 'docker-build' for only building images and 'docker-tar' for creating a docker tar as artifact (Dockerfile + support files).

Thanks btw for the nice PR, makes things quite simpler :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even after moving docker:stop to post-integration-test, calling mvn integration-test won't run post-integration-test and therefore not stop the container.

Unfortunately, mounting the directory is a non-starter in our build chain. Let me look at preStop tonight.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even after moving docker:stop to post-integration-test, calling mvn integration-test won't run post-integration-test and therefore not stop the container.

Yes, you are right, one would have to call mvn post-integration-test (totally intuitive ;-). Just out of curiosity : How do extract the jacoco data from a running container ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use jacoco:dump to connect to port 6300.

Reviewing my personal usage of phases: mostly it's mvn clean install. For jenkins it's mvn clean deploy. If I want to build without releasing to my local respository it's mvn clean verify. I use verify because I want to look at the jacoco and failsafe reports generated from the integration tests. Occasionally while debugging an aspect of the build I might use mvn package.

I almost never use any of the really long named phases; they take too long to type and I usually mis-remember the actual name. I suspect that these phases are more about sequencing than about the expectation of use from the command line. In the Maven Introduction to the Build Lifecycle, you don't see any of the pre-*, post-*, or process-* phases mentioned until the reference section.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, you convinced me ;-). I will put docker:stop to verify phase. We can still tune this if we get more feedback later on (but I want to make a release today).

The last question is whether docker:stop should be before failsafe:verify or after ? I guess before, because failsafe:verify probably stops the build in case of an error so that docker:stop wouldnt run anymore ....

Will revert to you original suggetion.

thanks :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through this with me. I appreciate the time you've taken. I'm now working with maven team to update lifecycle documentation.

<deploy>${project.groupId}:${project.artifactId}:${project.version}:push</deploy>
</phases>
</lifecycle>
</lifecycles>
</configuration>
</component>
</components>
</component-set>