avianey.blogspot.com Open in urlscan Pro
2a00:1450:400d:80a::2001  Public Scan

URL: http://avianey.blogspot.com/2012/12/maven-it-case-background-process.html
Submission: On March 01 via manual from DE — Scanned from DE

Form analysis 0 forms found in the DOM

Text Content

TONIO™ BLOG

"la Semplicità è l'ultima sofisticazione" - Leonardo da Vinci


 * Accueil
 * Linux MEMO






SAMEDI 15 DÉCEMBRE 2012


RUN BACKGROUND PROCESS FOR YOUR MAVEN INTEGRATION TESTS


The Maven Failsaif plugin makes it very simple to run integration test written
with JUnit or TestNG. This example shows how to use the Maven Failsafe Plugin to
run Integration Tests that properly starts and stop a Jetty server during both
the pre-integration-test phase and the post-integration-test phase. This
solution can be easyly derivated to start and stop any background process that
is well integrated as a Maven plugin :

<plugin>
    <groupId>${plugin.groupId}</groupId>
    <artifactId>${plugin.artifactId}</artifactId>
    <version>${plugin.version}</version>
    <configuration>
        <anything>...</anything>
    </configuration>
    <executions>
        <execution>
            <id>do-it-before</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>${plugin.start.goal}</goal>
            </goals>
            <configuration>
                <something>...</something>
            </configuration>
        </execution>
        <execution>
            <id>do-it-after</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>${plugin.stop.goal}</goal>
            </goals>
            <configuration>
                <whatever>...</whatever>
            </configuration>
        </execution>
    </executions>
</plugin>

Sometimes, there is no plugin or maven integration that you can use to start and
stop the background process(es) required by your intergation test case to run.
You can start and stop the desired process(es) manually but let see how to do
this properly with Maven.


MAVEN EXEC PLUGIN

The Maven exec plugin provides 2 goals to help execute system and Java programs.
However, the programs execution are blocking. It is thus impossible to run tests
during the intergation-test phase with running programs started during the
pre-integration-test phase. Hopefully, the Maven antrun plugin will help us...


MAVEN ANTRUN PLUGIN

The Maven antrun plugin provides the ability to run Ant tasks from within Maven.
It can do anything you can put into an ant build.xml script. In particular, it
is possible to use the Ant exec task with all its parameters. The solution to
our problem is the spawn parameter of the exec task that will run the specified
executable asynchronously in its own process :

<exec executable="command-to-run"
      dir="base/dir/for/the/command"
      spawn="true"> <!-- run it asynchronously and in background baby -->
    <arg value="arg"/>
    <arg value="arg"/>
    ...
    <arg value="arg"/>
</exec>

Okay but now, we want to start third parties programs with the Maven antrun
plugin during the pre-integration-test phase, keeping them running during the
integration-test phase and to shut them down during the post-integration-test
phase. Assuming we have plateform dependent scripts to start and stop Zookeeper,
Kafka or anything else... our pom.xml will look like this :

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>start-third-parties</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <target>
                    <exec executable="${run.command}"
                          dir="${basedir}/../scripts"
                          spawn="true">
                        <arg value="${run.command.additionnal.arg}"/>
                        <arg value="${basedir}/../scripts/${zookeeper.start.script}"/>
                    </exec>
                    <exec executable="${run.command}"
                          dir="${basedir}/../scripts"
                          spawn="true">
                        <arg value="${run.command.additionnal.arg}"/>
                        <arg value="${basedir}/../scripts/${kafka.start.script}"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-third-parties</id>
            <phase>post-integration-test</phase>
            <configuration>
                <target>
                    <exec executable="${run.command}"
                          dir="${basedir}/../scripts"
                          spawn="false">
                        <arg value="${run.command.additionnal.arg}"/>
                        <arg value="${basedir}/../scripts/${zookeeper.stop.script}"/>
                    </exec>
                    <exec executable="${run.command}"
                          dir="${basedir}/../scripts"
                          spawn="false">
                        <arg value="${run.command.additionnal.arg}"/>
                        <arg value="${basedir}/../scripts/${kafka.stop.script}"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
       </execution>
    </executions>
