几条总结:
一。在ant的构建文件中,根元素始终是:<project>
二。ant的概念模型:项目包含目标,目标包含任务,也就是把所有的目标放在目标中,即:所有的target,在project根标签下,且不要有其他东西在project根标签下。所有的执行过程,在targert标签中
三。在ant中,任何一个任务失败,会导致整个构建过程的终止
四。javac命令中依赖关系检查仅仅局限于在比对源文件与目标文件的日期上
我的代码:
HelloWorld.java//主类
package com.laolang.hello;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import com.laolang.stu.Student;public class HelloWorld { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); Student stu = null; int id = 0; String name = null; int age = 0; String sex = null; try { System.out.print("输入学号:"); id = Integer.parseInt(br.readLine()); System.out.print("输入姓名:"); name = br.readLine(); System.out.print("输入年龄:"); age = Integer.parseInt(br.readLine()); System.out.print("输入性别:"); sex = br.readLine(); } catch (NumberFormatException | IOException e) { e.printStackTrace(); } stu = new Student(id, name, age, sex); System.out.println("student:"); System.out.println(stu); System.out.println("Hello World!"); System.out.println("小代码!"); }}
Student.java//学生类
package com.laolang.stu;public class Student { public Student() { super(); } public Student(int stuId, String stuName, int stuAge, String stuSex) { super(); this.stuId = stuId; this.stuName = stuName; this.stuAge = stuAge; this.stuSex = stuSex; } @Override public String toString() { return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", stuSex=" + stuSex + "]"; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } private int stuId; private String stuName; private int stuAge; private String stuSex;}
build.xml
这是使用ant进行JAVA开发的第一个完整的例子
运行效果:
laolang@laolang:~/code/ant/book/two/first_build_35$ l总用量 8.0K4.0K -rw-rw-r-- 1 laolang laolang 1011 11月 21 21:54 build.xml4.0K drwxrwxr-x 3 laolang laolang 4.0K 11月 21 21:12 src/laolang@laolang:~/code/ant/book/two/first_build_35$ tree.├── build.xml└── src └── com └── laolang ├── hello │ └── HelloWorld.java └── stu └── Student.java5 directories, 3 fileslaolang@laolang:~/code/ant/book/two/first_build_35$ ant -projecthelp Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xml这是使用ant进行JAVA开发的第一个完整的例子Main targets: archive 打包 clean 清除中间文件 compile 编译 execute 运行 init 初始化Default target: archivelaolang@laolang:~/code/ant/book/two/first_build_35$ ant compileBuildfile: /home/laolang/code/ant/book/two/first_build_35/build.xmlinit: [mkdir] Created dir: /home/laolang/code/ant/book/two/first_build_35/build/classes [mkdir] Created dir: /home/laolang/code/ant/book/two/first_build_35/distcompile: [javac] /home/laolang/code/ant/book/two/first_build_35/build.xml:14: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to /home/laolang/code/ant/book/two/first_build_35/build/classesBUILD SUCCESSFULTotal time: 1 secondlaolang@laolang:~/code/ant/book/two/first_build_35$ ant execute Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xmlinit:compile: [javac] /home/laolang/code/ant/book/two/first_build_35/build.xml:14: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildsexecute: [echo] running1001tianya34nan [java] 输入学号:输入姓名:输入年龄:输入性别:student: [java] Student [stuId=1001, stuName=tianya, stuAge=34, stuSex=nan] [java] Hello World! [java] 小代码!BUILD SUCCESSFULTotal time: 7 secondslaolang@laolang:~/code/ant/book/two/first_build_35$ ant archive Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xmlinit:compile: [javac] /home/laolang/code/ant/book/two/first_build_35/build.xml:14: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildsarchive: [jar] Building jar: /home/laolang/code/ant/book/two/first_build_35/dist/project.jarBUILD SUCCESSFULTotal time: 0 secondslaolang@laolang:~/code/ant/book/two/first_build_35$ tree.├── build│ └── classes│ └── com│ └── laolang│ ├── hello│ │ └── HelloWorld.class│ └── stu│ └── Student.class├── build.xml├── dist│ └── project.jar└── src └── com └── laolang ├── hello │ └── HelloWorld.java └── stu └── Student.java12 directories, 6 fileslaolang@laolang:~/code/ant/book/two/first_build_35$ ant cleanBuildfile: /home/laolang/code/ant/book/two/first_build_35/build.xmlinit:clean: [delete] Deleting directory /home/laolang/code/ant/book/two/first_build_35/build [delete] Deleting directory /home/laolang/code/ant/book/two/first_build_35/distBUILD SUCCESSFULTotal time: 0 secondslaolang@laolang:~/code/ant/book/two/first_build_35$ tree.├── build.xml└── src └── com └── laolang ├── hello │ └── HelloWorld.java └── stu └── Student.java5 directories, 3 fileslaolang@laolang:~/code/ant/book/two/first_build_35$ l总用量 8.0K4.0K -rw-rw-r-- 1 laolang laolang 1011 11月 21 21:54 build.xml4.0K drwxrwxr-x 3 laolang laolang 4.0K 11月 21 21:12 src/laolang@laolang:~/code/ant/book/two/first_build_35$