Install Tomcat 6 on Mac OS X with MacPorts

What makes Tomcat 6 failed to install?

It causes that enough memory isn't assigned for Apache Ant to build Tomcat 6.

Conditions to build Tomcat 6 when I try doing it.

% java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode

Error summary when building Tomcat 6

% sudo port -d install Tomcat6
build-prepare:
   [delete] Deleting directory /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_java_tomcat6/work/apache-tomcat-6.0.20-src/output/build/temp
    [mkdir] Created dir: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_java_tomcat6/work/apache-tomcat-6.0.20-src/output/build/temp

compile:
    [javac] Compiling 1052 source files to /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_java_tomcat6/work/apache-tomcat-6.0.20-src/output/classes
    [javac] java.lang.OutOfMemoryError: Java heap space
BUILD FAILED
/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_java_tomcat6/work/apache-tomcat-6.0.20-src/build.xml:119: Compile failed; see the compiler error output for detail

Solution to build Tomcat 6

Add seviral rules to build.xml of Ant for building Tomcat 6 like following descriptions.

Where is the build.xml
% ls -l /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_java_tomcat6/work/apache-tomcat-6.0.20-src/build.xml
-rw-r--r--   1 root  wheel  29211  4 21 22:13 build.xml
Before the modification
<javac srcdir="java" destdir="${tomcat.classes}"
    debug="${compile.debug}"
    deprecation="${compile.deprecation}"
    source="${compile.source}"
    target="${compile.target}"
    optimize="${compile.optimize}"
    excludes="**/CVS/**,**/.svn/**">
After the modification

Add 3 following rules.

  • fork="true"
  • memoryinitialsize="256m"
  • memorymaximumsize="256m"
<javac srcdir="java" destdir="${tomcat.classes}"
    debug="${compile.debug}"
    deprecation="${compile.deprecation}"
    source="${compile.source}"
    target="${compile.target}"
    optimize="${compile.optimize}"
    excludes="**/CVS/**,**/.svn/**"
    fork="true"
    memoryinitialsize="256m"
    memorymaximumsize="256m">

See at Apache Ant Manual CoreTasks: Javac Taskhttp://ant.apache.org/manual/CoreTasks/javac.html

Retry to build Tomcat 6

% sudo port install tomcat6

Show following messages if Tomcat 6 is installed successfully.

###########################################################
# A startup item has been generated that will aid in
# starting tomcat6 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.tomcat6.plist
###########################################################
--->  Installing tomcat6 @6.0.20_0
#
# Configuration files in /opt/local/share/java/tomcat6/conf have been
# installed with a .sample extension so that they won't be replaced by a
# port update. Each will be copied to its proper location during the
# first startup of tomcat by catalina.sh or tomcatctl if you don't
# do this yourself first. If this _is_ an update, you might do a diff to
# verify that you're not missing anything in the newer .sample versions
# of these files.
#
# The script /opt/local/share/java/tomcat6/bin/tomcatctl has been installed to
# facilitate start and stop of tomcat using the jsvc utility, which allows you to
# start tomcat as a true daemon process. Once tomcat has acquired its TCP socket,
# jsvc will drop its root privileges and run as user www.
#
# Under Apple Java, control over the Java version used to launch tomcat6 is
# through the shell variable JAVA_JVM_VERSION. This variable should be set
# to a java version such as 1.5, 1.6, or CurrentJDK (for the default version).
# You may set JAVA_JVM_VERSION in /opt/local/share/java/tomcat6/bin/setenv.sh.

How to start up Tomcat 6 as a daemon

% sudo launchctl load -w /Library/LaunchDaemons/org.macports.tomcat6.plis

How to start up and to stop Tomcat 6 manually

To start up.

% sudo /opt/local/share/java/tomcat6/bin/tomcatctl start
Password:
conf_setup.sh: file conf/catalina.policy is missing; copying conf/catalina.policy.sample to its place.
conf_setup.sh: file conf/catalina.properties is missing; copying conf/catalina.properties.sample to its place.
conf_setup.sh: file conf/server.xml is missing; copying conf/server.xml.sample to its place.
conf_setup.sh: file conf/tomcat-users.xml is missing; copying conf/tomcat-users.xml.sample to its place.
conf_setup.sh: file conf/web.xml is missing; copying conf/web.xml.sample to its place.
conf_setup.sh: file conf/setenv.local is missing; copying conf/setenv.local.sample to its place.
Starting Tomcat.... started. (pid 504)

To stop Tomcat 6.

% sudo /opt/local/share/java/tomcat6/bin/tomcatctl stop

Type shorter to start and to stop Tomcat 6

Make Symbolic link at /opt/local/bin like this.

% sudo ln -s /opt/local/share/java/tomcat6/bin/tomcatctl /opt/local/bin/tomcatctl

You can execute like this.

% sudo /opt/local/bin/tomcatctl start
% sudo /opt/local/bin/tomcatctl stop

And you already register and add search path to /opt/local/bin in ~/.bashrc or ~/.zshrc, you can type like this too.

% sudo tomcatctl start
% sudo tomcatctl stop

Attention if you use Mac OS X 10.5.x

You have to install Java 1.5 from your Leopard CD or download it:

  1. Download the official Java package from Apple, “Java for Mac OS X 10.5 Update 4” dated June 15, 2009. Don't try with version 5 or 6.
  2. Meanwhile, go to /System/Library/Frameworks/JavaVM.framework/Versions and delete the two aliases (symlinks) “1.5” and “1.5.0”.
  3. Open "Java for Mac OS X 10.5 Update 4” with Pacifist, and drill down into Contents > System > Library > Frameworks > JavaVM.framework > Versions
  4. Still in Pacifist, select 1.5 and 1.5.0, right-click, and chose "Install to Default Location".
  5. Uncomment this line in /opt/local/share/java/tomcat6/conf/setenv.local #export JAVA_JVM_VERSION=1.5
  6. Start tomcat
https://trac.macports.org/ticket/22787