gwt+ant实践笔记----宋轶聪

本文详细介绍了使用Ant构建GWT工程的步骤,包括创建war目录、配置classpath、编译Java代码为class、将class转换为JS、打包成war包以及清理过程。重点讲解了build.xml文件的各个目标任务,如`prepare`、`javac`、`gwtc`和`war`等,以及如何自定义Ant构建脚本来适应不同项目需求。

build.xml源文件

构建工程war的分析

自定义的build工程文件

 

 

 

 

 

 

 

 


构建工程war的分析

编译打包的充分必要条件:所有路径均以build文件所在目录为根的相对路径

1、有build.xml文件,名称任意

2、在build所在目录创建一个用于打包的war目录

3、在这个war目录下创建WEB-INF/lib目录

    [mkdir] Created dir: E:/work/javaenv/eclipse/workspace/Showcase/war/WEB-INF/lib

4、在这个war目录下创建/WEB-INF/classes目录

     [mkdir] Created dir: E:/work/javaenv/eclipse/workspace/Showcase/war/WEB-INF/classes

5、将所需的gwt的几个jar包复制到WEB-INF/lib目录

    [copy] Copying 1 file to E:/work/javaenv/eclipse/workspace/Showcase/war/WEB-INF/lib

6、配置编译所需的classpath

<path id="project.class.path">

    <pathelement location="war/WEB-INF/classes"/>

    <pathelement location="${gwt.sdk}/gwt-user.jar"/>

    <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>

    <fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>

  </path>

7、编译,将java编译为class并将class复制到目标路径

8、编译,将class编译为js

9、打war包

10、清除临时文件

 


自定义的build工程文件            红色字体要根据不同工程而变化

<?xml version="1.0" encoding="utf-8" ?>

<!-- initalization -->

<project name="Showcase" default="flow" basedir=".">

  <property name="gwt.args" value="" />

  <property name="gwt.sdk" location="." /><!-- 几个jar相对于build文件的相对路径-->

  <path id="project.class.path">                                 <!-- 注意这里的classpath是先定义,后面才创建   这一部分不用改-->

    <pathelement location="war/WEB-INF/classes"/>

    <pathelement location="${gwt.sdk}/gwt-user.jar"/>

    <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>

    <fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>

  </path>

<!-- _________________main flow__________________________ -->

<target name= "flow" depends="prepare,javac,gwtc,war,clean"/>

<!-- _________________body begin__________________________ -->

<!-- prepare -->

  <target name="prepare">

    <mkdir dir="war/WEB-INF/lib" />

    <mkdir dir="war/WEB-INF/classes"/>

    <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />

  </target>

<!-- compile java to class -->

  <target name="javac" depends="prepare"  >

    <javac srcdir="src" includes="**" encoding="utf-8"

        destdir="war/WEB-INF/classes"

        source="1.6" target="1.6" nowarn="true"

        debug="true" debuglevel="lines,vars,source">

      <classpath refid="project.class.path"/>

    </javac>

    <copy todir="war/WEB-INF/classes">

      <fileset dir="src" excludes="**/*.java"/>

    </copy>

  </target>

<!-- compile class to js -->

  <target name="gwtc" depends="javac" >

    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">

      <classpath>

        <pathelement location="src"/>

        <path refid="project.class.path"/>

      </classpath>

  <jvmarg value="-Xmx256M"/>

      <arg line="${gwt.args}"/>

      <arg value="com.google.gwt.sample.showcase.Showcase"/><!-- 入口程序-->

    </java>

  </target>

<!-- war -->

  <target name="war" depends="gwtc" >

    <zip destfile="Showcase.war" basedir="war"/><!--打的war包名和war的目录-->

  </target>

<!-- clean -->

  <target name="clean" >

    <delete dir="war/WEB-INF/classes" failonerror="false" />

    <delete dir="war/showcase" failonerror="false" />

  </target>

<!-- _________________body end__________________________ -->

</project>

 


build.xml源文件

 

<!-- 标准定义 -->

<?xml version="1.0" encoding="utf-8" ?>

<!-- 工程名和缺省任务 -->

