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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义