明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

使用ant來自動(dòng)編譯應(yīng)用、公布應(yīng)用、與制作應(yīng)用的javadoc文檔

[摘要]利用ant來自動(dòng)編譯應(yīng)用、發(fā)布應(yīng)用、和制作應(yīng)用的javadoc文檔瑪瑞2002-4-26=========================================如果你是在用文本編輯器制作你的web應(yīng)用,那么你可以在半小時(shí)內(nèi)體會(huì)到ant的強(qiáng)大幫助:1)簡(jiǎn)介antant是java世界推崇的編...
利用ant來自動(dòng)編譯應(yīng)用、發(fā)布應(yīng)用、和制作應(yīng)用的javadoc文檔
瑪瑞2002-4-26
=========================================


如果你是在用文本編輯器制作你的web應(yīng)用,那么你可以在半小時(shí)內(nèi)體會(huì)到ant的強(qiáng)大幫助:

1)簡(jiǎn)介ant
ant是java世界推崇的編譯發(fā)布工具。

它是免費(fèi)公開源代碼的軟件。

如果你用過makefile,你就可以理解這句話:ant是跨平臺(tái)的makefile。
不只如此,ant是很瞧不上makefile的,這意味著ant還有其它很多絕活。

不過,對(duì)于我們?nèi)腴T者來說,還是實(shí)用為上、夠用就行!

2)下載ant

ant主頁
http://jakarta.apache.org/ant/index.html
ant二進(jìn)制下載
http://jakarta.apache.org/builds/jakarta-ant/release/v1.4.1/bin/

3)裝載ant

(1)解開jakarta-ant-1.4.1-bin.zip或jakarta-ant-1.4.1-bin.tar.gz到一個(gè)目錄,
這個(gè)目錄就叫ant目錄
(2)將ant目錄添加到系統(tǒng)path中。

4)運(yùn)行ant
在終端窗口,在任何包含build.xml的目錄下,運(yùn)行ant,ant會(huì)自動(dòng)執(zhí)行此腳本中命令。

5)應(yīng)用ant的例子

(1)獲得例子的模板
假設(shè)tomcat被安裝在win2000下硬盤G上,即tomcat目錄是
“G:\jakarta-tomcat-4.0.1\”,則此模板在如下目錄中:
“G:\jakarta-tomcat-4.0.1\webapps\tomcat-docs\appdev\sample”

建議你將此目錄下內(nèi)容拷貝至其它地方,以供學(xué)習(xí)。如拷貝到:"G:\mytry\"

應(yīng)用程序的文件結(jié)構(gòu)及關(guān)鍵文件如下:
G:\mytrybuild.xml
 docs src web WEB-INF web.xml

其中:
build.xml是ant構(gòu)建應(yīng)用程序的腳本。
docs\是你自己提供應(yīng)用程序文檔(不包含javadoc)的地方
src\等同于tomcat應(yīng)用結(jié)構(gòu)中的“根目錄\WEB-INF\classes\"目錄,
web\等同于tomcat應(yīng)用結(jié)構(gòu)中的根目錄
web\WEB-INF\對(duì)應(yīng)于tomcat應(yīng)用結(jié)構(gòu)中的“根目錄\WEB-INF\”目錄
但不包括“根目錄\WEB-INF\classes\"目錄
“根目錄\WEB-INF\”用來存放class和servlet以及
其它不允許用戶直接訪問的東東。
web\WEB-INF\web.xml等同于tomcat應(yīng)用結(jié)構(gòu)中的“根目錄\WEB-INF\web.xml”文件,
即應(yīng)用發(fā)布描述

(2)加入你自己的文件
假設(shè)你已有了包含jsp、html、java代碼的應(yīng)用程序。不要改變?nèi)魏未a。
將jsp、html及其它靜態(tài)內(nèi)容按照原來的文件結(jié)構(gòu)復(fù)制到web\目錄下。
將java代碼按原來的文件結(jié)構(gòu)復(fù)制到src\目錄下。

假設(shè)你新建文件,也應(yīng)按照以下原則來編寫:
在各個(gè)文件中,對(duì)"\WEB-INF\classes\"的引用就相當(dāng)于對(duì)"src\"的引用。
其它照舊。

(3)在終端窗口中,進(jìn)入"G:\mytry\"目錄,運(yùn)行命令“ant”。
ant會(huì)自動(dòng)在此目錄下建立目錄:build,并在其中建立編譯后的應(yīng)用程序結(jié)構(gòu)。
ant可以自動(dòng)發(fā)布這個(gè)目錄,即將其拷貝到tomcat的webapps目錄下。