<project name="Showcase" default="build" basedir=".">

  <!-- 指定gwt执行参数-->

  <property name="gwt.args" value="" />

  <!-- 指定GWT的SDK路径 -->

  <property name="gwt.sdk" location="../.." />

 

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;1、准备环境变量-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <!-- 将lib复制到WEB-INF/lib -->

  <target name="libs" description="Copy libs to WEB-INF/lib">

    <mkdir dir="war/WEB-INF/lib" />

    <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />

    <!-- Add any additional server libs that need to be copied -->

  </target>

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;2、编译java到class-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <target name="javac" depends="libs" description="Compile java source">

    <mkdir dir="war/WEB-INF/classes"/>

    <javac srcdir="src" includes="**" encoding="utf-8"

        destdir="war/WEB-INF/classes"

        source="1.5" target="1.5" nowarn="true"

        debug="true" debuglevel="lines,vars,source">

      <classpath refid="project.class.path"/>

    </javac>

    <copy todir="war/WEB-INF/classes">

      <fileset dir="src" excludes="**/*.java"/>

    </copy>

  </target>

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;3、设置classpath-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <path id="project.class.path">

    <pathelement location="war/WEB-INF/classes"/><!-- 将编译后产生的类的路径加入classpath -->>

    <pathelement location="${gwt.sdk}/gwt-user.jar"/>

    <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>

    <!-- Add any additional non-server libs (such as JUnit) -->

    <fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>

  </path>

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;4、将gwt编译成js-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

<!-- GWT编译到JavaScript||初始化gwt环境,并指定工程入口程序    -->

  <target name="gwtc" depends="javac" description="GWT compile to JavaScript">

    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">

      <classpath>

        <pathelement location="src"/>

        <path refid="project.class.path"/>

      </classpath>

      <!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->

      <jvmarg value="-Xmx256M"/>

      <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->

      <arg line="${gwt.args}"/>

      <arg value="com.google.gwt.sample.showcase.Showcase"/>

    </java>

  </target>

  

 <!-- -----------------------------下面的开发时用-------------------------------------------- -->

<!-- 运行开发模式 -->

  <target name="devmode" depends="javac" description="Run development mode">

    <java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">

      <classpath>

        <pathelement location="src"/>

        <path refid="project.class.path"/>

      </classpath>

      <jvmarg value="-Xmx256M"/>

      <arg value="-startupUrl"/>

      <arg value="Showcase.html"/>

      <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->

      <arg line="${gwt.args}"/>

      <arg value="com.google.gwt.sample.showcase.Showcase"/>

    </java>

  </target>

 <!-- -----------------------------下面的开发时用-------------------------------------------- -->

  <target name="eclipse.generate" depends="libs" description="Generate eclipse project">

    <java failonerror="true" fork="true" classname="com.google.gwt.user.tools.WebAppCreator">

      <classpath>

        <path refid="project.class.path"/>

      </classpath>

      <arg value="-XonlyEclipse"/>

      <arg value="-ignore"/>

      <arg value="com.google.gwt.sample.showcase.Showcase"/>

    </java>

  </target>

<!--

Test targets suppressed because -junit argument was not specified when running webAppCreator.

  <target name="javac.tests" depends="javac" description="Compiles test code">

    <javac srcdir="test" includes="**" encoding="utf-8"

      source="1.5" target="1.5" nowarn="true"

      debug="true" debuglevel="lines,vars,source">

      <classpath location="path_to_the_junit_jar"/>

      <classpath refid="project.class.path"/>

    </javac>

  </target>

  

  <target name="test.dev" depends="javac.tests" description="Run development mode tests">

    <mkdir dir="reports/htmlunit.dev" />

    <junit fork="yes" printsummary="yes" haltonfailure="yes">

      <jvmarg line="-Xmx256m" />

      <sysproperty key="gwt.args" value="-standardsMode -logLevel WARN" />

      <sysproperty key="java.awt.headless" value="true" />

      <classpath>

        <pathelement location="src" />

        <pathelement location="test" />

        <path refid="project.class.path" />

        <pathelement location="path_to_the_junit_jar" />

      </classpath>

      <batchtest todir="reports/htmlunit.dev" >

        <fileset dir="test" >

          <include name="**/*Test.java" />

        </fileset>

      </batchtest>

      <formatter type="plain" />

      <formatter type="xml" />

    </junit>

  </target>

  <target name="test.prod" depends="javac.tests" description="Run production mode tests">

    <mkdir dir="reports/htmlunit.prod" />

    <junit fork="yes" printsummary="yes" haltonfailure="yes">

      <jvmarg line="-Xmx256m" />

      <sysproperty key="gwt.args" value="-prod -standardsMode -logLevel WARN -standardsMode -out www-test" />

      <sysproperty key="java.awt.headless" value="true" />

      <classpath>

        <pathelement location="src" />

        <pathelement location="test" />

        <path refid="project.class.path" />

        <pathelement location="path_to_the_junit_jar" />

      </classpath>

      <batchtest todir="reports/htmlunit.prod" >

        <fileset dir="test" >

          <include name="**/*Test.java" />

        </fileset>

      </batchtest>

      <formatter type="plain" />

      <formatter type="xml" />

    </junit>

  </target>

  <target name="test" description="Run development and production mode tests">

    <antcall target="test.dev" />

    <antcall target="test.prod" />

  </target>

