Sunday, October 18, 2009

Mac OS X auto-starting Tomcat

To complete my dive into continuous integration with Hudson I wanted to have Tomcat (the Servlet Container of choice) auto-run in the background on my Mac box.

First of all, download the latest Tomcat distribution from the project's homepage. Now change the script files in its "bin" directory so that they can be executed using "chmod +x *.sh". This is all you have to do to get a working copy of Tomcat on your system. Give it a spin by executing the startup shell script and navigating to "http://localhost:8080/". You will already notice that the startup will cause an icon to appear in your dock that you might rather not want to see (first of all it's ugly and clutters the dock and secondly, it doesn't provide much functionality anyways). We'll get rid of that icon a little later.

Now to have Tomcat startup automatically on system start, you have to create an entry in "/Library/StartupItems". By default, the directory is protected and you're not allowed to copy anything here. Use the Finder to change the permissions of the directory and grant yourself rights to read and write the directory. Now create a new directory "Tomcat" and place the following two files inside.

File "Tomcat":
#!/bin/sh

. /etc/rc.common

export CATALINA_HOME=/Applications/apache-tomcat-6.0.20
export JAVA_OPTS=-Djava.awt.headless=true

StartService ()
{
ConsoleMessage "Starte Tomcat Server"
su -c $CATALINA_HOME/bin/startup.sh
}
StopService ()
{
ConsoleMessage "Stoppe Tomcat Server"
su -c $CATALINA_HOME/bin/shutdown.sh
}
RestartService ()
{
ConsoleMessage "Starte Tomcat Server erneut"
su -c $CATALINA_HOME/bin/shutdown.sh
su -c $CATALINA_HOME/bin/startup.sh
}
RunService "$1"
where you substitute "" for the user account that should be used for building your iPhone app (the name of your home directory should work). This setting is crucial as Xcode will not build your project unless your running build as the user whose key chain contains the iPhone developer certificate. If you're trying to build using a different account you will get error messages saying that a suitable provisioning profile could not be found.

File "StartupParameters.plist":


These files will allow Tomcat to automatically launch when the system starts. To make this work, give execution rights to the file "Tomcat" using "chmod +x Tomcat" in directory "/Library/StartupItems/Tomcat". Now change the ownership of the files and their folder using the command "chmod -R -v 0:0 Tomcat" in directory"/Library/StartupItems". This will give ownership of the files and the directory to root - a requirement by the service launcher.

You can now test whether things went smoothly by executing the command "sudo SystemStarter start Tomcat". If things go well you will notice that no icon appears in your dock on launch. This is due to the Java parameter "-Djava.awt.headless=true" that we specified in the script "Tomcat" above. If you would like to keep the icon, just comment the line using a "#".

If things do not go well it's most likely due to some missing permissions. Check the logs in the utility application "Console" that you can find in "/Applications/Utiltities".

This post followed the German description by Kai Surendorf and adjusted things where appropriate. The "java.awt.headless" tweak was found on Hardy Ferentschik's blog.

Now I'm off to write a small Hudson/Growl integration app since the Java-based notifier available on Hudson's site didn't seem to properly invoke Growl on my machine. Maybe the recent Growl update or Snow Leopard broke the tool.

Stay tuned!

No comments:

Post a Comment