alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Ant example source code file (war-test.xml)

This example Ant source code file (war-test.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Ant tags/keywords

ee, here, here, instead, java, notestwebxmlfilesetinimplicitupdate, notestwebxmlfilesetinimplicitupdate, the, the, this, web-inf, web-inf

The war-test.xml source code

<?xml version="1.0"?>

<project name="war-test" basedir="." default="all"
       xmlns:au="antlib:org.apache.ant.antunit">
  <property name="working.dir" value="working"/>

  <target name="all">
    <au:antunit>
      <fileset file="${ant.file}"/>
      <au:plainlistener/>
    </au:antunit>
  </target>


  <target name="init">
    <delete dir="${working.dir}"/>
    <mkdir dir="${working.dir}"/>
    <property name="warfile" location="${working.dir}/test.war"/>
    <property name="web.xml" location="web.xml"/>
    <property name="webxml.generated" location="${working.dir}/WEB-INF/web.xml"/>

    <!--failing on duplicates is half our testing-->
    <presetdef name="mkwar">
      <war destfile="${warfile}" duplicate="fail"/>
    </presetdef>
    <presetdef name="expandwar">
      <unzip src="${working.dir}/test.war" dest="${working.dir}"/>
    </presetdef>
  </target>

  <target name="tearDown">
    <delete dir="${working.dir}"/>
  </target>

  <!--test that you can patch a fileset reference into a lib element-->
  <target name="testlibrefs" depends="init">
    <mkwar webxml="${web.xml}">
      <fileset id="test" dir="." includes="web.xml"/>
      <lib refid="test"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>

  <!-- 
  This checks that as of Java EE 5, the web.xml attr is optional.
  Here there is a web.xml, in the webinf fileset, rather than a fileset
  -->
  <target name="testWebXmlInWebinf" depends="init">
    <mkwar>
      <webinf dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>

  <target name="testWebXmlMissingFromUpdate" depends="init">
    <mkwar webxml="${web.xml}" />
    <!-- there is no web.xml file, but that is ok, as
      we are updating -->
    <mkwar update="true">
      <classes dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>

  <target name="testWebXmlInImplicitUpdate" depends="init">
    <mkwar webxml="${web.xml}" />
    <!-- when we are implicitly updating, the web.xml file does not get
     pulled in, but the command still succeeds.-->
    <mkwar webxml="${web.xml}" >
      <classes dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>

  <target name="NotestWebXmlFilesetInImplicitUpdate" depends="init">
    <mkwar webxml="${web.xml}" />
    <!-- when we are implicitly updating, the web.xml file does not get
     pulled in, but the command still succeeds.-->
    <mkwar >
      <webinf dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>


  <target name="testDuplicateWebXml" depends="init">
    <mkwar webxml="${web.xml}" >
      <webinf dir="." includes="web.xml"/>
      <webinf file="${web.xml}"/>
      <zipfileset file="${web.xml}" prefix="WEB-INF"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
  </target>

  <target name="testDifferentDuplicateWebXml" depends="init">
    <copy file="${web.xml}" todir="${working.dir}" />
    <mkwar webxml="${web.xml}" >
      <webinf dir="${working.dir}" includes="web.xml"/>
      <webinf file="${web.xml}"/>
      <zipfileset file="${web.xml}" prefix="WEB-INF"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${webxml.generated}" />
    <au:assertLogContains text="The duplicate entry is"/>
  </target>


  <!--
    this target does not have a web.xml file.
    Instead it pulls in
  -->
  <target name="testWebXmlOptional" depends="init">
    <mkwar needxmlfile="false">
      <classes dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${working.dir}/WEB-INF/classes/web.xml" />
    <au:assertFalse>
      <available file="${webxml.generated}" />
    </au:assertFalse>
  </target>

  <target name="testWebXmlOptionalFailure" depends="init">
    <au:expectfailure>
      <mkwar >
        <classes dir="." includes="web.xml"/>
      </mkwar>
    </au:expectfailure>
  </target>

  <target name="testWebXmlOptionalFailure2" depends="init">
    <au:expectfailure>
      <mkwar  needxmlfile="true">
        <classes dir="." includes="web.xml"/>
      </mkwar>
    </au:expectfailure>
  </target>

  <target name="testClassesElement" depends="init">
    <mkwar needxmlfile="false">
      <classes dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${working.dir}/WEB-INF/classes/web.xml" />
  </target>

  <target name="testLibElement" depends="init">
    <mkwar needxmlfile="false">
      <lib dir="." includes="web.xml"/>
    </mkwar>
    <expandwar/>
    <au:assertFileExists file="${working.dir}/WEB-INF/lib/web.xml" />
  </target>

</project>

Other Ant examples (source code examples)

Here is a short list of links related to this Ant war-test.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.