-->

<!-- 下面这项不推荐 -->

  <target name="hosted" depends="devmode" description="Run development mode (NOTE: the 'hosted' target is deprecated)" />

 <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;5、创建war包-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

<!-- 构建这个工程 -->

  <target name="build" depends="gwtc" description="Build this project" />

  <target name="war" depends="build" description="Create a war file">

    <zip destfile="Showcase.war" basedir="war"/>

  </target>

 <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;6、删除打包的过程目录和文件-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <target name="clean" description="Cleans this project">

    <delete dir="war/WEB-INF/classes" failonerror="false" />

    <delete dir="war/showcase" failonerror="false" />

  </target>

</project>


<?xml version="1.0" encoding="UTF-8"?>

<!-- 工程名和缺省任务 -->

<project name="Showcase" default="dist" basedir=".">

  <!-- 指定gwt执行参数-->

  <property name="gwt.args" value="" />

  <!-- 指定GWT的SDK路径 -->

  <property name="gwt.sdk" value="../.." />

  <!-- 指定pro_path的路径 -->

  <property name="pro_path" value="E:/work/javaenv/eclipse/workspace/Showcase" />

  <!-- 指定tmp_path的路径 -->

  <property name="tmp_path" value="E:/work/javaenv/eclipse/workspace/deployment" />

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;1、准备环境变量&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <target name="libs" >

    <mkdir dir="${tmp_path}/war/WEB-INF/lib" />

    <copy todir="${tmp_path}/war/WEB-INF/lib">

      <fileset dir="${pro_path}/src">

              <include name="**" />

              <include name="com/**" />

          </fileset>

     </copy>

   </target>

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;2、编译java到class-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <target name="javac" >

    <mkdir dir="${tmp_path}/war/WEB-INF/classes"/>

    <javac srcdir="src" includes="**" encoding="utf-8"

        destdir="${tmp_path}/war/WEB-INF/classes"

        source="1.6" target="1.6" nowarn="true"

        debug="true" debuglevel="lines,vars,source">

      <classpath refid="project.class.path"/>

    </javac>

    <copy todir="war/WEB-INF/classes">

      <fileset dir="src" excludes="**/*.java"/>

    </copy>

  </target>

  <!-- &emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;3、设置classpath&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <path id="project.class.path">

    <pathelement location="${tmp_path}/war/WEB-INF/classes"/><!-- 将编译后产生的类的路径加入classpath -->

    <pathelement location="${tmp_path}/war/WEB-INF/lib/gwt-user.jar"/>

    <pathelement location="${tmp_path}/war/WEB-INF/lib/gwt-dev.jar"/>

    <pathelement location="${tmp_path}/war/WEB-INF/lib/gwt-servlet.jar"/>

    

    <fileset dir="${tmp_path}/war/WEB-INF/lib" includes="**/*.jar"/>

  </path>

  <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;4、将gwt编译成js-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

  <target name="gwtc" >

    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">

      <classpath>

        <pathelement location="src"/>

        <path refid="project.class.path"/>

      </classpath>

      <jvmarg value="-Xmx256M"/>

      <arg line="${gwt.args}"/>

      <arg value="com.google.gwt.sample.showcase.Showcase"/>

    </java>

  </target>

 <!-- -&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;5、创建war包-&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

 

  <target name="war" depends="libs,javac,gwtc">

    <zip destfile="Showcase.war" basedir="war"/>

  </target>

 <!-- &emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;6、删除打包的过程目录和文件&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash;&emdash; -->

<target name="copywar" depends="war" >

<copy todir="${tmp_path}" overwrite="yes">

<fileset dir="${tmp_path}/war">

</fileset>

</copy>

</target>

 

<!-- 执行打包后,清空目录 -->

<target name="dist" depends="clean,copywar">

<antcall target="clean" />

</target>

 

<!-- 清空内容 -->

<target name="clean">

<delete dir="${tmp_path}/**" />

</target>

</project>


 

 

 

ant fileset 用法

fileset用来定义目录位置及操作适用于该目录下的那些子目录或文件

