1. a.下载ant: http://ant.apache.org/bindownload.cgi
b.安装ant: 解压ant,路径越简单越好(这是好习惯),设置环境变量ANT_HOME,PATH里添加;%ANT_HOME%/bin
c.打开cmd,键入ant回车,如果出现提示信息
"Buildfile: build.xml does not exist!
Build failed"
就说明安装成功了
2. java工程目录结构:
|-src-|- //.java源文件
|-bin-|- // .class文件
|-lib-|- //第三方jar包
build.xml // ant 配置文件
3。由于ant没有提供填写jar包里Class-Path的任务,所以需要编写自定义任务,扩展Ant,Java代码如下:
- package personal.lw.util;
- import java.io.File;
- import org.apache.tools.ant.BuildException;
- import org.apache.tools.ant.Task;
- public class AntUtil extends Task {
- private String lib;
- private String classPath = "";
- private String fileType = ".jar";
- // ant task
- public void execute() throws BuildException {
- try {
- this.classPath(dir(lib));
- this.getOwningTarget().getProject().setProperty("class-path",
- classPath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // 根据路径获得目录
- private File dir(String path) throws Exception {
- return dir(path, false);
- }
- private File dir(String path, boolean bCreate) throws Exception {
- File dir = new File(path);
- if (dir.exists()) {
- if (!dir.isDirectory()) {
- throw new Exception(dir.getName() + "is not a directory !");
- }
- } else {
- if (bCreate) { // 新建
- if (!dir.mkdirs()) {
- throw new Exception("can not create the directory /""
- + dir.getName() + "/"!");
- }
- }
- }
- return dir;
- }
- // 生成classpath字符串
- private void classPath(File dir) {
- classPath(dir, lib);
- classPath = classPath.trim();
- }
- private void classPath(File dir, String parentPath) {
- // 处理目录
- if (dir.isDirectory()) {
- File[] subs = dir.listFiles();
- for (int i = 0, len = subs.length; i < len; i++) {
- if (subs[i].isFile()) { // 文件
- String name = subs[i].getName();
- // 处理.jar文件
- if (name.length() > fileType.length()
- && fileType.equals(name.substring(name.length()
- - fileType.length(), name.length()))) {
- classPath += parentPath + path(subs[i]) + " ";
- continue;
- }
- }
- if (subs[i].isDirectory()) { // 文件夹
- classPath(subs[i], parentPath + path(subs[i]));
- }
- }
- } else {
- // 不是目录
- System.out.println(dir.getPath() + dir.getName()
- + "is not a directory !");
- }
- }
- // 文件路径
- private String path(File f) {
- return "/" + f.getName();
- }
- // 字符串 -- > 路径
- @SuppressWarnings("unused")
- private String path(String path) {
- path = path.replaceAll("////", "/");
- if (!"/".equals(path.substring(0, 1))) {
- path = "/" + path;
- }
- return path;
- }
- public void setLib(String lib) {
- this.lib = lib;
- }
- }
编译后打成jar包,拷贝到%ANT_HOME%/lib目录下,注意编译的时候需要导入ant的jar包,如果用ant打包就不需要导ant的jar包了,生成普通jar包的ant配置文件如下:
- <?xml version="1.0" encoding="GB2312" standalone="no"?>
- <project basedir="." default="usage" name="hibernate"> <!-- ====此处需要修改====工程名 -->
- <!-- 工程名,没有被引用,可以不改 -->
- <property name="project-name" value="hibernate"/> <!-- ====此处需要修改====.jar文件名 -->
- <property name="lib" value="lib"/> <!-- lib-->
- <property name="src" value="src"/> <!-- src-->
- <property name="tar" value="bin"/> <!-- bin -->
- <property name="jar-file-name" value="${project-name}.jar"/>
- <property name="main-class" value="demo.book.controler.Test"/> <!-- ====此处需要修改====main-class-->
- <path id="Third-Part Lib">
- <fileset dir="${lib}">
- <include name="**/*.jar"/>
- </fileset>
- </path>
- <target description="Build file usage info (default task)" name="usage">
- <echo message=" "/>
- <echo message=" ${project-name} "/>
- <echo message="-------------------------------------------------------"/>
- <echo message=" Available Targets:"/>
- <echo message=" compile - Compiles the source code"/>
- <echo message=" clean - Delete class files and .jar file"/>
- <echo message=" jar - Generate an .jar for source code"/>
- <echo message=" run - Execute Main-Class"/>
- <echo message="-------------------------------------------------------"/>
- </target>
- <target name="prepare">
- <mkdir dir="${tar}"/>
- </target>
- <target name="clean">
- <delete dir="${tar}"/>
- <delete file="${jar-file-name}"/>
- </target>
- <target name="copy-res">
- <copy todir="${tar}">
- <fileset dir="${src}">
- <exclude name="**/*.java"/>
- </fileset>
- </copy>
- </target>
- <target depends="clean,prepare,copy-res" name="compile">
- <javac debug="true" deprecation="true" destdir="${tar}" failonerror="true" srcdir="${src}">
- <classpath refid="Third-Part Lib"/>
- </javac>
- </target>
- <!--
- <target name="run" depends="jar">
- <java jar="${jar-file-name}" fork="true" maxmemory="256m"/>
- </target>
- -->
- <!-- 注意:classpath="${tar}" 一定要加上,否则会报"java.lang.NoClassDefFoundError"的错误!-->
- <target depends="compile" name="run">
- <java classname="${main-class}" classpath="${tar}" fork="true" maxmemory="256m">
- <classpath refid="Third-Part Lib"/>
- </java>
- </target>
- <target depends="compile" name="jar">
- <jar basedir="${tar}" destfile="${jar-file-name}">
- <manifest>
- <attribute name="Main-Class" value="${main-class}"/>
- <attribute name="Class-Path" value=""/>
- </manifest>
- </jar>
- </target>
- </project>
4.生成可执行jar包的ant配置文件如下:
- <?xml version="1.0" encoding="GB2312" standalone="no"?>
- <project basedir="." default="usage" name="hibernate"> <!-- ====此处需要修改====工程名 -->
- <!-- 工程名,没有被引用,可以不改 -->
- <property name="project-name" value="hibernate"/> <!-- ====此处需要修改====.jar文件名 -->
- <property name="lib" value="lib"/> <!-- lib-->
- <property name="src" value="src"/> <!-- src-->
- <property name="tar" value="bin"/> <!-- bin -->
- <property name="jar-file-name" value="${project-name}.jar"/>
- <property name="main-class" value="demo.book.controler.Test"/> <!-- ====此处需要修改====main-class-->
- <path id="Third-Part Lib">
- <fileset dir="${lib}">
- <include name="**/*.jar"/>
- </fileset>
- </path>
- <target description="Build file usage info (default task)" name="usage">
- <echo message=" "/>
- <echo message=" ${project-name} "/>
- <echo message="-------------------------------------------------------"/>
- <echo message=" Available Targets:"/>
- <echo message=" compile - Compiles the source code"/>
- <echo message=" clean - Delete class files and .jar file"/>
- <echo message=" jar - Generate an .jar for source code"/>
- <echo message=" run - Execute Main-Class"/>
- <echo message="-------------------------------------------------------"/>
- </target>
- <target name="prepare">
- <mkdir dir="${tar}"/>
- </target>
- <target name="clean">
- <delete dir="${tar}"/>
- <delete file="${jar-file-name}"/>
- </target>
- <target name="copy-res">
- <copy todir="${tar}">
- <fileset dir="${src}">
- <exclude name="**/*.java"/>
- </fileset>
- </copy>
- </target>
- <target depends="clean,prepare,copy-res" name="compile">
- <javac debug="true" deprecation="true" destdir="${tar}" failonerror="true" srcdir="${src}">
- <classpath refid="Third-Part Lib"/>
- </javac>
- </target>
- <!--
- <target name="run" depends="jar">
- <java jar="${jar-file-name}" fork="true" maxmemory="256m"/>
- </target>
- -->
- <!-- 注意:classpath="${tar}" 一定要加上,否则会报"java.lang.NoClassDefFoundError"的错误!-->
- <target depends="compile" name="run">
- <java classname="${main-class}" classpath="${tar}" fork="true" maxmemory="256m">
- <classpath refid="Third-Part Lib"/>
- </java>
- </target>
- <!-- 自定义任务,创建并赋值变量class-path -->
- <taskdef name="jarcp" classname="personal.lw.util.AntUtil"/>
- <target name="class-path">
- <jarcp lib="${lib}"/>
- </target>
- <target depends="compile,class-path" name="jar">
- <jar basedir="${tar}" destfile="${jar-file-name}">
- <manifest>
- <attribute name="Main-Class" value="${main-class}"/>
- <attribute name="Class-Path" value="${class-path}"/>
- </manifest>
- </jar>
- </target>
- </project>
只比上一个多了一个自定义任务。
(完) ^_^
Ant构建Java项目

本文介绍如何使用Apache Ant构建Java项目,包括安装配置、目录结构、自定义任务扩展Ant功能及生成可执行jar包的方法。
3万+

被折叠的 条评论
为什么被折叠?



