AntによるWAR作成

Revised: 27th/Apr./2008; Since: 25th/Nov./2002

ソースファイル

サーブレット

package net.nextindex.helloworld;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MainServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException{

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
ソースファイル
図:ソースファイル

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<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">

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>net.nextindex.helloworld.MainServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/Welcome</url-pattern>
    </servlet-mapping>

</web-app>
web.xml
図:web.xml

build.xml

<project name="MyProject" default="war" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="war.home" value="." />
  <property name="web-app.name" value="helloworld" />
  <property name="src" location="${web-app.name}/WEB-INF/JavaSource"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>


  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/helloworld.jar" basedir="${build}"/>
  </target>
  
  <target name="war" depends="dist"
        description="generate the WAR file">
    <!-- WARファイルの生成 -->
    <war destfile="${war.home}/${web-app.name}-${DSTAMP}.war"
           webxml="${web-app.name}/WEB-INF/web.xml">
      <fileset dir="${web-app.name}" excludes="**/JavaSource/" />
      <lib dir="${dist}/lib" includes="*.jar"/>
    </war>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Antの実行

D:\java\webappl>ant -buildfile helloworld.xml
Buildfile: helloworld.xml

init:
    [mkdir] Created dir: D:\java\webappl\build

compile:
    [javac] Compiling 1 source file to D:\java\webappl\build

dist:
    [mkdir] Created dir: D:\java\webappl\dist\lib
      [jar] Building jar: D:\java\webappl\dist\lib\helloworld.jar

war:
      [war] Building war: D:\java\webappl\helloworld-20080428.war

BUILD SUCCESSFUL
Total time: 1 second
D:\java\webappl>

Tomcatへのデプロイ

WARのデプロイ
WARのデプロイ結果
図:WARのデプロイ

実行結果の確認

ブラウザから"http://localhost:8080/helloworld/Welcome"を要求してください。

HelloWorld実行結果
図:HelloWorld実行結果

${CATALINA_HOME} ("C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps")配下には、helloworld.warがコピーされ、中身が展開されています。

HelloWorldデプロイ結果
図:HelloWorldデプロイ結果


Copyright © 2002-2008 SUGAI, Manabu. All Rights Reserved.
SEO [PR] !uO z[y[WJ Cu