(4)執(zhí)行不同的任務(wù)
在這個(gè)模板中,主要提供以下任務(wù):
運(yùn)行“ant clean”,則清除編譯產(chǎn)生的文件結(jié)構(gòu),即刪除build目錄
運(yùn)行“ant build”,則創(chuàng)建build目錄、編譯構(gòu)建應(yīng)用程序
運(yùn)行“ant deploy”,則先執(zhí)行build任務(wù),再將build目錄下內(nèi)容發(fā)布到tomcat
運(yùn)行“ant javadoc”,則先執(zhí)行build任務(wù),再創(chuàng)建dist目錄,
并在此目錄下自動(dòng)生成應(yīng)用程序javadoc

(5)build.xml的分析和修改

以下是build.xml及其修改說明:(需修改的地方用漢字說明)

只需修改3到4處!適用于任何tomcat應(yīng)用。

你甚至根本不需要知道ant的具體用法。
每次修改了應(yīng)用,直接運(yùn)行ant,它就按照這個(gè)腳本編譯和發(fā)布。
每次只編譯和發(fā)布修改過的東東。


------------------ build.xml example for tomcat

<!-- A "project" describes a set of targets that may be requested
 when Ant is executed.The "default" attribute defines the
 target which is executed if no specific target is requested,
 and the "basedir" attribute defines the current working directory
 from which Ant executes the requested task.This is normally
 set to the current working directory.
-->


<project name="My Project" default="compile" basedir=".">
//將name的值改為應(yīng)用程序的名字,即發(fā)布到tomcat的名字
//將default的值改為你需要的缺省任務(wù)(運(yùn)行"ant"不指明任務(wù)時(shí)執(zhí)行的任務(wù))
//例如:<project name="mytry" default="deploy" basedir=".">

<!-- ===================== Property Definitions =========================== -->

<!--

Each of the following properties are used in the build script.
Values for these properties are set by the first place they are
defined, from the following list:
* Definitions on the "ant" command line (ant -Dcatalina.home=xyz compile)
* Definitions from a "build.properties" file in the top level
source directory
* Definitions from a "build.properties" file in the developer's
home directory
* Default definitions in this build.xml file

You will note below that property values can be composed based on the
contents of previously defined properties.This is a powerful technique
that helps you minimize the number of changes required when your development
environment is modified.Note that property composition is allowed within
"build.properties" files as well as in the "build.xml" script.

-->



<!-- ==================== File and Directory Names ======================== -->

<!--

These properties generally define file and directory names (or paths) that
affect where the build process stores its outputs.

app.name Base name of this application, used to
 construct filenames and directories.
 Defaults to "myapp".

app.versionVersion identifier for this application.

build.home The directory into which the "prepare" and
 "compile" targets will generate their output.
 Defaults to "build".

catalina.homeThe directory in which you have installed
 a binary distribution of Tomcat 4.This will
 be used by the "deploy" target.

deploy.homeThe name of the directory into which the
 deployment hierarchy will be created, and into
 which the build directory will be copied.
 Defaults to "${catalina.home}/webapps/${app.name}".

dist.homeThe name of the base directory in which
 distribution files are created.
 Defaults to "dist".

-->

<property name="app.name"value="myapp"/>
//將value的值改為應(yīng)用程序的名字,即發(fā)布到tomcat的名字
//例如:<property name="app.name"value="mytry"/>

<property name="app.version" value="1.0"/>
<property name="build.home"value="build"/>
<property name="catalina.home" value="../../../.."/> <!-- UPDATE THIS! -->
//將value的值改為你安裝tomcat的路徑
//例如:<property name="catalina.home" value="G:\jakarta-tomcat-4.0.1\"/>

<property name="deploy.home" value="${catalina.home}/webapps/${app.name}"/>
<property name="dist.home" value="dist"/>



<!--==================== Compilation Control Options ==================== -->

<!--

These properties control option settings on the Javac compiler when it
is invoked using the <javac> task.

compile.debugShould compilation include the debug option?

compile.deprecationShould compilation include the deprecation option?

compile.optimize Should compilation include the optimize option?

-->

<property name="compile.debug" value="true"/>
<property name="compile.deprecation" value="false"/>
<property name="compile.optimize"value="true"/>



<!-- ==================== External Dependencies =========================== -->


<!--

Use property values to define the locations of external JAR files on which
your application will depend.In general, these values will be used for
two purposes:
* Inclusion on the classpath that is passed to the Javac compiler
* Being copied into the "/WEB-INF/lib" directory during execution
of the "deploy" target.

Because we will automatically include all of the Java classes that Tomcat 4
exposes to web applications, we will not need to explicitly list any of those
dependencies.You only need to worry about external dependencies for JAR
files that you are going to include inside your "/WEB-INF/lib" directory.

-->

<!-- Dummy external dependency -->
<!--
<property name="foo.jar"
 value="/path/to/foo.jar"/>
-->


<!-- ==================== Compilation Classpath =========================== -->

<!--

Rather than relying on the CLASSPATH environment variable, Ant includes
features that makes it easy to dynamically construct the classpath you
need for each compilation.The example below constructs the compile
classpath to include the servlet.jar file, as well as the other components
that Tomcat makes available to web applications automatically, plus anything
that you explicitly added.

-->

<path id="compile.classpath">

<!-- Include all JAR files that will be included in /WEB-INF/lib -->
<!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** -->
<!--
<pathelement location="${foo.jar}"/>
-->

<!-- Include all elements that Tomcat exposes to applications -->
<pathelement location="${catalina.home}/common/classes"/>
<fileset dir="${catalina.home}/common/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${catalina.home}/classes"/>
<fileset dir="${catalina.home}/lib">
<include name="*.jar"/>
</fileset>

</path>



<!-- ==================== All Target ====================================== -->

<!--

The "all" target is a shortcut for running the "clean" target followed
by the "compile" target, to force a complete recompile.

-->

<target name="all" depends="clean,compile"
 description="Clean build and dist, then compile"/>



<!-- ==================== Clean Target ==================================== -->

<!--

The "clean" target deletes any previous "build" and "dist" directory,
so that you can be ensured the application can be built from scratch.

-->

<target name="clean"
 description="Delete old build and dist directories">
<delete dir="${build.home}"/>
<delete dir="${dist.home}"/>
</target>



<!-- ==================== Compile Target ================================== -->

<!--

The "compile" target transforms source files (from your "src" directory)
into object files in the appropriate location in the build directory.
This example assumes that you will be including your classes in an
unpacked directory hierarchy under "/WEB-INF/classes".

-->

<target name="compile" depends="prepare"
 description="Compile Java sources">

<!-- Compile Java classes as necessary -->
<mkdirdir="${build.home}/WEB-INF/classes"/>
<javac srcdir="src"
destdir="${build.home}/WEB-INF/classes"
 debug="${compile.debug}"
 deprecation="${compile.deprecation}"
optimize="${compile.optimize}">
<classpath refid="compile.classpath"/>
</javac>

<!-- Copy associated resource files -->
<copytodir="${build.home}/library/classes">
<fileset dir="src" includes="**/*.properties"/>
</copy>

</target>



<!-- ==================== Deploy Target =================================== -->

<!--

The "deploy" target copies the contents of the build directory into a
location required by our servlet container, and picks up any external
dependencies along the way.AFter restarting the servlet container, you
can now test your web application.

-->

<target name="deploy" depends="compile"
 description="Deploy application to servlet container">

<!-- Copy the contents of the build directory -->
<mkdir dir="${deploy.home}"/>
<copytodir="${deploy.home}">
<fileset dir="${build.home}"/>
</copy>

<!-- Copy external dependencies as required -->
<!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** -->
<mkdirdir="${deploy.home}/WEB-INF/lib"/>
<!--
<copy todir="${deploy.home}/WEB-INF/lib" file="${foo.jar}"/>
-->

</target>



<!-- ==================== Dist Target ===================================== -->


<!--

The "dist" target creates a binary distribution of your application
in a directory structure ready to be archived in a tar.gz or zip file.
Note that this target depends on two others:
* "deploy" so that the entire web application (including external
dependencies) will have been assembled
* "javadoc" so that the application Javadocs will have been created

-->

<target name="dist" depends="deploy,javadoc"
 description="Create binary distribution">

<!-- Copy documentation subdirectory -->
<copytodir="${dist.home}/docs">
<fileset dir="docs"/>
</copy>

<!-- Create application JAR file -->
<jar jarfile="${dist.home}/${app.name}.war"
 basedir="${deploy.home}"/>

<!-- Copy additional files to ${dist.home} as necessary -->

</target>



<!-- ==================== Javadoc Target ================================== -->

<!--

The "javadoc" target creates Javadoc API documentation for the Java
classes included in your application.Normally, this is only required
when preparing a distribution release, but is available as a separate
target in case the developer wants to create Javadocs independently.

-->

<target name="javadoc" depends="compile"
 description="Create Javadoc API documentation">

<mkdirdir="${dist.home}/docs/api"/>
<javadoc sourcepath="src"
destdir="${dist.home}/docs/api"
 packagenames="mypackage.*"/>
//改為需要制作javadoc的包名。如果不做javadoc,這里不必改。
//例如:packagenames="see.*"/>

</target>



<!-- ==================== Prepare Target ================================== -->

<!--

The "prepare" target is used to create the "build" destination directory,
and copy the static contents of your web application to it.If you need
to copy static files from external dependencies, you can customize the
contents of this task.

Normally, this task is executed indirectly when needed.

-->

<target name="prepare">

<!-- Create build directory and copy static content -->
<mkdirdir="${build.home}"/>
<copy todir="${build.home}">
<fileset dir="web"/>
</copy>

<!-- Copy static files from external dependencies as needed -->

</target>



</project>


----------------------------------------------


btw:瑪瑞從來只發(fā)自己寫的貼子。歡迎轉(zhuǎn)載。