目前的Eclipse都集成了ant。
1.新建一个Java Project名为HelloAnt。并在其中新建Java文件HelloWorld.java:
2.在项目根目录下新建build.xml:
此脚本文件内容是编译/src/demo下的java文件,并就地生成class文件,将这个class文件打成jar包,HelloWorld.jar。
此时工程的目录结构如下图所示:
[img]http://dl2.iteye.com/upload/attachment/0085/5744/4b38c867-ce19-37fa-900f-0fb29fb786cc.png[/img]
3.右键选中HelloAnt工程,选择Properties:
[img]http://dl2.iteye.com/upload/attachment/0085/5746/26aa13ce-f9e8-38ca-991e-bcf8585e7f00.png[/img]
选择Builders-New…,选择Ant Build:
[img]http://dl2.iteye.com/upload/attachment/0085/5748/26063675-12ef-3b7c-82a6-38a573996f17.png[/img]
在弹出窗口填写一下内容:
Name:Ant_Builder
Buildfile:${workspace_loc:/HelloAnt/build.xml}
Base Directory:${workspace_loc:/HelloAnt}
(按“Browse Workspace”选择工程根目录)
[img]http://dl2.iteye.com/upload/attachment/0085/5782/839b2d05-3236-382d-bbe6-2c8b0da9a746.png[/img]
在Builder面板中钩上Ant_Build,去掉Java Builder,即可编译执行。
[img]http://dl2.iteye.com/upload/attachment/0085/5784/0afa3b30-cba0-329b-8831-b82de9a5f2c6.png[/img]
每次编译时,右键build.xml,选择Run As-Ant Build:
[img]http://dl2.iteye.com/upload/attachment/0085/5786/fb4c67ea-2647-3534-a6e6-566ed06cdc89.png[/img]
编译结果:
1.新建一个Java Project名为HelloAnt。并在其中新建Java文件HelloWorld.java:
package demo;
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
2.在项目根目录下新建build.xml:
<?xml version="1.0" encoding="utf-8"?>
<project default="main" basedir=".">
<target name="main" depends="compile, compress" description="Main target">
<echo>Building the .jar file.</echo>
</target>
<target name="compile" description="Compilation tartget">
<javac srcdir="${basedir}/src/demo" />
</target>
<target name="compress" description="Compression target">
<jar jarfile="HelloWorld.jar" basedir="${basedir}/src/demo" includes="*.class" />
</target>
</project>
此脚本文件内容是编译/src/demo下的java文件,并就地生成class文件,将这个class文件打成jar包,HelloWorld.jar。
此时工程的目录结构如下图所示:
[img]http://dl2.iteye.com/upload/attachment/0085/5744/4b38c867-ce19-37fa-900f-0fb29fb786cc.png[/img]
3.右键选中HelloAnt工程,选择Properties:
[img]http://dl2.iteye.com/upload/attachment/0085/5746/26aa13ce-f9e8-38ca-991e-bcf8585e7f00.png[/img]
选择Builders-New…,选择Ant Build:
[img]http://dl2.iteye.com/upload/attachment/0085/5748/26063675-12ef-3b7c-82a6-38a573996f17.png[/img]
在弹出窗口填写一下内容:
Name:Ant_Builder
Buildfile:${workspace_loc:/HelloAnt/build.xml}
Base Directory:${workspace_loc:/HelloAnt}
(按“Browse Workspace”选择工程根目录)
[img]http://dl2.iteye.com/upload/attachment/0085/5782/839b2d05-3236-382d-bbe6-2c8b0da9a746.png[/img]
在Builder面板中钩上Ant_Build,去掉Java Builder,即可编译执行。
[img]http://dl2.iteye.com/upload/attachment/0085/5784/0afa3b30-cba0-329b-8831-b82de9a5f2c6.png[/img]
每次编译时,右键build.xml,选择Run As-Ant Build:
[img]http://dl2.iteye.com/upload/attachment/0085/5786/fb4c67ea-2647-3534-a6e6-566ed06cdc89.png[/img]
编译结果:
Buildfile: D:\dev\eclipse\workspace\HelloAnt\build.xml
compile:
[javac] D:\dev\eclipse\workspace\HelloAnt\build.xml:7: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file
compress:
[jar] Building jar: D:\dev\eclipse\workspace\HelloAnt\HelloWorld.jar
main:
[echo] Building the .jar file.
BUILD SUCCESSFUL
Total time: 1 second