收集天下好文,各位朋友尽情发言。

Ivy与Ant的基本介绍(3)

上一篇 / 下一篇  2010-11-18 12:37:02 / 个人分类:转帖

Ivy与Ant的基本介绍(3)
2009-02-02 17:21
4. 使用Ant的war任务打包J2EE Web项目
建立一个J2EE Web工程,其目录结构如下图所示:
其中src 为源代码目录,WebRoot为各jsp存放目录,lib为工程的包目录。在antwebproject工程目录下建立了build.xml文件,该文件 为该工程的Ant构件文件。读者可以src目录下放入在前续例子中开发的HelloWorld.java文件,并在WebRoot下建立 index.jsp文件,其内容很简单,就是输出Hello信息,代码如下所示:
<%@ page language="java" contentType="text/html; charset="UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    
<head>
       
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       
<title>ant打包测试</title>
    
</head>
    
<body>
        Hello,Ant
    
</body>
</html>


接下来编写build.xml文件,其内容如下:
<?xml version="1.0"?>
<project name="antwebproject"   default="war" basedir=".">
<property name="classes" value="build/classes"/>
    
<property name="build" value="build"/>
    
<property name="lib" value="WebRoot/WEB-INF/lib"/>
    
<!-- 删除build路径-->
    
<target name="clean">
       
<delete dir="build"/>
    
</target>

    
<!-- 建立build/classes路径,并编译class文件到build/classes路径下-->
    
<target name="compile" depends="clean">
       
<mkdir dir="${classes}"/>

       
<javac srcdir="src" destdir="${classes}"/>
    
</target>

    
<!-- war-->
    
<target name="war" depends="compile">
<war destfile="${build}/antwebproject.war" webxml="WebRoot/WEB-INF/web.xml">
           
<!-- 拷贝WebRoot下除了WEB-INFMETA-INF的两个文件夹-->
    
<fileset dir="WebRoot" includes="**/*.jsp"/>

           
<!-- 拷贝lib目录下的jar-->
           
<lib dir="${lib}"/>
           
<!-- 拷贝build/classes下的class文件-->
           
<classes dir="${classes}"/>
       
</war>
    
</target>
</project>

各target的作用在内容中已经进行说明,在此不再赘述。运行该build文件,更新目录后,可看到在build目录下生成了antwebproject.war文件,如图:

解开后可看到其目录结构如下:
--META-INF
     --MANIFEST.MF
--index.jsp
--WEB-INF
     --lib
            --log4j-1.2.9.jar
     --classes
            --HelloWorld.class
     --web.xml
     读者可以将该war包拷贝到Tomcat的目录下看一下运行结果。

五.总结
在 本文中,笔者由浅至深详细描述了Ant的安装与配置、关键元素和常用任务。并通过实例讲述了Ant在我们Java项目中的应用,讲述了编译、运行java 程序,以及打jar包、war包等知识,引领读者进入Ant的奇妙世界。在本文中可以看到,Ant在自动构建和部署Java程序方面方便易用,而且非常灵 活,不失为我们Java开发者的绝佳帮手。

原文地址:http://tech.it168.com/j/2007-11-09/200711091344781.shtml

--------------------------- 其它Ant知识摘录(下面的文字没有亲自测试过) ----------------------------------
ant中执行Junit测试
首先要确保正确的ant和junit测试环境。
下面看个简单的例子:
<target name="compile">
            <mkdir dir="${basedir}/build/tool/classes" />
            <javac destdir="${basedir}/build/tool/classes" includes="**/*.java" debug="${compile.debug}"   encoding="UTF-8">
                <src path="${basedir}/tool/performancetest/src" />
                <classpath refid="test.classpath" />
            </javac>
            <copy todir="${basedir}/build/tool/classes" verwrite="yes">
                <fileset dir="${basedir}/tool/performancetest/src">
                    <include name="**/*.xml" />
                </fileset>
            </copy>
</target>

    <target name="test" depends="compile">
        <junit printsummary="on" fork="yes" >
            <formatter type="brief" usefile="false"/>
            <test name="com.zozoc.AllPerformanceTests"/>
            <classpath>
                <pathelement location="${basedir}/build/tool/classes"/>
                <path refid="test.classpath" />
            </classpath>
        </junit>
    </target>

用Ant完成生成javadoc的任务
用ant生成javadoc相当简单,命令如下:
<target name="create_doc">
<!-- destdir是javadoc生成的目录位置 -->
<javadoc destdir="${distDir}" encoding="UTF-8" docencoding="UTF-8">
   <!-- dir是你的代码位置,记住是java文件的位置而不是class文件的位置哦,第一次用这个命令容易忽略这点 -->
   <packageset dir="${srcDir}">
    <!-- exclude是不想生成哪些类的javadoc -->
    <exclude name="${excludeClasses}" />
   </packageset>
</javadoc>
</target>

ant条件判断
[ 2008/06/17 15:48 | by followme ]
大 | 中 | 小 
前段时间写Ant的时候用到了条件判读condition,找了一堆资料,现总结如下:
1、istrue isfalse:断言真假
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <istrue value="true"/>                   
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

2、逻辑运算
2.1、not 逻辑非 
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <not>
                <istrue value="true"/>                   
            </not>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>
2.2、and 逻辑与
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <and>
                <istrue value="true"/>
                <istrue value="false"/>                   
            </and>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>
2.3、or 逻辑或 xor异或 (语法上与and类似)

3、available 是否可用
<project name="testCondition">
    <path id="all.test.classes">        
         <pathelement location="bin"/>
     </path>
    <target name="test">
        <condition property="scondition">
            <!--在指定的classpath路径下是否存在资源 TestTest.class-->
            <available resource="TestTest.class">
                <classpath refid="all.test.classes" />       
            </available>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

4、isset 指定属性是否存在
<project name="testCondition">
    <!--属性也可以通过ant参数-D来设置-->
    <property name="name" value="this is name"/>   
    <target name="test">
        <condition property="scondition">
            <!--如果属性name不存在则返回false-->
            <isset property="name"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

5、equals 是否相等
<project name="testCondition">
    <!--属性也可以通过ant参数-D来设置-->
    <property name="name" value="this is name"/>   
    <target name="test">
        <condition property="scondition">
            <!--如果arg1的值与arg2的值相等返回true,否则为false-->
            <equals arg1="${name}" arg2="this is name"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>   

6、filesmatch 比较文件
<project name="testCondition">       
    <target name="test">
        <condition property="scondition">
            <!--如果file1所代表的文件与file2所代表的文件相等返回true,否则为false-->
            <filesmatch file1="testfile1.txt" file2="testfile2.txt"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

TAG:

 

评分:0

我来说两句

日历

« 2024-05-15  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 146415
  • 日志数: 25
  • 书签数: 5
  • 建立时间: 2007-01-08
  • 更新时间: 2011-04-22

RSS订阅

Open Toolbar