Łukasz works as a Technical Architect for an international IT company and is responsible for delivering applications written in Java EE, Spring, and .NET. He has been involved in many various projects ranging from online insurance systems, voice and video solutions, mobile systems (both native and HTML5-based), medical systems, and large system integration projects. Łukasz is an expert in distributed systems, SOA, and cloud. Łukasz holds PhD in Computer Science. Łukasz is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Running Maven Applications on OpenShift PaaS

04.25.2012
| 6008 views |
  • submit to reddit
As you know I'm in the middle of completely overhauling and open-sourcing my PhD thesis system (more here: Qualitas). I didn't work at Gdańsk University of Technology any more and I had to find some kind of a server where I could setup a live demo of the system.

Two weeks ago I stumbled upon OpenShift. It's a division of Red Hat which offers you free cloud PaaS services for your applications. By default OpenShift comes with pre-defined platforms for Java EE, PHP, Python, Perl, Node.js, Ruby, and Do-It-Yourself (DIY) applications. DIY applications turned out to be extremely powerful. As a proof of concept I decided to deploy my Apache Camel route (with Apache CXF endpoint as a front-end).

There was one trick to make it all work, but thanks to the OpenShift community I got it working like a charm. See how I did it.

Running DIY applications on OpenShift


Using OpenShift is pretty straight forward. You create an account, then you create an application. Next you get OpenShift Git address where you upload your code. OpenShift automatically exposes all applications (even old school C) which listen on 8080 port. DIY application comes with a few action hooks. They all are stored in a hidden directory .openshift/action_hooks. The most important hooks are:
.openshift/action_hooks/start
.openshift/action_hooks/stop
Names speak for themselves. And in case you haven't figure it out yet, OpenShift, as a Red Hat division, uses Linux machines  :)

Running Maven 


OK, so I started from writing simple start and stop action hooks respectively:
nohup mvn -f qualitas-internal-monitor-camel/pom.xml camel:run &
and
kill `ps aux | grep camel:run | awk '{print $2}'`
When I pushed my application, I saw Maven message complaining about not being able to create .m2/repository directory. Now, to cut long story short Maven installed on your platform doesn't have write permissions to your home directory. Setting M2_REPO to a writable directory $OPENSHIF_DATA_DIR did not help either because Maven installed on your platform ignores it. The trick (thanks Ram!) was to create settings file and pass it to Maven. The final start action hook looked like this:
mkdir -p $OPENSHIFT_DATA_DIR/m2/repository
echo -e "<settings><localrepository>$OPENSHIFT_DATA_DIR/m2/repository</localrepository>\n</settings>\n" > $OPENSHIFT_DATA_DIR/settings.xml
cd $OPENSHIFT_REPO_DIR
nohup mvn -s $OPENSHIFT_DATA_DIR/settings.xml -f qualitas-internal-monitor-camel/pom.xml camel:run &

Points to note


  • $OPENSHIFT_DATA_DIR - is persisted between deployments and is a perfect place for Maven repository :)
  • Check the directory you're in. After I fixed repository stuff, Maven couldn't find my pom.xml file. That is why in the above start hook I change directory to $OPENSHIFT_REPO_DIR.
  • First my CXF endpoint was listening on localhost:8080, this endpoint of course was not exposed. On my local Linux I changed CXF endpoint to 0.0.0.0:8080 and everything was working like a charm, but when I ran my Camel route I got address already in use error. I had to explicitly use the $OPENSHIFT_INTERNAL_IP variable to define the IP address.

Summary


After I finish my 0.0.5-SNAPSHOT development I definitely will try to setup all Qualitas modules on OpenShift! Stay tuned!

cheers,
Łukasz
Published at DZone with permission of Łukasz Budnik, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Simon Massey replied on Sun, 2012/06/17 - 5:25am


There is a case issue with "<localRepository>" as it needs a capital R. I am building a DIY with maven then then running the resultant war and this is what I had to use in the ".openshift/action_hooks/build" action to get maven to buid the source code: 
mkdir -p ${OPENSHIFT_DATA_DIR}/m2/repository
echo -e "<settings><localRepository>${OPENSHIFT_DATA_DIR}m2/repository</localRepository>\n</settings>\n" > ${OPENSHIFT_DATA_DIR}/settings.xml
#
cd ${OPENSHIFT_REPO_DIR}
mvn -Dmaven.test.skip=true -s ${OPENSHIFT_DATA_DIR}settings.xml package  
The actual project is https://github.com/simbo1905/ZkToDo2/blob/master/openshift.build.and.run.txt which builds a jetty-runnable.jar which is then started on openshift with the following "start" command: 
nohup java -Djetty.host=${OPENSHIFT_INTERNAL_IP} -Djetty.port=${OPENSHIFT_INTERNAL_PORT} -DDATABASE_URL=postgres://${OPENSHIFT_DB_USERNAME}:${OPENSHIFT_DB_PASSWORD}@${OPENSHIFT_DB_HOST}/${OPENSHIFT_GEAR_NAME} -jar ${OPENSHIFT_REPO_DIR}target/jetty-runner-jmx.jar --port ${OPENSHIFT_INTERNAL_PORT} --config ${OPENSHIFT_REPO_DIR}src/etc/jetty.xml ${OPENSHIFT_REPO_DIR}/target/zktodo2.war > ${OPENSHIFT_LOG_DIR}server.log 2>&1 & 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.