CI | java python tutorial Wed 25 April 2012 Olivier Mansion

Since I graduated, I always worked at companies dealing with more than one language to build their solution:

  • an AWT application deployed via Java Web Start, displaying some Python interpreted DSL outputs,
  • the use of GWT with a Django project,
  • and of course the integration of Jenkins in ShiningPanda's Python backend for our Hosted Continuous Integration Service dedicated to Python (and soon Java).

The target was always to implement a unified build process for the comprehensive solution, building and packaging it by issuing a single command. Most languages have their own build tools, but as soon as you have more than one language at hand, issues arise.

This article describes how we are currently dealing with both Java and Python at ShiningPanda to build our Java and Python based service.

First of all we needed to find a conductor. With Java involved, Maven seemed the smartest choice. So the challenge was to integrate some classical setuptools based Python projects with Maven.

The general idea is to map some of the phases of the Maven lifecycle with the setuptools commands:

  • clean with python setup.py clean,
  • compile with python setup.py develop (not mapped with Maven install phase to come before the test phase),
  • test with python setup.py test,
  • deploy with python setup.py register sdist upload.

The following sample project builds a pysample Python project and a jsample Java project. Sources can be found here, and the project is organized as follows:

Let's have a look on the POMs, the project descriptors telling Maven what to do:

  • First the root pom.xml: it defines the plugin versions and delegates to the subfolder POMs,
  • Then setuptools/pom.xml: this POM defines all the mappings and will be inherited in all Python projects to avoid mapping duplications,
  • Finally jsample/pom.xml: a classical Java POM defining the dependences for the jsample project,
  • And pysample/pom.xml: a POM that inherits from setuptools/pom.xml.

As you may have guessed, the core of the solutions is setuptools/pom.xml. It uses the exec-maven-plugin to start Python subprocesses calling setup.py:

 1 <project>
 2   <!-- Project definition [...] -->
 3   <build>
 4     <pluginmanagement>
 5       <plugins>
 6         <plugin>
 7           <groupid>org.codehaus.mojo</groupid>
 8           <artifactid>exec-maven-plugin</artifactid>
 9           <configuration>
10             <executable>python</executable>
11             <workingdirectory>${basedir}</workingdirectory>
12           </configuration>
13           <executions>
14             <!-- Mappings [...] -->
15           </executions>
16         </plugin>
17       </plugins>
18     </pluginmanagement>
19   </build>
20 </project>

Then we only have to define the mapping in the executions tag:

 1 <executions>
 2   <execution>
 3     <id>setuptools clean</id>
 4     <phase>clean</phase>
 5     <goals>
 6       <goal>exec</goal>
 7     </goals>
 8     <configuration>
 9       <arguments>
10         <argument>setup.py</argument>
11         <argument>clean</argument>
12       </arguments>
13     </configuration>
14   </execution>
15   <execution>
16     <id>setuptools install</id>
17     <phase>compile</phase>
18     <goals>
19       <goal>exec</goal>
20     </goals>
21     <configuration>
22       <arguments>
23         <argument>setup.py</argument>
24         <argument>develop</argument>
25       </arguments>
26     </configuration>
27  </execution>
28   <execution>
29     <id>setuptools test</id>
30     <phase>test</phase>
31     <goals>
32       <goal>exec</goal>
33     </goals>
34     <configuration>
35       <skip>${maven.test.skip}</skip>
36       <arguments>
37         <argument>setup.py</argument>
38         <argument>test</argument>
39       </arguments>
40     </configuration>
41   </execution>
42   <execution>
43     <id>setuptools deploy</id>
44     <phase>deploy</phase>
45     <goals>
46       <goal>exec</goal>
47     </goals>
48     <configuration>
49       <arguments>
50         <argument>setup.py</argument>
51         <argument>register</argument>
52         <argument>sdist</argument>
53         <argument>upload</argument>
54       </arguments>
55     </configuration>
56   </execution>
57 </executions>

Note that we're skipping the Python tests if the classical -Dmaven.test.skip=true property is set on command line.

Then pysample/pom.xml is quite straightforward (note the parent tag defining the inheritance):

 1 <project>
 2   <modelversion>4.0.0</modelversion>
 3   <parent>
 4     <groupid>com.shiningpanda</groupid>
 5     <artifactid>setuptools</artifactid>
 6     <version>0.1-SNAPSHOT</version>
 7     <relativepath>../setuptools/pom.xml</relativepath>
 8   </parent>
 9   <artifactid>pysample</artifactid>
10   <packaging>pom</packaging>
11   <build>
12     <plugins>
13       <plugin>
14         <groupid>org.codehaus.mojo</groupid>
15         <artifactid>exec-maven-plugin</artifactid>
16       </plugin>
17     </plugins>
18   </build>
19 </project>

And so is the root pom.xml:

 1 <project>
 2   <!-- Project definition [...] -->
 3   <build>
 4     <pluginmanagement>
 5       <plugins>
 6         <!-- Define exec-maven-plugin version -->
 7         <plugin>
 8           <groupid>org.codehaus.mojo</groupid>
 9           <artifactid>exec-maven-plugin</artifactid>
10           <version>1.2</version>
11         </plugin>
12       </plugins>
13     </pluginmanagement>
14   </build>
15   <!-- Call subprojects -->
16   <modules>
17     <module>setuptools</module>
18     <module>pysample</module>
19     <module>jsample</module>
20   </modules>
21 </project>

That's it. Now issue the following command in the root folder to build your full solution:

1 mvn clean install
2 # or to deploy (see our upcoming blog post)
3 mvn clean deploy

In the next articles we will deal with these topics:

  • synchronize project versions,
  • automate deploys and releases,
  • tips for continuous integration.

So stay tuned! And if you need some help on your build process, contact our service team!


Made with love: Tomotcha