zoukankan      html  css  js  c++  java
  • ant编译java的例子

    ant hello world
    建一上文件夹HelloWorld.里面的内容如下所示:

    第一个例子不讨论build1.xml和HelloWorld1.java.运行出helloworld程序要如下步骤:
    1.建一个文件夹,用于保存编译成的.class
    2.编译.java文件,生成.class文件
    3.运行程序,出结果
    第1步通过下面代码实现

    <target name="init" depends="clean" description="Removes the temporary directories used">
        <mkdir dir="out/classes" />
    </target>
    

    第2步通过下面代码实现

    <target name="compile" depends="init" description="Compiles the source code">
            <javac srcdir=".src" destdir="out/classes" includeantruntime="false"/>
            <echo>compilation complete!</echo>
        </target>
    

    第3步通过下面代码实现.

    <target name="execute" depends="compile">
            <java
                    classname="edu.cgxy.helloworld.HelloWorld"
                    classpath="out/classes">
            </java>
    </target>
    

    build.xml的完整例子如下:

    <?xml version="1.0"?>
    <project name="HelloWorld" default="execute" >
        <description>Compiles and runs a simple program</description>
        <target name="init" depends="clean" description="Removes the temporary directories used">
            <mkdir dir="out/classes" />
        </target>
        <target name="compile" depends="init" description="Compiles the source code">
            <javac srcdir=".src" destdir="out/classes" includeantruntime="false"/>
            <echo>compilation complete!</echo>
        </target>
        <target name="clean">
            <delete dir="out" />
        </target>
        <target name="execute" depends="compile">
            <java
                    classname="edu.cgxy.helloworld.HelloWorld"
                    classpath="out/classes">
            </java>
        </target>
    </project>
    

    执行命令ant -f build1.xml可以运行结果. 还有一个名字为clean的target上面没有介绍.作用是运行程序之后先把以前的结果删除.
    程序源代码: https://github.com/zhouyang209117/AntTutorial/tree/master/HelloWorld

  • 相关阅读:
    Java 字典
    java 集合
    Java Array类、大数据运算、包装类
    java String、StringBuilder 、正则表达式
    Java时间日期类 Date、DateFormat、Calendar类
    java包的声明、导入、System类
    java正则表达式
    java-StringBuffer类和StringBuilder类
    java-String类
    java的API,Object类,equals方法,toString方法
  • 原文地址:https://www.cnblogs.com/zhouyang209117/p/5411046.html
Copyright © 2011-2022 走看看