ant读书之使用ant进行java开发--第二章



几条总结:

一。在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



<?xml version="1.0" encoding="utf-8"?>
<project name="hello" default="archive">
	<description>这是使用ant进行JAVA开发的第一个完整的例子</description>

	<!-- 初始化  -->
	<target name="init" description="初始化">
		<mkdir dir="build/classes" />
		<mkdir dir="dist" />
	</target>


	<!-- 编译  -->
	<target name="compile" depends="init" description="编译">
		<javac srcdir="src" destdir="build/classes" />
	</target>

	<!-- 打包  -->
	<target name="archive" depends="compile" description="打包">
		<jar destfile="dist/project.jar" basedir="build/classes" />
	</target>

	<!-- 清除  -->
	<target name="clean" depends="init" description="清除中间文件">
		<delete dir="build" />
		<delete dir="dist" />
	</target>

	<!-- 运行  -->
	<target name="execute" depends="compile" description="运行">
		<echo level="warning" message="running" />
		<java
			classname="com.laolang.hello.HelloWorld"
			classpath="build/classes">
			<arg file="." />
		</java>
	</target>
</project>


运行效果:



laolang@laolang:~/code/ant/book/two/first_build_35$ l
总用量 8.0K
4.0K -rw-rw-r-- 1 laolang laolang 1011 11月 21 21:54 build.xml
4.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.java

5 directories, 3 files
laolang@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: archive
laolang@laolang:~/code/ant/book/two/first_build_35$ ant compile
Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xml

init:
    [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/dist

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 builds
    [javac] Compiling 2 source files to /home/laolang/code/ant/book/two/first_build_35/build/classes

BUILD SUCCESSFUL
Total time: 1 second
laolang@laolang:~/code/ant/book/two/first_build_35$ ant execute 
Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xml

init:

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 builds

execute:
     [echo] running
1001
tianya
34
nan
     [java] 输入学号:输入姓名:输入年龄:输入性别:student:
     [java] Student [stuId=1001, stuName=tianya, stuAge=34, stuSex=nan]
     [java] Hello World!
     [java] 小代码!

BUILD SUCCESSFUL
Total time: 7 seconds
laolang@laolang:~/code/ant/book/two/first_build_35$ ant archive 
Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xml

init:

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 builds

archive:
      [jar] Building jar: /home/laolang/code/ant/book/two/first_build_35/dist/project.jar

BUILD SUCCESSFUL
Total time: 0 seconds
laolang@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.java

12 directories, 6 files
laolang@laolang:~/code/ant/book/two/first_build_35$ ant clean
Buildfile: /home/laolang/code/ant/book/two/first_build_35/build.xml

init:

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/dist

BUILD SUCCESSFUL
Total time: 0 seconds
laolang@laolang:~/code/ant/book/two/first_build_35$ tree
.
├── build.xml
└── src
    └── com
        └── laolang
            ├── hello
            │   └── HelloWorld.java
            └── stu
                └── Student.java

5 directories, 3 files
laolang@laolang:~/code/ant/book/two/first_build_35$ l
总用量 8.0K
4.0K -rw-rw-r-- 1 laolang laolang 1011 11月 21 21:54 build.xml
4.0K drwxrwxr-x 3 laolang laolang 4.0K 11月 21 21:12 src/
laolang@laolang:~/code/ant/book/two/first_build_35$




转载于:https://my.oschina.net/iamhere/blog/347344

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值