Run Groovy Script Using Tomcat Step 7
You can get instructions on installing Tomcat from my prior posts.
You might want to run a groovy script with your Application Server. You can do this with Tomcat by doing the following:
In the GroovyHelloWorld example, you should create an antbuild directory, a config directory, a content directory, a build.xml file all at the root of your project. In the build.xml file, paste the following code:
build.xml
<project name="GroovyHelloWorld" default="war">
<!-- ============ Project Settings ====================================-->
<property environment="env" />
<property name="PROJECT_HOME" value="./" />
<property name="BUILD_DIR" value="${PROJECT_HOME}/antBuild" />
<property name="JAVA_BUILD_DIR" value="${BUILD_DIR}/classes" />
<property name="CONTENT_DIR" value="${PROJECT_HOME}/content" />
<property name="JAVA_SRC_DIR" value="${PROJECT_HOME}/src" />
<property name="CONFIG" value="${PROJECT_HOME}/config" />
<property name="LIB_DIR" value="${CONTENT_DIR}/WEB-INF/lib" />
<property name="HIBERNATE" value="${CONTENT_DIR}/WEB-INF" />
<path id="class.path">
<fileset dir="${LIB_DIR}">
<include name="*.jar"/>
</fileset>
</path>
<!-- =========== Clean ====================================================== -->
<target name="clean">
<delete dir="${BUILD_DIR}" />
<delete dir="${CONFIG}/WEB-INF" />
</target>
<!-- =========== Init System ================================================= -->
<target name="init" depends="clean">
<tstamp>
<format property="now" pattern="MMM d yyyy hh:mm aa" />
</tstamp>
<!-- Create build dir -->
<mkdir dir="${BUILD_DIR}" />
<mkdir dir="${JAVA_BUILD_DIR}" />
<mkdir dir="${CONFIG}/WEB-INF" />
</target>
<!-- =========== Compile Java Code =========================================== -->
<target name="compile" depends="init">
<echo>Compile</echo>
<javac debug="on" classpathref="class.path" debuglevel="lines,vars,source" optimize="on" destdir="${JAVA_BUILD_DIR}">
<src path="${JAVA_SRC_DIR}" />
</javac>
</target>
<!-- =========== Jar Process ================================================== -->
<target name="jar" depends="compile">
<!-- increase the build number -->
<buildnumber />
<echo>Create Jar</echo>
<jar jarfile="${JAVA_BUILD_DIR}/GroovyHelloWorld.jar">
<fileset dir="${JAVA_BUILD_DIR}">
<include name="com/**" />
</fileset>
</jar>
</target>
<!-- =========== War DEV ======================================================= -->
<target name="war" depends="jar">
<echo>${BUILD_ENV}</echo>
<war warfile="${BUILD_DIR}/GroovyHelloWorld.war" webxml="${CONTENT_DIR}/WEB-INF/web.xml">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<section name="Struts 1">
<attribute name="Implementation-Title" value="GroovyHelloWorld" />
<attribute name="Implementation-Build" value="${build.number}" />
<attribute name="Implementation-Build-Date" value="${now}" />
<attribute name="Implementation-Vendor" value="" />
</section>
</manifest>
<!-- include everything in content directory -->
<fileset dir="${CONTENT_DIR}" />
<lib dir="${JAVA_BUILD_DIR}">
<include name="*.jar" />
</lib>
<lib dir="${LIB_DIR}">
<include name="*.jar"/>
</lib>
<classes dir="${BUILD_DIR}/classes">
<exclude name="*.war" />
</classes>
</war>
<copy file="${BUILD_DIR}/GroovyHelloWorld.war" todir="${env.CATALINA_HOME}/webapps" />
</target>
</project>
You also want to create a WEB-INF folder in your content directory and in the WEB-INF folder you should create a web.xml file.
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>GroovyHelloWorld</description>
<servlet>
<servlet-name>Groovy</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Groovy</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
<!-- Time Out -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
Make sure you have the groovy-all-1.6-RC-3.jar in your lib folder and move the lib folder under your WEB-INF folder under your content folder. (correct all library paths to point to the correct library in your eclipse build paths).
Create a groovy file called groovytest.groovy and put it under your content directory.
Paste this code in it:
println "Hello ${request.getParameter('name')}"
You should visit this URL and see the script execute on your Tomcat server.
http://localhost:8080/GroovyHelloWorld/groovytest.groovy?name=greg
Go To Step 8

0 comments:
Post a Comment