1. 拷贝单个文件到指定目录下。

例:<copy todir="${basedir}/new" file="${basedir}/old/old1.txt1">

将${basedir}/old/old.txt文件拷贝到${basedir}/new下

2. 拷贝一批文件到指定目录下,例:<copy todir="${basedir}/new">

           <fileset dir="${basedir}/old">

              <include name="old1.txt" />

              <include name="old2.txt" />

              <exclude name="old8.txt" />

          </fileset>

       </copy>

      这里fileset定义的是原文件的组成形式,<include/>子属性表示包括,<exclude/>子属性表示排除,很简单,通过他们组合实现多文件的筛选,当然我这个例子用得很傻。比如

                <include name="appgen/**"/>

                <include name="ibatis/**"/>

                <exclude name="**/*.log"/>

      拷贝appget目录和ibatis目录下除了.log文件以外的其它所有文件和子目录。

      可以把<fileset/>简写成<fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />,includes可以理解成include的复数形式,包含多个文件时用逗号隔开,excludes也一样。

3. 拷贝一个目录到指定目录下

例:<copy todir="${basedir}/new">

           <fileset dir="${basedir}/old">

             <include name="appgen" />

             <include name="appgen/" />

             <include name=appgen/**" />

             <include name="appgen/***" />

           </fileset>

       </copy>

      同样使用<fileset/>属性,name指定目录名,不过这里要分两种情况,用<include/>子属性和不用<include/>子属性.

      若使用<include/>, 又要分三种情况

          若是”r;appgen”,则只会拷贝名为appgen的空目录过去,它里面的文件和子目录则不会拷贝。

          若是”r;appgen/”,或”r;appgen/**”,则会把整个appgen目录拷贝过去,包括里面的文件和子目录。

          若是”r;appgen/*”,则只会把该目录和该目录下第一级子目录的所有东西拷贝过去,而不会拷贝第二级和第二级以下的。注:”r;appgen/*”这儿是一个*号,*号若大于两个,也跟一个*号是同样效果。比如”r;appgen/*”和”r;appgen/****”都只拷贝appgen目录下第一级子目录。

注:若appeng这个目录本身就是个空目录(就是不存在),无论怎么写,这个空目录都不会被拷贝。也就是说,copy操作不会产生创建空目录的作用,要想创建空目录,只有用mkdir。

      若不使用任何<include>属性,如

           <fileset dir="${basedir}/old">

           </fileset>

      则会拷贝${basedir}/old下的所有文件和子目录。

注:使用<exclude/>排除目录时,目录名必须写成”r;appgen/”或”r;appgen/**”形式,否则不会生效。

      以上是三种拷贝到目录的种类,注意如果计算机中没有todir指定的路径,ant将会自动创建这个路径。

4. 拷贝单个的文件:

〈copy tofile="old.txt" file="new.txt" /〉就这么简单就行了。

当然也可以写成

  <copy tofile="${basedir}/new/new.txt">

     <fileset dir="${basedir}/old" includes="old.txt" />

  </copy>

      这里includes就只能写一个文件,不能写上多个文件,因为不能将多个文件复制到一个文件中去,所以这样麻烦的写法是没有意义的。?这个地方还有待去验证一下.

      复制肯定还要涉及到同名覆盖的问题,ant在copy类的API中说明:Files are only copied if the source file is newer than the destination file,这里的newer是指文件的修改时间,即使你在修改时文件内容没有任何变化,只是导致修改时间变了,ant同样会覆盖同名文件,也就是说,ant不会检查文件内容。

      对于是复制目录的情况,由于目录没有修改时间,ant还是通过检查目录内文件的修改时间来决定是否覆盖的,若目录内某文件修改时间有变化,则会覆盖这个文件,而不是整个目录。

如果要强行覆盖,<copy/>有个overwrite属性,默认为false,改成true就行了。

Ant真是太方便了,以前都没注意到它。功能很强大,能创建数据库,配置服务器,部署发布应用&ldots;&ldots;只需要写好build.xml文件,剩下的就交给ant来”r;安装”你的WEB应用了。

以上就这些:

您也可以去apache ant项目里去看一下 fileset的用法:

FileSet Type

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/duming115/archive/2007/12/04/1915638.aspx

 

 

 

 

 

 

################################################################################################3333

delete 任务

删除文件:

<delete file="war/WEB-INF/lib/*" failonerror="false" />

删除目录:

<delete dir ="war/WEB-INF/lib/" failonerror="false" />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值