Skip to content

Instantly share code, notes, and snippets.

@ryenus
Last active August 20, 2019 19:50
Show Gist options
  • Save ryenus/d4aaac85655e62165cf58acbfda55b59 to your computer and use it in GitHub Desktop.
Save ryenus/d4aaac85655e62165cf58acbfda55b59 to your computer and use it in GitHub Desktop.
/usr/local/opt/tomcat/bin/catalina
#!/bin/sh
export CATALINA_HOME="$(brew --prefix tomcat)/libexec"
export CATALINA_BASE="${CATALINA_BASE:-$(brew --prefix)/var/tomcat}"
catalina_init() {
if [ ! -e "$CATALINA_BASE/conf" ]; then
mkdir -p "$CATALINA_BASE"
cp -r "$CATALINA_HOME/conf" "$CATALINA_BASE/"
fi
if [ ! -e "$CATALINA_BASE/conf/Catalina/localhost/" ]; then
mkdir -p "$CATALINA_BASE/conf/Catalina/localhost/"
fi
for app in "$CATALINA_HOME/webapps"/*/; do
app_name=$(basename "$app")
context_file="$CATALINA_BASE/conf/Catalina/localhost/${app_name/ROOT/tomcat}.xml"
if [ ! -e "$context_file" ]; then
if [ -e "$app/META-INF/context.xml" ]; then
cp "$app/META-INF/context.xml" "$context_file"
sed -i "s,<Context,<Context docBase=\"\${catalina.home}/webapps/${app_name}\"," "$context_file"
else
printf '<?xml version="1.0" encoding="UTF-8"?>\n<Context docBase="${catalina.home}/webapps/%s" />\n' \
"${app_name}" > "$context_file"
fi
fi
done
}
if [ ! -e "$CATALINA_BASE" ]; then
catalina_init
fi
"$CATALINA_HOME/bin/catalina.sh" "$@"
@ryenus
Copy link
Author

ryenus commented Aug 20, 2019

This is to leverage $CATALINA_BASE to make sure previously deployed web apps are properly migrated after brew update tomcat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment