2006-6-24

Installing Ant
The binary distribution of Ant consists of the following directory layout:

  ant
   +--- bin  // contains launcher scripts
   |
   +--- lib  // contains Ant jars plus necessary dependencies
   |
   +--- docs // contains documentation
   |      +--- ant2    // a brief description of ant2 requirements
   |      |
   |      +--- images  // various logos for html documentation
   |      |
   |      +--- manual  // Ant documentation (a must read ;-)
   |
   +--- etc // contains xsl goodies to:
            //   - create an enhanced report from xml output of various tasks.
            //   - migrate your build files and get rid of 'deprecated' warning
            //   - ... and more ;-)

Only the bin and lib directories are required to run Ant. To install Ant, choose a directory and copy the distribution file there. This directory will be known as ANT_HOME.
在安装ant的时候,需要两个环境变量。ANT_HOME指定ant的安装目录,Path指定bin的目录路径。
-------------------------------------

Using Ant
Each Buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this.

A project has three attributes: name (required no), default(the default target to use when no target is supplied) (required no), basedir (required no).
Optionally, a description for the project can be provided as a top-level <description> element. It is include in the output of the ant - projecthelp command. The description has no parameters. One example:
<description>
This buildfile is used to build the Foo subproject within
the large, complex Bar project.
</description>

Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed.
A target has the following attributes: name (required yes), depends, if, unless, description.

A task is a piece of code that can be executed.
Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />

Example Buildfile
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
--------------------------------------

Running Ant
Examples
ant
runs Ant using the build.xml file in the current directory, on the default target.

ant -buildfile test.xml
runs Ant using the test.xml file in the current directory, on the default target.

ant -buildfile test.xml dist
runs Ant using the test.xml file in the current directory, on the target called dist.
-------------------------------------------

上面的内容都是从manual中copy出来的,觉得我用到的特征应该不多。会按装、会写简单的build.xml、以及运行ant就可以了。Ant提供的tasks很多,可能大部分都是复杂工程当中所需的,但是对于我个人写的小projiect,这些tasks就显得没有必要了。简单是一种美德:-),这也是懒得借口之一。
--------------------------------------------

Developing with Ant
Tutorials
Hello World with Ant

We want to separate the source from the generated files, so our java source files will be in src folder. All generated files should be under build, and there splitted into several subdirectories for the individual steps: classes for our compiled files and jar for our own JAR-file.
The later directories are created by our buildfile, so we have to create only the src directory.
write this code into src/oata/HelloWorld.java 代码如下:
  package oata;
  public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World");
      }
  }

The most simplest buildfile describing that would be:写一个最简单的build.xml:
<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="oata.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

Now you can compile, package and run the application via

ant compile
ant jar
ant run

Or shorter with

ant compile jar run

完善build file:
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="oata.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

Now it's easier, just do a ant and you will get

Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: C:\...\build\classes
    [javac] Compiling 1 source file to C:\...\build\classes

jar:
    [mkdir] Created dir: C:\...\build\jar
      [jar] Building jar: C:\...\build\jar\HelloWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL

========这下面是我尝试了这例子后在cmd中的输出==========
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

D:\doctemp\project helloworld using ant>ant
Buildfile: build.xml

clean:
   [delete] Deleting directory D:\doctemp\project helloworld using ant\build

compile:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\classes
    [javac] Compiling 1 source file to D:\doctemp\project helloworld using ant\b
uild\classes

jar:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\jar
      [jar] Building jar: D:\doctemp\project helloworld using ant\build\jar\Hell
oWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL
Total time: 2 seconds
D:\doctemp\project helloworld using ant>

=====================================================

此后还讲了使用外部libraries的例子,这里省去。

 

posted @ 2006-06-26 01:27 cjren 阅读(708) | 评论 (0)编辑 收藏
 

2006-6-24
小叙
之前写很小的程序的时候,几个源文件和编译后的class文件统一都放在同一个目录之下,倒也相安无事而且省事。但是在做毕设(坦克对战游戏)的时候,多则二十个的源文件,再连同那些class文件都放在一个目录下,显得非常的杂乱。于是觉得是时候考虑一下一个project的目录结构了,例如把源文件和class文件分开,把测试文件也统一在另一个独立的目录下。

