服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

eclipse快速上手指南(3)

5. 在eclipse中使用ant

ant是java平台下非常棒的批处理命令执行程序,能非常方便地自动完成编译,测试,打包,部署等等一系列任务,大大提高开发效率。如果你现在还没有开始使用ant,那就要赶快开始学习使用,使自己的开发水平上一个新台阶。

eclipse中已经集成了ant,我们可以直接在eclipse中运行ant。

以前面建立的hello工程为例,创建以下目录结构:

eclipse快速上手指南(3)(图一)
 
新建一个build.xml,放在工程根目录下。build.xml定义了ant要执行的批处理命令。虽然ant也可以使用其它文件名,但是遵循标准能更使开发更规范,同时易于与别人交流。

通常,src存放java源文件,classes存放编译后的class文件,lib存放编译和运行用到的所有jar文件,web存放jsp等web文件,dist存放打包后的jar文件,doc存放api文档。

然后在根目录下创建build.xml文件,输入以下内容:

<?xml version="1.0"?>
<project name="hello world" default="doc">

 <!-- properies -->
    <property name="src.dir" value="src" />
    <property name="report.dir" value="report" />
    <property name="classes.dir" value="classes" />
    <property name="lib.dir" value="lib" />
    <property name="dist.dir" value="dist" />
 <property name="doc.dir" value="doc"/>

    <!-- 定义classpath -->
    <path id="master-classpath">
        <fileset file="${lib.dir}/*.jar" />
        <pathelement path="${classes.dir}"/>
    </path>

    <!-- 初始化任务 -->
    <target name="init">
    </target>

    <!-- 编译 -->
    <target name="compile" depends="init" description="compile the source files">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <!-- 测试 -->
    <target name="test" depends="compile" description="run junit test">
        <mkdir dir="${report.dir}"/>
        <junit printsummary="on"
                haltonfailure="false"
                failureproperty="tests.failed"
                showoutput="true">
            <classpath refid="master-classpath" />
            <formatter type="plain"/>
            <batchtest todir="${report.dir}">
                <fileset dir="${classes.dir}">
                    <include name="**/*test.*"/>
                </fileset>
            </batchtest>
        </junit>
        <fail if="tests.failed">
        ***********************************************************
        ****  one or more tests failed!  check the output ...  ****
        ***********************************************************
        </fail>
    </target>

    <!-- 打包成jar -->
    <target name="pack" depends="test" description="make .jar file">
     <mkdir dir="${dist.dir}" />
        <jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
            <exclude name="**/*test.*" />
            <exclude name="**/test*.*" />
        </jar>
    </target>

    <!-- 输出api文档 -->
    <target name="doc" depends="pack" description="create api doc">
     <mkdir dir="${doc.dir}" />
     <javadoc destdir="${doc.dir}"
            author="true"
            version="true"
            use="true"
            windowtitle="test api">
            <packageset dir="${src.dir}" defaultexcludes="yes">
                <include name="example/**" />
            </packageset>
            <doctitle><![cdata[<h1>hello, test</h1>]]></doctitle>
            <bottom><![cdata[<i>all rights reserved.</i>]]></bottom>
            <tag name="todo" scope="all" description="to do:" />
        </javadoc>
    </target>
</project>

以上xml依次定义了init(初始化),compile(编译),test(测试),doc(生成文档),pack(打包)任务,可以作为模板。

选中hello工程,然后选择“project”,“properties”,“builders”,“new…”,选择“ant build”:

eclipse快速上手指南(3)(图二)

点击查看大图

填入name:ant_builder;buildfile:build.xml;base directory:${workspace_loc:/hello}(按“browse workspace”选择工程根目录),由于用到了junit.jar包,搜索eclipse目录,找到junit.jar,把它复制到hello/lib目录下,并添加到ant的classpath中:

eclipse快速上手指南(3)(图三)

点击查看大图

然后在builder面板中钩上ant_build,去掉java builder:
 eclipse快速上手指南(3)(图四)

再次编译,即可在控制台看到ant的输出:

buildfile: f:/eclipse-projects/hello/build.xml

init:

compile:
       [mkdir] created dir: f:/eclipse-projects/hello/classes
       [javac] compiling 2 source files to f:/eclipse-projects/hello/classes

test:
       [mkdir] created dir: f:/eclipse-projects/hello/report
       [junit] running example.hellotest
       [junit] tests run: 1, failures: 0, errors: 0, time elapsed: 0.02 sec

pack:
       [mkdir] created dir: f:/eclipse-projects/hello/dist
         [jar] building jar: f:/eclipse-projects/hello/dist/hello.jar

doc:
       [mkdir] created dir: f:/eclipse-projects/hello/doc
     [javadoc] generating javadoc
     [javadoc] javadoc execution
     [javadoc] loading source files for package example...
     [javadoc] constructing javadoc information...
     [javadoc] standard doclet version 1.4.2_04
     [javadoc] building tree for all the packages and classes...
     [javadoc] building index for all the packages and classes...
     [javadoc] building index for all classes...
     [javadoc] generating f:/eclipse-projects/hello/doc/stylesheet.css...
     [javadoc] note: custom tags that could override future standard tags:  @todo. to avoid potential overrides, use at least one period character (.) in custom tag names.
     [javadoc] note: custom tags that were not seen:  @todo
build successful
total time: 11 seconds

ant依次执行初始化,编译,测试,打包,生成api文档一系列任务,极大地提高了开发效率。将来开发j2ee项目时,还可加入部署等任务。并且,即使脱离了eclipse环境,只要正确安装了ant,配置好环境变量ant_home=<ant解压目录>,path=…;%ant_home%/bin,在命令行提示符下切换到hello目录,简单地键入ant即可。

扫描关注微信公众号