Eclipse下使用Ant

目前的 Eclipse 都集成了 ant ,本文图示如何在 eclipse 下使用 ant
1. 新建 Java Project- 新建 Java 文件 HelloWorld.java
HelloWorld.java
package example;
public class HelloWorld {
    public static void main(String[] args) {
       System. out .println( "Hello World" );
    }
}
 
2. 在工程根目录下新建 build.xml
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 target" >
       <javac srcdir= "${basedir}/src/example" />
    </target>
    <target name= "compress" description= "Compression target" >
       <jar jarfile= "HelloWorld.jar" basedir= "${basedir}/src/example" includes= "*.class" />
    </target>
</project>
此脚本文件内容是编译 /src/example 下的 java 文件,并就地生成 class 文件,将这个 class 文件打成 jar 包, HelloWorld.jar
此时工程的目录结构如下图所示:
右键选中 HelloAnt 工程,选择 Properties
选择 Builders-New… ,选择 Ant Build
Name Ant_Builder
Buildfile ${workspace_loc:/HelloAnt/build.xml}
Base Directory ${workspace_loc:/HelloAnt}
(按 “Browse Workspace” 选择工程根目录)
Builder 面板中钩上 Ant_Build ,去掉 Java Builder ,即可编译执行。
每次编译时,右键 build.xml ,选择 Run As-Ant Build
此示例工程编译结果:
Buildfile: D:\dev\Workspaces\J2EE\HelloAnt\build.xml
compile :
compress :
main :
     [ echo ] Building the .jar file.
BUILD SUCCESSFUL
Total time: 281 milliseconds
 

你可能感兴趣的:(eclipse,ant,职场,休闲)