Friday, November 13, 2015

Build maven-based RPM's

In an daily DevOps world it's necessary to have an easy to use mechanism for a revisionable software deployment. Especially when continuous integration comes to play, in terms of installing, upgrading and deleting software in an easy and proven way.
Why not use RPM for that? The great is, Maven can do that easily.

Prerequisites:
Eclipse (or IntelliJ or any other editor)
maven (command "mvn" has to work on command line)
git (command "git" should work on command line)
rpm build (sudo yum install rpm-build)

Building an RPM works on Linux systems like RedHat or CentOS.

Guide:
Build the project, so that the targets are available locally (skipTest if they fail on your PC, e.g. because of missing MongoDB or TomCat or ...). 

The following params are necessary to get it working properly:
directory = where the code should be placed after the RPM is rolled out
filemode = permissions for the installed code
username = UID 
groupname = GID
location = local location of the project which will be included in the RPM

Add the RPM goal to your pom.xml:

<project>
... <build>
... <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-rpm</id>
<goals><goal>rpm</goal></goals>
</execution>
</executions>
<configuration>
<license>Apache</license>
<distribution>Development</distribution>
<group>Applications/Internet</group>
<packager>ALO</packager>
<defineStatements>
<defineStatement>_unpackaged_files_terminate_build 0</defineStatement>
</defineStatements>
<mappings>
<mapping>
<directory>/var/lib/tomcat/webapps</directory>
<filemode>600</filemode>
<username>tomcat</username>
<groupname>tomcat</groupname>
<directoryIncluded>false</directoryIncluded>
<sources>
<source>
<location>target/test.war</location>
</source>
</sources>
</mapping>
</mappings>
<preinstallScriptlet>
<script>echo "Deploying test-api webapp"</script>
</preinstallScriptlet>
</configuration>
</plugin>
</plugins>...
</build>...
</project>

Build the RPM file
mvn rpm:rpm

Check the contents of the RPM file

rpm -q --filesbypkg -p target/rpm/<build-name>/RPMS/noarch/test-api-0.0.1-1.noarch.rpm