下载与安装
安装
略
版本使用 1.9.15
文档
可在安装目录下找到
hello world
代码
目录结构如下
com.laolang.km 下添加一个java文件,内容如下
package com.laolang.km;
public class KmApplication {
public static void main(String[] args) {
int a = 1;
a = 2;
a = 3;
a = 4;
a = 5;
a = 6;
System.out.println("hello world");
}
}
build.xml内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-one" default="run" basedir=".">
<property name="src" value="src" />
<property name="target" value="target" />
<property name="classes" value="target/classes" />
<property name="jar" value="target/ant-one.jar" />
<target name="init">
<mkdir dir="${target}" />
<mkdir dir="${classes}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${classes}" includeantruntime="on" debug="on" source="1.8" target="1.8" />
</target>
<target name="build" depends="compile">
<jar jarfile="${jar}" basedir="${classes}" />
</target>
<target name="run" depends="build">
<java classname="com.laolang.km.KmApplication" classpath="${jar}" fork="true" />
<!--<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432"/>
</java>-->
</target>
<target name="run1" depends="build">
<!--<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432"/>
</java>-->
</target>
<target name="clean">
<delete dir="${target}" />
<delete file="${jar}" />
</target>
<target name="rerun">
<ant target="clean" />
<ant target="run" />
</target>
</project>
eclipse 打开方式如下
关于DTD
参考:
https://blog.csdn.net/weixin_34184158/article/details/91885338
https://blog.csdn.net/m0_37355951/article/details/78580674
https://www.cnblogs.com/lzsu1989/archive/2011/07/26/2117718.html
可添加如下task, 自动生成dtd
<target name="makeantdtd">
<antstructure output="ant.dtd" />
</target>
将生成的ant.dtd
使用eclipse打开并验证
此时会提示有很多错误,参考上面的博客,共修改4处
-
每个build.xml至少应该有一个project
-
write 和 read 未定义, telnet 暂时也用不到
-
同上
-
删除
将ant.dtd
和build.xml放在同级目录然后添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "ant.dtd">
此时在验证已经没有问题
添加lib
添加lib文件夹,并添加一个jar包
在eclipse
中将此jar包加入构建路径
build.xml内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "ant.dtd"> -->
<project name="ant-one" default="run" basedir=".">
<!-- 源码目录 -->
<property name="src.dir" value="src" />
<!-- 输出目录 -->
<property name="target.dir" value="target" />
<!-- class目录 -->
<property name="classes.dir" value="classes" />
<!-- jar名称 -->
<property name="jar_name" value="ant-one.jar" />
<!-- 三方库目录 -->
<property name="lib.dir" value="lib" />
<!-- 主类 -->
<property name="mainclass" value="com.laolang.km.KmApplication" />
<!-- 第三方jar包的路径 -->
<path id="lib-classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- 初始化, 新建目录 -->
<target name="init">
<mkdir dir="${target.dir}" />
<mkdir dir="${target.dir}/${classes.dir}" />
<echo>init complete!</echo>
</target>
<!-- 编译 -->
<target name="compile" depends="init">
<!--
deprecation : 提示过期API
-->
<javac srcdir="${src.dir}" destdir="${target.dir}/${classes.dir}" includeantruntime="on" debug="on" encoding="UTF-8" deprecation="true" source="1.8" target="1.8">
<classpath refid="lib-classpath" />
</javac>
<echo>compile complete!</echo>
</target>
<!-- 构建jar包 -->
<target name="build" depends="compile">
<copy todir="${target.dir}/${lib.dir}">
<fileset dir="${lib.dir}" />
</copy>
<!--Create a property containing all .jar files,
prefix lib/, and seperated with a space-->
<pathconvert property="mf.classpath" pathsep=" ">
<mapper>
<chainedmapper>
<!-- jar包文件只留文件名,去掉目录信息 -->
<flattenmapper />
<!-- add lib/ prefix -->
<globmapper from="*" to="lib/*" />
</chainedmapper>
</mapper>
<path refid="lib-classpath" />
</pathconvert>
<jar jarfile="${target.dir}/${jar_name}" basedir="${target.dir}/${classes.dir}">
<manifest>
<attribute