Archive for December, 2013

Memo: tomcat configurations

December 7th, 2013

Here are some common used configurations of tomcat.

  • Add a new tomcat host. In server.xml, add these lines:
    		<Host name="" appBase="/var/www/domain_webapps_dir"
                    unpackWARs="true" autoDeploy="false">
                    <Context path="" docBase="/var/www/domain_webapps_dir/proect_name" reloadable="true" />
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                            prefix="xxx_access_log." suffix=".txt"
                            pattern="%h %l %u %t "%r" %s %b" />

Ant build script for java web project with tomcat on ubuntu

December 7th, 2013

Here is a sample ant build script for java web project with tomcat on ubuntu. It should be put under the root directory of the web project.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectName" default="war" basedir=".">
	<property environment="env" />
	<property name="" value="ProjectName" />
	<property name="webapp.dir" value="/data/public/JavaWebapps" />
	<property name="tomcat.lib" value="/usr/share/tomcat7/lib" />
	<property name="ant.lib" value="/usr/share/ant/lib" />
	<property name="dist.dir" value="${basedir}/war" />
	<property name="webRoot.dir" value="${basedir}/WebContent" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<!-- Init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		<fileset dir="${tomcat.lib}">
			<include name="*.jar" />
		<!-- ant lib  -->
		<fileset dir="${ant.lib}">
			<include name="*.jar" />

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	<target name="clear" description="Clear the project">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${}/${}.war" />
		<delete dir="${}/${}" />

	<target name="init" depends="clear" description="Init the directories">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />

	<target name="compile" depends="init" description="Compile source files">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			target="1.7" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
		<echo message="end compile..." />

	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 

	<target name="war" depends="compile" description="Package the project as .war file.">
		<echo message="begin packing war..." />
		<war destfile="${dist.dir}/${}.war" basedir="${webRoot.dir}" 
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
		<echo message="end war..." />

	<target name="deploy" depends="war" description="Deploy the war to tomcat">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${}.war" todir="${webapp.dir}" />
		<echo message="end deploy..." />