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的時(shí)候,需要兩個(gè)環(huán)境變量。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.
          -------------------------------------------

          上面的內(nèi)容都是從manual中copy出來的,覺得我用到的特征應(yīng)該不多。會(huì)按裝、會(huì)寫簡單的build.xml、以及運(yùn)行ant就可以了。Ant提供的tasks很多,可能大部分都是復(fù)雜工程當(dāng)中所需的,但是對(duì)于我個(gè)人寫的小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:寫一個(gè)最簡單的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) 版權(quán)所有 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 閱讀(716) | 評(píng)論 (0)編輯 收藏
           

          2006-6-24
          小敘
          之前寫很小的程序的時(shí)候,幾個(gè)源文件和編譯后的class文件統(tǒng)一都放在同一個(gè)目錄之下,倒也相安無事而且省事。但是在做畢設(shè)(坦克對(duì)戰(zhàn)游戲)的時(shí)候,多則二十個(gè)的源文件,再連同那些class文件都放在一個(gè)目錄下,顯得非常的雜亂。于是覺得是時(shí)候考慮一下一個(gè)project的目錄結(jié)構(gòu)了,例如把源文件和class文件分開,把測(cè)試文件也統(tǒng)一在另一個(gè)獨(dú)立的目錄下。

          回顧和插曲
          之前用C語言程序?qū)W習(xí)編寫小的編譯器的時(shí)候,我就曾不解怎么把一個(gè)程序分為幾個(gè)獨(dú)立的文件。那時(shí)的做法很傻但簡單,就是盡量不用.h頭文件,只使用.c文件,然后一層一層的include。后來看《The C Programming Language》第四章head files,才較為清楚地知道了怎樣通過.h文件來把一個(gè)程序分割為幾個(gè)小的源代碼文件。
          這里有一個(gè)小的插曲,在3月末的研究生復(fù)式中,其中一道編程題是實(shí)現(xiàn)stack數(shù)據(jù)結(jié)構(gòu),提供push,pop和get的函數(shù)實(shí)現(xiàn),并提供一到兩個(gè)測(cè)試?yán)印N沂怯肅實(shí)現(xiàn)的,在實(shí)現(xiàn)過程中分為了三個(gè)文件,其中兩個(gè).c文件和一個(gè).h文件。分別是main.c stack.c and stack.h。
          stack.h:定義了main.c and stack.c公用的stack struct變量,以及對(duì)main.c將需要用到的和在stack.c里由于函數(shù)定義順序的關(guān)系需要到的函數(shù)進(jìn)行了聲明(區(qū)別于定義)。
          stack.c:include “stack.h”,定義了一系列函數(shù),可以為其它文件中所調(diào)用(這也是為什么這當(dāng)中的一些函數(shù)需要在stack.h中聲明的原因)。
          main.c:include “stack.h”,使用到了stack.c中的函數(shù),也就是執(zhí)行了兩個(gè)測(cè)試?yán)佣选?BR>stack的具體操作實(shí)現(xiàn)細(xì)節(jié)這個(gè)略過了。考試的結(jié)果得了A,想必這種源代碼文件的組合關(guān)系也另評(píng)分的老師覺得清晰和有序:-)

          原則
          這方面的工具(build tool: 名字是構(gòu)建工具)最流行的好像是ant,在看《Developing Games in Java》的時(shí)候,作者也是通過提供build.xml給ant來完成相應(yīng)的工作,于是我嘗試把build.xml看明白了之后,第一印象就是它可以很好的處理一個(gè)project的各種文件的目錄結(jié)構(gòu):-),第二印象是又得使用不熟悉的工具了:-(。我覺得自己很懶,懶在不想學(xué)一些可能使用不多或不大有用的工具,同時(shí)覺得怕,怕在于這個(gè)ant是人家的,我害怕一個(gè)東西以我不清楚的方法和細(xì)節(jié)完成工作。這涉及一個(gè)學(xué)習(xí)新工具時(shí)候的原則問題:一,它好用嗎,可以meet my need嗎;二,它是怎么完成的啊,我想“看”清楚它的做法;三,夠簡單嗎。
          ant是一個(gè)現(xiàn)成的魔術(shù),而我希望自己可以使用現(xiàn)掌握的簡單的知識(shí)做一個(gè)最簡單的而且合乎我要求的魔術(shù),而最要緊的是我知道它到底做了什么,它是怎么做了。

          工作
          現(xiàn)在我有必要自己來完成兩件事情:一:明確在一個(gè)project目錄下有什么目錄;二,我怎么實(shí)現(xiàn)把各種文件(如.java, .class, .jar, .html)存放到相應(yīng)的目錄并且讓它們和調(diào)的協(xié)作呢?我現(xiàn)在只懂編譯時(shí)候的-d參數(shù)和運(yùn)行時(shí)候的-cp參數(shù)。

          (1)
          嘗試學(xué)習(xí)ant的最簡的使用方法,記錄在"mynotes about ant -ant的最簡單使用方法.txt"文件中。
          (2)
          下面嘗試自己來構(gòu)建目錄結(jié)構(gòu),而不使用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 //間接地建立一個(gè)a.txt文件并把文件的內(nèi)容顯示出來

          posted @ 2006-06-26 01:25 cjren 閱讀(351) | 評(píng)論 (0)編輯 收藏
           
          最近完成了畢設(shè)和答辯,畢設(shè)的主要內(nèi)容是(1)做一個(gè)單機(jī)版的坦克游戲,(2)在此基礎(chǔ)上嘗試加入聯(lián)網(wǎng)操控和聊天功能。

          單機(jī)版的特點(diǎn):title-based map,雙人對(duì)戰(zhàn),自定義地圖
          r_tankbattle-1.PNG

          提供給玩家使用的地圖編輯器:
          mapeditor1.PNG

          posted @ 2006-06-24 00:13 cjren 閱讀(1043) | 評(píng)論 (8)編輯 收藏
           
          ??????? 還有一個(gè)禮拜就畢業(yè)回廣東了,但是離研一的開學(xué)時(shí)間還有兩個(gè)多月,現(xiàn)在正在找廣州和佛山地區(qū)的實(shí)習(xí)機(jī)會(huì)。
          ??????? 實(shí)習(xí)工作希望和軟件開發(fā)有關(guān)的,但也不是硬性一定要跟計(jì)算機(jī)有關(guān)。實(shí)習(xí)嘛,為了鍛煉和學(xué)習(xí)平時(shí)學(xué)校里沒有的東西而已。
          posted @ 2006-06-23 21:50 cjren 閱讀(198) | 評(píng)論 (0)編輯 收藏
          僅列出標(biāo)題
          共2頁: 上一頁 1 2 
           
          主站蜘蛛池模板: 阿荣旗| 蒲江县| 赣榆县| 稷山县| 洞头县| 济源市| 肥东县| 阜宁县| 北京市| 盐山县| 盐池县| 桂平市| 桓台县| 马鞍山市| 莲花县| 玉门市| 湖州市| 仲巴县| 怀仁县| 天门市| 镇巴县| 桐城市| 阳江市| 石嘴山市| 南靖县| 上林县| 若尔盖县| 顺义区| 房产| 锡林浩特市| 南雄市| 荔浦县| 博罗县| 桃江县| 赫章县| 工布江达县| 祥云县| 微山县| 黑龙江省| 伊吾县| 从江县|