</plugin>

There is no need to spawn the shutdown of our third parties programs, but it's
possible too...


OKAY BUT WHAT IF I WANT TO USE BOTH LINUX AND WINDOWS ?

No problem ! Maven profiles are here to help you. You just have to define two
profiles wich <activation> relies on the os family like this :

<profile>
    <id>windows-properties</id>
    <activation>
        <os>
          <family>Windows</family>
        </os>
    </activation>
    <properties>
        <run.command>cmd</run.command>
        <run.command.additionnal.arg>/c</run.command.additionnal.arg>
        <zookeeper.start.script>start-zookeeper.bat</zookeeper.start.script>
        <zookeeper.stop.script>stop-zookeeper.bat</zookeeper.stop.script>
        <kafka.start.script>start-kafka.bat</kafka.start.script>
        <kafka.stop.script>stop-kafka.bat</kafka.stop.script>
    </properties>
</profile>
<profile>
    <id>linux-properties</id>
    <activation>
        <os>
          <family>unix</family>
        </os>
    </activation>
    <properties>
        <run.command>sh</run.command>
        <run.command.additionnal.arg></run.command.additionnal.arg>
        <zookeeper.start.script>start-zookeeper.sh</zookeeper.start.script>
        <zookeeper.stop.script>stop-zookeeper.sh</zookeeper.stop.script>
        <kafka.start.script>start-kafka.sh</kafka.start.script>
        <kafka.stop.script>stop-kafka.sh</kafka.stop.script>
    </properties>
</profile>

Et voila, the start and stop scripts properties will be set correctly depending
on the runtime OS.


OKAY BUT I DON'T WANT TO RUN WHOLE INTERGATION TESTS ALL THE TIME ?

This time again, you can use a Maven profile but without <activation>. Plugins
will be activated only if you specify the profile inside the maven command :

mvn clean install -P integration-tests

<profiles>
    <profile>
        <id>integration-tests</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.12.2</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <id>start-third-parties</id>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <target>
                                    <!-- ANT stuff -->
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-third-parties</id>
                            <phase>post-integration-test</phase>
                            <configuration>
                                <target>
                                    <!-- ANT stuff -->
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                       </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>


ABOUT THE STOP SCRIPTS

Stop scripts are sometimes not easy to write... but there is always a way to
kill a process ! The examples bellow are made for Kafka (replace kafka.Kafka by
anything that can identify the process you started) :


ON LINUX

ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}' | xargs kill -SIGTERM

You may have to pay to run it on Mac OS X indeed...


ON WINDOWS

wmic process where (commandline like "%%kafka.Kafka%%" and not name="wmic.exe") delete

You only need to double '%' if you want the command to be executed within a .bat
file.


CONCLUSION

This solution might work for a lot of third parties appliations that you might
want to run in background in order to pass integration tests on your whole
application layers :
 * run a queue like Kafka or ZeroMQ that doesn't provide Maven integration yet
 * run a database like MySQL, Cassandra (Maven plugin available btw...) or HBase
 * run an independent instance of anything : Memcached, ElasticSearch or Solar
   for example

Tell me if you use it for something else ;-)

Publié par Unknown
Envoyer par e-mailBlogThis!Partager sur TwitterPartager sur FacebookPartager sur
Pinterest
Libellés : Ant, Antrun, continuous integration, Elasticsearch, Hadoop, HBase,
intégration continue, integration test, Java, Jetty, Kafka, Maven, Maven
profile, testing, testNG, ZMQ, Zookeeper


4 COMMENTAIRES:

 1. esalagea14 juin 2013 à 16:53
    
    Excellent article. It saved me a lot of time.
    
    Merci beaucoup.
    
    RépondreSupprimer
    Réponses
    Répondre
    
    
 2. esalagea14 juin 2013 à 16:56
    
    Oh, I forgot. I'm actually not using it for starting third party
    applications. I'm using it for starting the application being built by this
    maven project itself (to start the application under test). Then, in the
    test-integration phase, I run the robot tests which will connect to the
    application using sockets.
    
    RépondreSupprimer
    Réponses
    Répondre
    
    
 3. André16 août 2013 à 15:56
    
    Thanks a lot! It helped me automating my tests!
    I had to change a little bit however, because the command under linux was
    not executed when "run.command.additionnal.arg" is empty. I just added a
    legal parameter that would not change the execution like:
    
    
    
    and it worked :)
    
    RépondreSupprimer
    Réponses
    Répondre
    
    
 4. geneqew7 janvier 2014 à 06:45
    
    Thanks a lot.. bookmarked!
    
    RépondreSupprimer
    Réponses
    Répondre
    
    

Ajouter un commentaire

Charger la suite...








Article plus récent Article plus ancien Accueil

Inscription à : Publier les commentaires (Atom)



CHALLENGE ME !





ARCHIVES

 * ►  2018 (3)
   * ►  avril (2)
   * ►  janvier (1)

 * ►  2016 (1)
   * ►  janvier (1)

 * ►  2015 (2)
   * ►  décembre (1)
   * ►  avril (1)

 * ►  2014 (2)
   * ►  juin (1)
   * ►  février (1)

 * ►  2013 (2)
   * ►  mars (1)
   * ►  février (1)

 * ▼  2012 (8)
   * ▼  décembre (1)
     * Run background process for your Maven integration ...
   * ►  novembre (1)
   * ►  septembre (1)
   * ►  avril (2)
   * ►  mars (1)
   * ►  janvier (2)

 * ►  2011 (7)
   * ►  décembre (1)
   * ►  septembre (1)
   * ►  juin (1)
   * ►  mai (1)
   * ►  mars (2)
   * ►  février (1)

 * ►  2010 (12)
   * ►  décembre (3)
   * ►  novembre (2)
   * ►  octobre (3)
   * ►  mai (1)
   * ►  mars (2)
   * ►  janvier (1)

 * ►  2009 (2)
   * ►  novembre (1)
   * ►  septembre (1)





TAGS

Android (19) Ant (2) Apache (2) Appengine (2) AWS (1) Cloud (2) continuous
integration (1) CoreOS (1) css3 (2) CXF (1) Debian (1) Devops (1) Docker (1) EC2
(1) Elasticsearch (1) Foreign key (1) GAE (1) GitHub (1) Glassfish (1) Hibernate
(1) HSQLDB (1) HTML5 (1) HTTPS (2) integration test (1) J2EE (1) Java (10)
Javascript (2) JAX-B (2) JAX-RS (4) JAX-WS (1) JBoss (1) JEE (3) Jersey (3)
Jetty (3) Jonas (2) JPA (3) JPA2 (1) JSR-181 (1) JSR-303 (1) JSR-311 (1) JUnit
(1) Kafka (1) KISS (2) Linux (3) Live Wallpaper (1) LXC (1) Mahout (1) Maven (6)
Mes applications (1) MySQL (1) openssl (2) SL4J (1) SOAP (1) Spring (1) Swing
(1) testNG (1) Theme (1) Tomcat (1) Tracking (1) Ubuntu (1) Unique constraint
(1) Vagrant (1) Webservices (2) Websphere (1) WSDL (1) WTP (1) X509 (2) XML (1)
XSD (1) ZMQ (1) Zookeeper (1)



PAGES VUES (30 DERNIERS JOURS)

05615823733243454369273581593410171114121313351421152916391736182419502039213622512341243725322656279728452967



2,189





Diese Website verwendet Cookies von Google, um Dienste anzubieten und Zugriffe
zu analysieren. Deine IP-Adresse und dein User-Agent werden zusammen mit
Messwerten zur Leistung und Sicherheit für Google freigegeben. So können
Nutzungsstatistiken generiert, Missbrauchsfälle erkannt und behoben und die
Qualität des Dienstes gewährleistet werden.Weitere InformationenOk

This website attempted to run a cryptominer in your browser. Click here for more
information.