回顾和插曲
之前用C语言程序学习编写小的编译器的时候,我就曾不解怎么把一个程序分为几个独立的文件。那时的做法很傻但简单,就是尽量不用.h头文件,只使用.c文件,然后一层一层的include。后来看《The C Programming Language》第四章head files,才较为清楚地知道了怎样通过.h文件来把一个程序分割为几个小的源代码文件。
这里有一个小的插曲,在3月末的研究生复式中,其中一道编程题是实现stack数据结构,提供push,pop和get的函数实现,并提供一到两个测试例子。我是用C实现的,在实现过程中分为了三个文件,其中两个.c文件和一个.h文件。分别是main.c stack.c and stack.h。
stack.h:定义了main.c and stack.c公用的stack struct变量,以及对main.c将需要用到的和在stack.c里由于函数定义顺序的关系需要到的函数进行了声明(区别于定义)。
stack.c:include “stack.h”,定义了一系列函数,可以为其它文件中所调用(这也是为什么这当中的一些函数需要在stack.h中声明的原因)。
main.c:include “stack.h”,使用到了stack.c中的函数,也就是执行了两个测试例子而已。
stack的具体操作实现细节这个略过了。考试的结果得了A,想必这种源代码文件的组合关系也另评分的老师觉得清晰和有序:-)

原则
这方面的工具(build tool: 名字是构建工具)最流行的好像是ant,在看《Developing Games in Java》的时候,作者也是通过提供build.xml给ant来完成相应的工作,于是我尝试把build.xml看明白了之后,第一印象就是它可以很好的处理一个project的各种文件的目录结构:-),第二印象是又得使用不熟悉的工具了:-(。我觉得自己很懒,懒在不想学一些可能使用不多或不大有用的工具,同时觉得怕,怕在于这个ant是人家的,我害怕一个东西以我不清楚的方法和细节完成工作。这涉及一个学习新工具时候的原则问题:一,它好用吗,可以meet my need吗;二,它是怎么完成的啊,我想“看”清楚它的做法;三,够简单吗。
ant是一个现成的魔术,而我希望自己可以使用现掌握的简单的知识做一个最简单的而且合乎我要求的魔术,而最要紧的是我知道它到底做了什么,它是怎么做了。

工作
现在我有必要自己来完成两件事情:一:明确在一个project目录下有什么目录;二,我怎么实现把各种文件(如.java, .class, .jar, .html)存放到相应的目录并且让它们和调的协作呢?我现在只懂编译时候的-d参数和运行时候的-cp参数。

(1)
尝试学习ant的最简的使用方法,记录在"mynotes about ant -ant的最简单使用方法.txt"文件中。
(2)
下面尝试自己来构建目录结构,而不使用ant。记录在"2006-06-24build project.doc"文件中。


----------------------------
附录一:bat批处理文件中常用的命令
date /t
time /t

mkdir or md
del /Q or earse /Q
rmdir or rd

echo
@echo off
rem
pause

date /t > a.txt | type a.txt //间接地建立一个a.txt文件并把文件的内容显示出来

posted @ 2006-06-26 01:25 cjren 阅读(346) | 评论 (0)编辑 收藏
 
最近完成了毕设和答辩,毕设的主要内容是(1)做一个单机版的坦克游戏,(2)在此基础上尝试加入联网操控和聊天功能。

单机版的特点:title-based map,双人对战,自定义地图
r_tankbattle-1.PNG

提供给玩家使用的地图编辑器:
mapeditor1.PNG

posted @ 2006-06-24 00:13 cjren 阅读(1035) | 评论 (8)编辑 收藏
 
        还有一个礼拜就毕业回广东了,但是离研一的开学时间还有两个多月,现在正在找广州和佛山地区的实习机会。
        实习工作希望和软件开发有关的,但也不是硬性一定要跟计算机有关。实习嘛,为了锻炼和学习平时学校里没有的东西而已。
posted @ 2006-06-23 21:50 cjren 阅读(191) | 评论 (0)编辑 收藏
仅列出标题
共2页: 上一页 1 2