Ant学习

这里介绍了ant入门,自己编写了build.xml并且执行编译工作。

下面是build.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<!--project元素。name属性指定工程的名称,default属性  
指定ant默认执行的target,即不指定要执行的target时,ant  
执行的target。basedir指定本工程的基准目录,这里指定为  
当前目录。-->
<project name="Main" default="compile" basedir="." >
<!--property元素,定义了两个元素,供后面以${元素名}的形式引用。-->
<property name="targetdir" value="classes" />
<property name="srcdir" value="src" />

<!--定义一个path元素,指定id属性,供后面引用-->
<path id="library">
	<!--定义一个fileset元素,用于指定需要用到的文件。dir属性指定哪个目录下的文件。-->
	<fileset dir="lib">
		<!--指定包含lib目录下的所有jar包-->
		<include name="*.jar"/>
	</fileset>
</path>


<!--定义一个target元素,name属性为必须,用于标识此target。description属性用于描述此target,无太大意义。depends属性指定执行此target,所依赖的其他target。这里指定为了运行compile这个target,需首先执行clean,copy-resources这两个target。-->
<target name="compile" description="Compiles the Task" depends="clean">
<!--javac是ant的核心任务(task),用于编译JAVA源程序。srcdir属性指定源程序所在的目录,${srcdir}表明引用上面定义的名为srcdir的property元素的值。destdir指定编译后生成的.class文件的输出目录。同样地,这里使用了${targetdir}的形式引用了上面定义的名为targetdir的property元素的值。classpathref表明引用上面定义的id为library的path元素。在这里,即可把它包含的.jar包加载到classpath中,而不需要像笔者以前那样自己手动运行setclasspath命令。-->
<javac srcdir="${srcdir}" destdir="${targetdir}" classpathref="library" includeantruntime="no"/>
</target>




<!--定义一个名为clean的target。-->
<target name="clean">
        <!--delete是ant的一个核心任务,用于删除目录或文  
        件。这里用来删除classes目录。即先把以前创建的classes目录删除,免得旧文件造成干扰-->
<delete dir="${targetdir}"/>
        <!--mkdir也是一核心任务,用于创建目录。这里用来  
        新创建classes目录-->
<mkdir dir="${targetdir}"/>
</target>




<target name="copy-resources">
        <!--copy也是ant的一个核心任务,用于复制目录或文  
        件。todir指定复制的目标目录。本示例未使用此任务-->
<copy todir="${targetdir}">
<!--fileset元素指定要复制的文件集,dir属性指定复制的源目录,exclude元素指定排除此目录下的所有java源文件,即复制除这些文件之外的所有文件-->
<fileset dir="${srcdir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>




<!--depends属性指定运行此target应先运行名为compile的target-->
<target name="run" depends="compile">
<!--java是ant的核心任务,用于执行某个java类。classname属性用于指定要运行的类,
这里要用到类的全名fork设为true表明使用另外的JVM来运行我们的JAVA类,而不是使用运行ant的那个JVM。classpathref与上面的javac里的一样-->
<java fork="true" classname="com.example.main.Main" classpathref="library">
<!--将classes目录加载到classpath中-->
<classpath path="${targetdir}"/>
</java>
</target>



</project>

了解了标签之后,就很好理解ant的build.xml了。

这是一个简单的例子。下面这个例子是安卓使用DexClassLoader来加载代码并执行的demo。其中也是用到了ant编译。

build.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright 2011 Google Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<project name="secondary_dex_sample" default="help">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
            unless="sdk.dir"
    />


<!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
<!--
    <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>

    /* This is typically used for code obfuscation.
       Compiled code location: ${out.classes.absolute.dir}
       If this is not done in place, override ${out.dex.input.absolute.dir} */
    <target name="-post-compile">
    </target>
-->

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->

    <!-- This is a modified version of the "dex-helper" macro.  It added the "input-dir" and
         "output-dex-file" required attributes.
         Configurable macro, which allows to pass as parameters input directory,
         output directory, output dex filename and external libraries to dex (optional) -->
    <macrodef name="dex-helper-mod">
        <attribute name="input-dir" />
        <attribute name="output-dex-file" />
        <element name="external-libs" optional="yes" />
        <element name="extra-parameters" optional="yes" />
        <attribute name="nolocals" default="false" />
        <sequential>
            <!-- set the secondary dx input: the project (and library) jar files
                 If a pre-dex task sets it to something else this has no effect -->
            <if>
                <condition>
                    <isreference refid="out.dex.jar.input.ref" />
                </condition>
                <else>
                    <path id="out.dex.jar.input.ref">
                        <path refid="project.all.jars.path" />
                    </path>
                </else>
            </if>

            <echo>Converting compiled files and external libraries into @{output-dex-file}...</echo>
            <dex executable="${dx}"
                    output="@{output-dex-file}"
                    nolocals="@{nolocals}"
                    verbose="${verbose}">
                <path path="@{input-dir}"/>
                <path refid="out.dex.jar.input.ref" />
                <external-libs />
            </dex>
        </sequential>
    </macrodef>

    <!-- This is a modified version of "-dex" target taken from $SDK/tools/ant/main_rules.xml -->
    <!-- Converts this project's .class files into .dex files -->
    <target name="-dex" depends="-compile, -post-compile, -obfuscate"
            unless="do.not.compile">
        <if condition="${manifest.hasCode}"> <!-- 是否包含java code -->
            <then>
                <!-- Create staging directories to store .class files to be converted to the -->
                <!-- default dex and the secondary dex. -->
                <mkdir dir="${out.classes.absolute.dir}.1"/>
                <mkdir dir="${out.classes.absolute.dir}.2"/>

                <!-- Primary dex to include everything but the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.1" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <exclude name="com/example/dex/lib/**" />
                    </fileset>
                </copy>
                <!-- Secondary dex to include the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.2" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <include name="com/example/dex/lib/**" />
                    </fileset>
                </copy>

                <!-- Compile .class files from the two stage directories to the apppropriate dex files. -->
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.1"
                    output-dex-file="${out.absolute.dir}/${dex.file.name}" />  <!-- classes.dex -->
                <mkdir dir="${out.absolute.dir}/secondary_dex_dir" />
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.2"
                    output-dex-file="${out.absolute.dir}/secondary_dex_dir/classes.dex" />
                <!-- Jar the secondary dex file so it can be consumed by the DexClassLoader. -->
                <!-- Package the output in the assets directory of the apk. -->
                <jar destfile="${asset.absolute.dir}/secondary_dex.jar"
                     basedir="${out.absolute.dir}/secondary_dex_dir" includes="classes.dex" />
            </then>
            <else>
                <echo>hasCode = false. Skipping...</echo>
            </else>
        </if>
    </target>

    <!-- version-tag: custom -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>

其中用到的一些标签可以多关注下。比如说import、if、copy、macrodef等。

DexClassLoader的使用例子在这里:http://download.csdn.net/detail/imzoer/6325741

你可能感兴趣的:(Ant学习)