Ant脚本编写

一)使用Eclipse生成Ant脚本

 eclipse已经集成了ant 的功能,因此可以用来生产ant 的build.xml 文件,具体操作如下:

     选中你的项目-> 右键->export-> 在打开的窗口中选 general -> ant buildfiles , 点击next , 选中你的项目, 点击finish.

     

完成上面的步骤后,可以看见项目下生成了一个build.xml文件

在 window -> show view  ,打开ant 窗口,添加刚才生成的文件,则会生成该项目的Ant builder,可以直接点击运行。

二)手动编写

这里介绍常用的标签以及编译中的问题。

1)<javac标签>
该标签用于编译一个或一组java文件,其属性如下。
(1).srcdir表示源程序的目录。
(2).destdir表示class文件的输出目录。
(3).include表示被编译的文件的模式。
(4).excludes表示被排除的文件的模式。
(5).classpath表示所使用的类路径。
(6).debug表示包含的调试信息。
(7).optimize表示是否使用优化。
(8).verbose 表示提供详细的输出信息。
(9).fileonerror表示当碰到错误就自动停止。

示例如下:

<target name="compile" depends="clean" description="compile java source code">

<mkdir dir="${classes.dir}"/>
   <javac destdir="${classes.dir}" srcdir="${sources.dir}">
    <classpath>
    <fileset dir ="${libs.dir}" includes="*.jar" />
    </classpath>
   </javac>
</target>

编译过程中的警告

  [javac] D:\build.xml:25: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
  关于这个的解释见:关于ant -javac的 includeAntRuntime 属性,这里节录如下:

这个警告是从 Ant1.8 才开始有的,历史上,Ant 总会把它自己的 runtime包含进对 javac 任务可见的 classpath中。因此,任何与Ant有关的库都会自动进入你的构建类路径,不管你是否需要它们。这对大部分人来说可能是没必要的,因此,新的Ant版本提供了这个选项。新版本不提供默认值是为了保证早期构建文件的可移植性,即在不同Ant版本下,构建文件都能保持一致的行为。 

D:\Log.java:8: 错误: 编码UTF8的不可映射字符

 添加编码方式:

<compilerarg line="-encoding gbk" />


2)<java>标签
该标签用来执行编译生成的.class文件,其属性如下。
(1).classname 表示将执行的类名。
(2).jar表示包含该类的JAR文件名。
(3).classpath表示用到的类路径。
(4).fork表示在一个新的进程中运行该类。
(5).failonerror表示当出现错误时自动停止。
(6).output 表示输出文件。
(7).append表示追加或者覆盖默认文件。

    <target name="ModifyPackage">
        <java classname="com.example.tools.ModifyPackage" failοnerrοr="true" fork="yes">
            <classpath refid="MyRenamePackage.classpath"/>
        </java>
    </target>

关于refid, 这里顺便介绍一下<PATH>, 参考文档:http://kyfxbl.iteye.com/blog/1453793

path是ant内置的一种datatype,作用是声明路径之类的东西,在官方的manual中也叫做Path-like Structures. 声明path的时候,可以用内嵌的<pathelement>元素,来指定若干个位置。<pathelement>元素常用的属性有2个,location可以声明一个路径,path可以声明多个路径,其中用;或者:来分隔 .

  1. <path id="id">  
  2.     <pathelement location="location1" />  
  3.     <pathelement location="location2" />  
  4. </path>  

或者 
  1. <path id="id">  
  2.     <pathelement path="location1;location2" />  
  3. </path>  

也可以简写为:
  1. <path id="id" location="location" />  
或者 
  1. <path id="id" path="location1;location2" />  
<path>中可以带一个<fileset>,比如说: 
  1. <path id="classpath">  
  2.     <fileset dir="${lib.dir}">  
  3.         <include name="**/*.jar" />  
  4.     </fileset>  
  5. </path>  

使用时来refid引用即可,如上面<java>标签中的使用 

3)<jar>标签
该标签用来生成一个JAR文件,其属性如下。
(1) destfile表示JAR文件名。
(2) basedir表示被归档的文件名。
(3) includes表示别归档的文件模式。
(4) exchudes表示被排除的文件模式。

<target name="jar" depends="compile">  
<jar destfile="${output}/${jar-file-name}" basedir="${bin}">
<manifest>
    <attribute name="Manifest-Version" value="${versionName}" />
<attribute name="Build-By" value="${user.name}" />
<attribute name="Main-Class" value="${main-class}" />
<attribute name="Class-Path" value="." />
</manifest>
<!-- 包含第三方jar包 -->
<zipfileset excludes="META-INF/*.SF" src="${external-libs}/org.json.jar" />
</jar>
</target>

添加manifest与Main-Class属性,则打包后的jar文件为可执行JAR文件。

使用zipfileset来使打包文件包含所依赖的第三方jar包

可以在http://www.cnblogs.com/xionghui/archive/2012/03/13/2393679.html中看更多的关于build.xml使用到的标签解释。

另外,在开发过程中用到的Condition标签,不太容易看懂,以下是原文:

一,Condition

http://ant.apache.org/manual/CoreTasks/conditions.html

Description

Sets a property if a certain condition holds true - this is a generalization of Available and Uptodate.

If the condition holds true, the property value is set to true by default; otherwise, the property is not set. You can set the value to something other than the default by specifying the value attribute.

Conditions are specified as nested elements, you must specify exactly one condition.

Parameters

AttributeDescriptionRequired
propertyThe name of the property to set.Yes
valueThe value to set the property to. Defaults to "true".No
elseThe value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Ant 1.6.3No


All conditions to test are specified as nested elements, for a complete list see here.

Examples

  <condition property="javamail.complete">
    <and>
      <available classname="javax.activation.DataHandler"/>
      <available classname="javax.mail.Transport"/>
    </and>
  </condition>

sets the property javamail.complete if both the JavaBeans Activation Framework and JavaMail are available in the classpath.

  <condition property="isMacOsButNotMacOsX">
    <and>
      <os family="mac"/>

      <not>
        <os family="unix"/>

      </not>
    </and>
  </condition>

sets the property isMacOsButNotMacOsX if the current operating system is MacOS, but not MacOS X - which Ant considers to be in the Unix family as well.

  <condition property="isSunOSonSparc">
    <os name="SunOS" arch="sparc"/>

  </condition>

sets the property isSunOSonSparc if the current operating system is SunOS and if it is running on a sparc architecture.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值