1 Ant是什么?
Apache Ant 是一個基于 Java的生成工具。
生成工具在軟件開發中用來將源代碼和其他輸入文件轉換為可執行文件的形式(也有可能轉換為可安裝的產品映像形式)。隨著應用程序的生成過程變得更加復雜,確保在每次生成期間都使用精確相同的生成步驟,同時實現盡可能多的自動化,以便及時產生一致的生成版本
2下載、安裝Ant
安裝Ant
下載.zip文件,解壓縮到c:\ant1.3(后面引用為%ANT_HOME%)
?
2.1在你運行Ant之前需要做一些配置工作。
·將bin目錄加入PATH環境變量。
·設定ANT_HOME環境變量,指向你安裝Ant的目錄。在一些OS上,Ant的腳本可以猜測ANT_HOME(Unix和Windos NT/2000)-但最好不要依賴這一特性。
·可選地,設定JAVA_HOME環境變量(參考下面的高級小節),該變量應該指向你安裝JDK的目錄。
注意:不要將Ant的ant.jar文件放到JDK/JRE的lib/ext目錄下。Ant是個應用程序,而lib/ext目錄是為JDK擴展使用的(如JCE,JSSE擴展)。而且通過擴展裝入的類會有安全方面的限制。
2.2運行Ant
運行Ant非常簡單,當你正確地安裝Ant后,只要輸入ant就可以了。
?
沒有指定任何參數時,Ant會在當前目錄下查詢build.xml文件。如果找到了就用該文件作為buildfile。如果你用 -find 選項。Ant就會在上級目錄中尋找buildfile,直至到達文件系統的根。要想讓Ant使用其他的buildfile,可以用參數 -buildfile file,這里file指定了你想使用的buildfile。
?
可以指定執行一個或多個target。當省略target時,Ant使用標簽<project>的default屬性所指定的target。
?
?
命令行選項總結:
ant [options] [target [target2 [target3] ...]]
Options:
-help print this message
-projecthelp print project help information
-version print the version information and exit
-quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-emacs produce logging information without adornments
-logfile file use given file for log output
-logger classname the class that is to perform logging
-listener classname add an instance of class as a project listener
-buildfile file use specified buildfile
-find file search for buildfile towards the root of the filesystem and use the first one found
-Dproperty=value set property to value
例子
ant
使用當前目錄下的build.xml運行Ant,執行缺省的target。
ant -buildfile test.xml
使用當前目錄下的test.xml運行Ant,執行缺省的target。
ant -buildfile test.xml dist
使用當前目錄下的test.xml運行Ant,執行一個叫做dist的target。
ant -buildfile test.xml -Dbuild=build/classes dist
使用當前目錄下的test.xml運行Ant,執行一個叫做dist的target,并設定build屬性的值為build/classes。
?
3編寫build.xml
Ant的buildfile是用XML寫的。每個buildfile含有一個project。
?
buildfile中每個task元素可以有一個id屬性,可以用這個id值引用指定的任務。這個值必須是唯一的。(詳情請參考下面的Task小節)
?
3.1Projects
?
project有下面的屬性:
AttributeDescriptionRequired
name項目名稱.No
default當沒有指定target時使用的缺省targetYes
basedir用于計算所有其他路徑的基路徑。該屬性可以被basedir property覆蓋。當覆蓋時,該屬性被忽略。如果屬性和basedir property都沒有設定,就使用buildfile文件的父目錄。No
項目的描述以一個頂級的<description>元素的形式出現(參看description小節)。
?
一個項目可以定義一個或多個target。一個target是一系列你想要執行的。執行Ant時,你可以選擇執行那個target。當沒有給定target時,使用project的default屬性所確定的target。
?
3.2Targets
?
一個target可以依賴于其他的target。例如,你可能會有一個target用于編譯程序,一個target用于生成可執行文件。你在生成可執行文件之前必須先編譯通過,所以生成可執行文件的target依賴于編譯target。Ant會處理這種依賴關系。
?
然而,應當注意到,Ant的depends屬性只指定了target應該被執行的順序-如果被依賴的target無法運行,這種depends對于指定了依賴關系的target就沒有影響。
?
Ant會依照depends屬性中target出現的順序(從左到右)依次執行每個target。然而,要記住的是只要某個target依賴于一個target,后者就會被先執行。
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
假定我們要執行target D。從它的依賴屬性來看,你可能認為先執行C,然后B,最后A被執行。錯了,C依賴于B,B依賴于A,所以先執行A,然后B,然后C,最后D被執行。
?
一個target只能被執行一次,即時有多個target依賴于它(看上面的例子)。
?
如果(或如果不)某些屬性被設定,才執行某個target。這樣,允許根據系統的狀態(java version, OS, 命令行屬性定義等等)來更好地控制build的過程。要想讓一個target這樣做,你就應該在target元素中,加入if(或unless)屬性,帶上target因該有所判斷的屬性。例如:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
如果沒有if或unless屬性,target總會被執行。
?
可選的description屬性可用來提供關于target的一行描述,這些描述可由-projecthelp命令行選項輸出。
?
將你的tstamp task在一個所謂的初始化target是很好的做法,其他的target依賴這個初始化target。要確保初始化target是出現在其他target依賴表中的第一個target。在本手冊中大多數的初始化target的名字是"init"。
?
target有下面的屬性:
AttributeDescriptionRequired
nametarget的名字Yes
depends用逗號分隔的target的名字列表,也就是依賴表。No
if執行target所需要設定的屬性名。No
unless執行target需要清除設定的屬性名。No
description關于target功能的簡短描述。No
?
3.3Tasks
?
一個task是一段可執行的代碼。
?
一個task可以有多個屬性(如果你愿意的話,可以將其稱之為變量)。屬性只可能包含對property的引用。這些引用會在task執行前被解析。
?
下面是Task的一般構造形式:
<name attribute1="value1" attribute2="value2" ... />
這里name是task的名字,attributeN是屬性名,valueN是屬性值。
?
有一套內置的(built-in)task,以及一些可選task,但你也可以編寫自己的task。
?
所有的task都有一個task名字屬性。Ant用屬性值來產生日志信息。
?
可以給task賦一個id屬性:
<taskname id="taskID" ... />
這里taskname是task的名字,而taskID是這個task的唯一標識符。通過這個標識符,你可以在腳本中引用相應的task。例如,在腳本中你可以這樣:
<script ... >
task1.setFoo("bar");
</script>
設定某個task實例的foo屬性。在另一個task中(用java編寫),你可以利用下面的語句存取相應的實例。
project.getReference("task1").
注意1:如果task1還沒有運行,就不會被生效(例如:不設定屬性),如果你在隨后配置它,你所作的一切都會被覆蓋。
?
注意2:未來的Ant版本可能不會兼容這里所提的屬性,因為很有可能根本沒有task實例,只有proxies。
?
3.4Properties
?
一個project可以有很多的properties。可以在buildfile中用property task來設定,或在Ant之外設定。一個property有一個名字和一個值。property可用于task的屬性值。這是通過將屬性名放在"
{"和"}"之間并放在屬性值的位置來實現的。例如如果有一個property builddir的值是"build",這個property就可用于屬性值:
{builddir}/classes。這個值就可被解析為build/classes。
內置屬性
?
如果你使用了<property> task 定義了所有的系統屬性,Ant允許你使用這些屬性。例如,
{os.name}對應操作系統的名字。
?
要想得到系統屬性的列表可參考the Javadoc of System.getProperties。
?
除了Java的系統屬性,Ant還定義了一些自己的內置屬性:
basedir project基目錄的絕對路徑 (與<project>的basedir屬性一樣)。
ant.file buildfile的絕對路徑。
ant.version Ant的版本。
ant.project.name 當前執行的project的名字;由<project>的name屬性設定.
ant.java.version Ant檢測到的JVM的版本;目前的值有"1.1", "1.2", "1.3" and "1.4".
例子
<project name="MyProject" default="dist" basedir=".">
?
<!-- set global properties for this build -->
<property name="src" value="."/>
<property name="build" value="build"/>
<property name="dist" value="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">
<!-- Compile the java code from
{src} into
{build} -->
<javac srcdir="
{src}" destdir="
{build}"/>
</target>
<target name="dist" depends="compile">
<!-- 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">
<!-- Delete the
{build} and
{dist} directory trees -->
<delete dir="
{build}"/>
<delete dir="
{dist}"/>
</target>
</project>
?
3.5Path-like Structures
你可以用":"和";"作為分隔符,指定類似PATH和CLASSPATH的引用。Ant會把分隔符轉換為當前系統所用的分隔符。
?
當需要指定類似路徑的值時,可以使用嵌套元素。一般的形式是
<classpath>
<pathelement path="
{classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>
location屬性指定了相對于project基目錄的一個文件和目錄,而path屬性接受逗號或分號分隔的一個位置列表。path屬性一般用作預定義的路徑--其他情況下,應該用多個location屬性。
?
為簡潔起見,classpath標簽支持自己的path和location屬性。所以:
<classpath>
<pathelement path="
{classpath}"/>
</classpath>
可以被簡寫作:
<classpath path="
{classpath}"/>
也可通過<fileset>元素指定路徑。構成一個fileset的多個文件加入path-like structure的順序是未定的。
<classpath>
<pathelement path="
{classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</classpath>
上面的例子構造了一個路徑值包括:
{classpath}的路徑,跟著lib目錄下的所有jar文件,接著是classes目錄。
?
如果你想在多個task中使用相同的path-like structure,你可以用<path>元素定義他們(與target同級),然后通過id屬性引用--參考Referencs例子。
?
path-like structure可能包括對另一個path-like structurede的引用(通過嵌套<path>元素):
<path id="base.path">
<pathelement path="
{classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
<path id="tests.path">
<path refid="base.path"/>
<pathelement location="testclasses"/>
</path>
前面所提的關于<classpath>的簡潔寫法對于<path>也是有效的,如:
<path id="tests.path">
<path refid="base.path"/>
<pathelement location="testclasses"/>
</path>
可寫成:
<path id="base.path" path="
{classpath}"/>
命令行變量
?
有些task可接受參數,并將其傳遞給另一個進程。為了能在變量中包含空格字符,可使用嵌套的arg元素。
AttributeDescriptionRequired
value一個命令行變量;可包含空格字符。只能用一個
line空格分隔的命令行變量列表。
file作為命令行變量的文件名;會被文件的絕對名替代。
path一個作為單個命令行變量的path-like的字符串;或作為分隔符,Ant會將其轉變為特定平臺的分隔符。
?
例子
<arg value="-l -a"/>
是一個含有空格的單個的命令行變量。
<arg line="-l -a"/>
是兩個空格分隔的命令行變量。
<arg path="/dir;/dir2:\dir3"/>
是一個命令行變量,其值在DOS系統上為\dir;\dir2;\dir3;在Unix系統上為/dir:/dir2:/dir3 。
?
References
?
buildfile元素的id屬性可用來引用這些元素。如果你需要一遍遍的復制相同的XML代碼塊,這一屬性就很有用--如多次使用<classpath>結構。
?
下面的例子:
<project ... >
<target ... >
<rmic ...>
<classpath>
<pathelement location="lib/"/>
<pathelement path="
{java.class.path}/"/>
<pathelement path="
{additional.path}"/>
</classpath>
</rmic>
</target>
<target ... >
<javac ...>
<classpath>
<pathelement location="lib/"/>
<pathelement path="
{java.class.path}/"/>
<pathelement path="
{additional.path}"/>
</classpath>
</javac>
</target>
</project>
可以寫成如下形式:
<project ... >
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="
{java.class.path}/"/>
<pathelement path="
{additional.path}"/>
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... >
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
所有使用PatternSets, FileSets 或 path-like structures嵌套元素的task也接受這種類型的引用。
4Ant的Core Tasks
4.1File(Directory)類
4.1.1Mkdir
創建一個目錄,如果他的父目錄不存在,也會被同時創建。
例子:
<mkdir dir="build/classes"/>
說明:如果build不存在,也會被同時創建
4.1.2Copy
拷貝一個(組)文件、目錄
例子:
1.拷貝單個的文件:
<copy file="myfile.txt" tofile="mycopy.txt"/>
2.拷貝單個的文件到指定目錄下
<copy file="myfile.txt" todir="../some/other/dir"/>
3.拷貝一個目錄到另外一個目錄下
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
4.拷貝一批文件到指定目錄下
<copy todir="../dest/dir">
<fileset dir="src_dir">
<include name="**/*.java"/>
<iexclude name="**/Test.java"/>
</fileset>
</copy>
?
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
5.拷貝一批文件到指定目錄下,將文件名后增加。Bak后綴
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<mapper type="glob" from="*" to="*.bak"/>
</copy>
6.拷貝一組文件到指定目錄下,替換其中的@標簽@內容
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>
4.1.3Delete
刪除一個(組)文件或者目錄
例子
1.刪除一個文件
<delete file="/lib/ant.jar"/>
2.刪除指定目錄及其子目錄
<delete dir="lib"/>
3.刪除指定的一組文件
<delete>
<fileset dir="." includes="**/*.bak"/>
</delete>
4.刪除指定目錄及其子目錄,包括他自己
<delete includeEmptyDirs="true">
<fileset dir="build"/>
</delete>
4.1.4Move
移動或重命名一個(組)文件、目錄
例子:
1.移動或重命名一個文件
<move file="file.orig" tofile="file.moved"/>
2.移動或重命名一個文件到另一個文件夾下面
<move file="file.orig" todir="dir/to/move/to"/>
3.將一個目錄移到另外一個目錄下
<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>
4.將一組文件移動到另外的目錄下
<move todir="some/new/dir">
<fileset dir="my/src/dir">
<include name="**/*.jar"/>
<exclude name="**/ant.jar"/>
</fileset>
</move>
5.移動文件過程中增加。Bak后綴
<move todir="my/src/dir">
<fileset dir="my/src/dir">
<exclude name="**/*.bak"/>
</fileset>
<mapper type="glob" from="*" to="*.bak"/>
</move>
?
4.2Java相關
4.2.1Javac
編譯java原代碼
例子
1.<javac srcdir="
{src}"
destdir="
{build}"
classpath="xyz.jar"
debug="on"
/>
編譯
{src}目錄及其子目錄下的所有。Java文件,。Class文件將放在
{build}指定的目錄下,classpath表示需要用到的類文件或者目錄,debug設置為on表示輸出debug信息
2.<javac srcdir="
{src}:
{src2}"
destdir="
{build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
編譯
{src}和
{src2}目錄及其子目錄下的所有。Java文件,但是package/p1/**,mypackage/p2/**將被編譯,而mypackage/p1/testpackage/**將不會被編譯。Class文件將放在
{build}指定的目錄下,classpath表示需要用到的類文件或者目錄,debug設置為on表示輸出debug信息
3.<property name="classpath" value=".;./xml-apis.jar;../lib/xbean.jar;./easypo.jar"/>
?
<javac srcdir="
{src}"
destdir="
{src}"
classpath="
{classpath}"
debug="on"
/>
路徑是在property中定義的
4.2.2java
執行指定的java類
例子:
1.<java classname="test.Main">
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="
{java.class.path}"/>
</classpath>
</java>
classname中指定要執行的類,classpath設定要使用的環境變量
2.<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="
{java.class.path}/"/>
<pathelement path="
{additional.path}"/>
</path>
?
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
?
4.3打包相關
4.3.1jar
將一組文件打包
例子:
1.<jar destfile="
{dist}/lib/app.jar" basedir="
{build}/classes"/>
將
{build}/classes下面的所有文件打包到
{dist}/lib/app.jar中
2.<jar destfile="
{dist}/lib/app.jar"
basedir="
{build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class"
/>
將
{build}/classes下面的所有文件打包到
{dist}/lib/app.jar中,但是包括mypackage/test/所有文件不包括所有的Test.class
3.<jar destfile="
{dist}/lib/app.jar"
basedir="
{build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class"
manifest="my.mf"
/>
manifest屬性指定自己的META-INF/MANIFEST.MF文件,而不是由系統生成
4.3.2war
對Jar的擴展,用于打包Web應用
例子:
假設我們的文件目錄如下:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
下面是我們的任務的內容:
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
完成后的結果:
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
4.3.3ear
用于打包企業應用
例子
<ear destfile="
{build.dir}/myapp.ear" appxml="
{src.dir}/metadata/application.xml">
<fileset dir="
{build.dir}" includes="*.jar,*.war"/>
</ear>
4.4時間戳
在生成環境中使用當前時間和日期,以某種方式標記某個生成任務的輸出,以便記錄它是何時生成的,這經常是可取的。這可能涉及編輯一個文件,以便插入一個字符串來指定日期和時間,或將這個信息合并到 JAR 或 zip 文件的文件名中。
這種需要是通過簡單但是非常有用的 tstamp 任務來解決的。這個任務通常在某次生成過程開始時調用,比如在一個 init 目標中。這個任務不需要屬性,許多情況下只需 <tstamp/> 就足夠了。
tstamp 不產生任何輸出;相反,它根據當前系統時間和日期設置 Ant 屬性。下面是 tstamp 設置的一些屬性、對每個屬性的說明,以及這些屬性可被設置到的值的例子:
屬性說明例子
DSTAMP 設置為當前日期,默認格式為yyyymmdd 20031217
TSTAMP 設置為當前時間,默認格式為 hhmm 1603
TODAY 設置為當前日期,帶完整的月份2003 年 12 月 17 日
例如,在前一小節中,我們按如下方式創建了一個 JAR 文件:
?
<jar destfile="package.jar" basedir="classes"/>
?
在調用 tstamp 任務之后,我們能夠根據日期命名該 JAR 文件,如下所示:
?
<jar destfile="package-
{DSTAMP}.jar" basedir="classes"/>
?
因此,如果這個任務在 2003 年 12 月 17 日調用,該 JAR 文件將被命名為 package-20031217.jar。
還可以配置 tstamp 任務來設置不同的屬性,應用一個當前時間之前或之后的時間偏移,或以不同的方式格式化該字符串。所有這些都是使用一個嵌套的 format 元素來完成的,如下所示:
<tstamp>
<format property="OFFSET_TIME"
pattern="HH:mm:ss"
offset="10" unit="minute"/>
</tstamp>
?
上面的清單將 OFFSET_TIME 屬性設置為距離當前時間 10 分鐘之后的小時數、分鐘數和秒數。
用于定義格式字符串的字符與 java.text.SimpleDateFormat 類所定義的那些格式字符相同
4.5執行SQL語句
通過jdbc執行SQL語句
例子:
1.<sql
driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/mydb"
userid="root"
password="root"
src="data.sql"
/>
2.<sql
driver="org.database.jdbcDriver"
url="jdbc:database-url"
userid="sa"
password="pass"
src="data.sql"
rdbms="oracle"
version="8.1."
>
</sql>
只有在oracle、版本是8.1的時候才執行
4.6發送郵件
使用SMTP服務器發送郵件
例子:
<mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build">
<from address="me@myisp.com"/>
<to address="all@xyz.com"/>
<message>The
{buildname} nightly build has completed</message>
<fileset dir="dist">
<includes name="**/*.zip"/>
</fileset>
</mail>
mailhost: SMTP服務器地址
mailport:服務器端口
subject:主題
from:發送人地址
to:接受人地址
message:發送的消息
fileset:設置附件
Ant常用功能
1,想知道這段時間CVS做過哪些改動,CVS修改記錄
??? <target name="cvschangelog" description="make the change log">
??? <cvschangelog dir="
{src}" destfile="changelog.xml" start="25 Apr 2004"/>
??? <style in="changelog.xml"
?????????????? out="changelog.html"
?????????????? style="
{ant.home}/etc/changelog.xsl">
????????? <param name="title" expression="Ant ChangeLog"/>
????????? <param name="module" expression="ant"/>
??????? </style>
??? </target>
這個任務生成25 Apr 2004到現在CVS的修改記錄,其中包括了style命令來將XML轉化為可讀性強的HTML
?
2,單元測試報告
??? <target name="test" depends="compile">
??????? <junit haltonfailure="true" printsummary="true">??
??????????? <classpath refid="test.classpath"/>
??????????? <formatter type="brief" usefile="false"/>
??????????? <formatter type="xml"/>
??????????? <batchtest todir="data">
??????????????? <fileset dir="
{test.dir}" includes="**/*Test.class"/>
??????????? </batchtest>
??????? </junit>
???????
??????? <junitreport todir="data">
??????????? <fileset dir="data">
??????????????? <include name="TEST-*.xml"/>
??????????? </fileset>?
??????????? <report format="frames" todir="data"/>
??????? </junitreport>
???????
??? </target>
???
想看所有的JUNIT結果,就這樣吧!持續集成,自動測試。
?
3,生成todolist這里要用到xdoclet包,是擴展的任務。他會把有java代碼中todo的標簽的內容做成文檔,格式類似javadoc。
??? <target name="todolist">
??? <taskdef name="document" classname="xdoclet.modules.doc.DocumentDocletTask" classpathref="xdoclet.classpath"/>
??? <document destdir="todo" >
??????? <fileset dir="src">???
??????????? <include name="**/*.java" />?
??????? </fileset>?
??????? <info header="To-do list" projectname="Custom Ant Task" tag="todo"/>
??? </document>
??? </target>
??? 這只是一種最簡單的應用,復雜的還有生成servlet的描述符,EJB描述符,Hibernate配置文件。
???
4,java程序寫的不規范,怎么辦,使用checkstyle檢查一下,這里要去下載checkstyle的包放到路徑里。
<target name="checkstyle" description="Check the code's style">
??? <taskdef resource="checkstyletask.properties">
??????? <classpath refid="cms.classpath"/>
??? </taskdef>
??? <checkstyle config="sun_checks.xml" failOnViolation="false">
??????? <fileset dir="
{src.dir}" includes="**/*.java"/>
??????? <formatter type="plain"/>
??????? <formatter type="xml" toFile="checkstyle_errors.xml"/>
??? </checkstyle>
</target>
?
<target name="checkreport" description="Check the code's style's report">
??? <style in="checkstyle_errors.xml" out="checkstyle_report.html" style="checkstyle-frames.xsl"/>
</target>
?
這樣會生成一個報告,包括所有不合規范的代碼,檢查一下,真是很多。
?
5,好玩的功能,聲音和圖片。
?
???? <target name="init">???
???????? <sound>?????
???????????? <success source="sound/5.wav" loops="1"/>
???????????? <fail source="sound/a.wav" loops="2"/>???
???????? </sound>?
???? </target>??
?????
???? <target name="fail" depends="init">???
???????? <fail/>
???? </target>???
????
???? <target name="success" depends="init"/>???
???? 這樣就會在執行success或fail的時候就會播放上面的聲音,就像jbuilder編譯完了的聲音。
????
???? <target name="show_pic" depends="success">???
??????? <splash imageurl="http://localhost/x.jpg"? showduration="2000"/>
??????? <sleep seconds="1"/>
??????? <sleep seconds="1"/>
??????? <sleep seconds="1"/>???
??????? <sleep seconds="1"/>???
??????? <sleep seconds="1"/>???
??????? <sleep seconds="1"/>?
??? </target>
??? 桌面回探出窗口顯示URL里的圖片,可惜不知道為什么本地圖片路徑不可以。
XDoclet基本任務手冊
任何人都想過自動生成的代碼,自己去寫個分析器不是件容易的事,而XDoclet給我們機會建立自己的代碼生成器。
?
XDoclet最早用來對付EJB,因為生成EJB的本地和遠程接口以及對應的HOME和描述符是一件簡單但是枯燥的事,本來我以為,只有JBuilder這樣的集成編輯器可以很好的簡化工作,后來知道XDoclet也能夠完成得不錯。
我最早用XDoclet是因為學習hibernate,hibetnate的描述文件并不復雜,關鍵的部分不多,可是一旦對POJO修改,就必須要勞師動眾的找到對應的描述符來修改,增加了一次出錯的機會,而使用了XDoclet就可以做到同步的修改。
還有就是struts,首先是配置文件,許多人操作同一個文件會產生沖突,有了XDoclet我們就不怕了,還有validate文件也一樣,有了XDoclet就會解決大部分沖突問題。
之所以起這么一個名字,主要因為XDoclet和Ant結合得很緊,盡管實際上做的工作并沒有直接的聯系,但XDoclet除了Ant接口就只有些Maven接口插件了,所以XDoclet幾乎是完全依賴Ant的。
?
?
本篇文章的目錄結構如下,因為只是為了說明問題,在我的ant的build文件中并沒有包括路徑名的引用,一切是直接的方式。
├─classes
├─doc
├─gen
├─lib
│commons-collections-2.1.jar
│commons-logging-1.0.3.jar
│commons-validator.jar
│log4j-1.2.8.jar
│servlet.jar
│struts.jar
│xdoclet-1.2.1.jar
│xdoclet-apache-module-1.2.1.jar
│xdoclet-ejb-module-1.2.1.jar
│xdoclet-hibernate-module-1.2.1.jar
│xdoclet-web-module-1.2.1.jar
│xdoclet-xdoclet-module-1.2.1.jar
│xjavadoc-1.0.3.jar
├─merge
├─src
├─todo
└─web
?
只列出lib中的文件,每一個的作用在后面慢慢描述。
?
build.xml
開頭增加
<path id="xdoclet.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
**************************************************************************************************
?
1,最簡單的todolist
?
每一篇講XDoclet都送這里開始,有很多原因的。XDoclet的靈感來自JavaDoc,JavaDoc把文檔寫在代碼里,緩解了困擾編程領域多年的文檔與程序同步問題。這里有個很有趣的事,就是UNIX業界的人們傳遞下來這樣一個傳統,就是代碼是最好的文檔,保持文檔的同步實在是費力不討好的事,所以他們提出這樣一個好主意,不過JavaDoc更聰明,文檔是程序注釋的一部分,而且可以提取出來。
?
來吧,看這個任務。
<target name="todolist" description="todolist">
<taskdef name="documentdoclet" classname="xdoclet.modules.doc.DocumentDocletTask" classpathref="xdoclet.classpath"/>
<documentdoclet destdir="todo" >
<fileset dir="src">
<include name="**/*.java" />
</fileset>
<info header="Todo list" projectname="XDoclet in Action" tag="todo"/>
</documentdoclet>
</target>
?
然后src寫這么一個文件
package xdoclet;
?
public class TodoListTest {
/**
* @todo 我有許多工作要做,只是測試,忽略吧
*/
public TodoListTest() {
}
?
/**
* @todo 我還不知道名字,只是測試,忽略吧
*
*/
public String getYourName(){
return null;
}
}
?
注意要按照javadoc的寫法。還要注意ant中的子任務系統,其中info就是我們定義的documentdoclet任務的子任務,我們以后會看到很多類似的情況.
然后運行ant todolist
結果就是一個結構類似javadoc,但是只包括todo標簽的html文檔,呵呵,可以看看項目里有哪些待辦的事。
?
?
?
?
?
2,web.xml和taglib
作servlet映射是個討厭的工作,當你接收別的項目的時候,這個項目的servlet怎么用可能比較麻煩,可能當時web.xml的映射找不到了,這時怎么辦呢?
?
看這個文件
package com.xdocletbook.blog.servlet;
?
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
?
/**
* @web.servlet
* name="HomePage"
* @web.servlet-init-param
* name="LogLevel"
* value="
{LOG_LEVEL}"
* @web.servlet-mapping
* url-pattern="/home"
*
* @web.security-role
*role-name="
{OwnerRole}"
* @web.security-role-ref
*role-name="blogowner"
*role-link="
{OwnerRole}"
*/
?
?
public class HomePageServlet
extends HttpServlet {
private static Logger LOGGER = Logger.getLogger(HomePageServlet.class);
public void init() throws ServletException {
String logLevel = getInitParameter("LogLevel");
if (logLevel != null && logLevel.length() > 0) {
LOGGER.setLevel(Level.toLevel(logLevel));
}
}
?
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
LOGGER.debug("Displaying home page");
request.getRequestDispatcher("jsp/home.jsp").forward(request, response);
}
}
然后配置build.xml增加如下任務
?
<target name="generate-web">
<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask" classpathref="xdoclet.classpath"/>
<!-- Generate servlet and JSP Tag "stuff" -->
<webdoclet destdir="gen" mergeDir="merge">
<fileset dir="src">
<include name="**/*.java" />
</fileset>
<deploymentdescriptor destdir="web" distributable="false" />
</webdoclet>
</target>
?
然后運行ant generate-web
?
結果就是這樣一個web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
?
<web-app >
?
<!--
To use non XDoclet filters, create a filters.xml file that
contains the additional filters (eg Sitemesh) and place it in your
project's merge dir.Don't include filter-mappings in this file,
include them in a file called filter-mappings.xml and put that in
the same directory.
-->
?
<!--
To use non XDoclet filter-mappings, create a filter-mappings.xml file that
contains the additional filter-mappings and place it in your
project's merge dir.
-->
?
<!--
To use non XDoclet listeners, create a listeners.xml file that
contains the additional listeners and place it in your
project's merge dir.
-->
?
<servlet>
<servlet-name>StrutsActionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
?
<servlet>
<servlet-name>HomePage</servlet-name>
<servlet-class>com.xdocletbook.blog.servlet.HomePageServlet</servlet-class>
?
<init-param>
<param-name>LogLevel</param-name>
<param-value>1</param-value>
</init-param>
?
<security-role-ref>
<role-name>blogowner</role-name>
<role-link>aOwner</role-link>
</security-role-ref>
</servlet>
?
<servlet-mapping>
<servlet-name>StrutsActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
?
<servlet-mapping>
<servlet-name>HomePage</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
?
<!--
To specify mime mappings, create a file named mime-mappings.xml, put it in your project's mergedir.
Organize mime-mappings.xml following this DTD slice:
?
<!ELEMENT mime-mapping (extension, mime-type)>
-->
?
<!--
To specify error pages, create a file named error-pages.xml, put it in your project's mergedir.
Organize error-pages.xml following this DTD slice:
?
<!ELEMENT error-page ((error-code | exception-type), location)>
-->
?
<!--
To add taglibs by xml, create a file called taglibs.xml and place it
in your merge dir.
-->
?
<!--
To set up security settings for your web app, create a file named web-security.xml, put it in your project's mergedir.
Organize web-security.xml following this DTD slice:
?
<!ELEMENT security-constraint (display-name?, web-resource-collection+, auth-constraint?, user-data-constraint?)>
<!ELEMENT web-resource-collection (web-resource-name, description?, url-pattern*, http-method*)>
<!ELEMENT web-resource-name (#PCDATA)>
<!ELEMENT url-pattern (#PCDATA)>
<!ELEMENT http-method (#PCDATA)>
<!ELEMENT user-data-constraint (description?, transport-guarantee)>
<!ELEMENT transport-guarantee (#PCDATA)>
?
<!ELEMENT login-config (auth-method?, realm-name?, form-login-config?)>
<!ELEMENT auth-method (#PCDATA)>
<!ELEMENT realm-name (#PCDATA)>
<!ELEMENT form-login-config (form-login-page, form-error-page)>
<!ELEMENT form-login-page (#PCDATA)>
<!ELEMENT form-error-page (#PCDATA)>
-->
?
<security-role>
<role-name>aOwner</role-name>
</security-role>
?
</web-app>
?
?
仔細看這個文件,你一定詫異struts的配置信息怎樣得來,這是XDoclet的另一種方式,對于第三方的Servlet,我們沒有辦法再處理原代碼,所以我們有了merge選項,看<webdoclet destdir="gen" mergeDir="merge">這一句就知道了,在merge目錄里我們有兩個文件:
servlets.xml
?
<servlet>
<servlet-name>StrutsActionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
servlet-mappings.xml
?
<servlet-mapping>
<servlet-name>StrutsActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
還要注意的是
{LOG_LEVEL},這個是說要引用ant 中的變量,所以我的build.xml前面中增加了這兩項
<property name="LOG_LEVEL" value="1"/>
<property name="OwnerRole" value="aOwner"/>
?
所以,我們就可以動態的改變部署的Log級別
3,struts自動配置
?
Struts中有兩樣比較重要的類,Action和Form。
對于Action,我們需要配置Action的映射和Forward屬性,對于Form我們也需要注冊名字和校驗參數,以下就是我們能用XDoclet做到的。
對于Action我們寫這樣一個Java文件
?
package com.xdocletbook.blog.servlet;
?
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
?
/**
* Simple class to test Jakarta Struts generation (Jakarta Struts 1.2 beta 2 only).
*
* @struts.action
*path="/struts/foo"
*name="userForm"
*input="jsp/createBlog.jsp"
*
* @struts.action-forward
*name="success"
*path="/struts/getAll.do"
*redirect="false"
*
*
* @struts.action-exception
* type="com.xdocletbook.blog.exception.ApplicationException"
* key="app.exception"
* path="jsp/error.jsp"
*
* @struts.action-form name="blog.Create"
*/
public final class StrutsAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
return mapping.findForward("success");
}
}
?
關鍵部分就是注釋部分。
?
看我們增加build.xml一個任務,
<target name="generate-web">
<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask" classpathref="xdoclet.classpath"/>
<!-- Generate servlet and JSP Tag "stuff" -->
<webdoclet destdir="gen" mergeDir="merge">
<fileset dir="src">
<include name="**/*.java" />
</fileset>
<strutsconfigxml version="1.1"/>
</webdoclet>
</target>
?
運行ant generate-web,我們就在gen得到了struts-config.xml
其中關鍵內容如下
<action
path="/struts/foo"
type="com.xdocletbook.blog.servlet.StrutsAction"
name="userForm"
?
scope="request"
input="jsp/createBlog.jsp"
unknown="false"
validate="true"
>
<exception
?
key="app.exception"
type="com.xdocletbook.blog.exception.ApplicationException"
path="jsp/error.jsp"
/>
?
?
<forward
name="success"
path="/struts/getAll.do"
redirect="false"
/>
</action>
?
如果我們有許多Action,就可以隨時生成這樣一個文件,不必在意有人改過這個文件。同時你也不必擔心不小心忘了改這個文件,因為你改了
?
Java時,許多默認的屬性也跟這改了。
看到這里,許多用過workshop的一定感覺到頁面流不就是這樣嗎?當通過圖形界面定義流程時,看看頁面流的源碼你就會發現,注釋中有一些
?
特殊的標記,這說明workshop的注釋有著xdoclet一樣的功能,只不過workshop提供了很好的界面,而不需要自己寫注釋,而且workshop提供了
?
更好的語法檢查,呵呵,只是將許多action寫到一起,是有些亂。
?
Struts的另一個主要的部分就是Form了,雖然我一開始覺得Form有些麻煩,對付動態的Form有些無能為力,但是結合一些相關的插件后,效果確實不錯。
?
?
這是我們的Form文件,我們還使用ValidatorForm來做自動驗證。
?
package com.xdocletbook.blog.servlet;
?
import java.io.Serializable;
import org.apache.struts.validator.ValidatorForm;
?
/**
*
* @struts.form
*name="blog.Create"
*/
public class BlogCreateForm
extends ValidatorForm
implements Serializable {
private String name;
private String owner;
private String email;
public BlogCreateForm() {}
?
public String getName() {
return this.name;
}
/**
* @struts.validator
* type="required"
*/
?
public void setName(String name) {
this.name = name;
}
?
?
public String getOwner() {
return this.owner;
}
/**
* @struts.validator
* type="required"
*/
?
public void setOwner(String owner) {
this.owner = owner;
}
?
public String getEmail() {
return this.email;
}
/**
*@struts.validator
*type="required"
*@struts.validator
*type="email"
*/
?
public void setEmail(String email) {
this.email = email;
}
}
?
?
然后運行ant generate-web
?
這樣struts-config.xml就有了
<form-beans>
<form-bean
name="blog.Create"
type="com.xdocletbook.blog.servlet.BlogCreateForm"
/>
?
<!
?
--
If you have non XDoclet forms, define them in a file called struts-forms.xml and
place it in your merge
?
directory.
-->
</form-beans>
?
這里有一個陷阱,就是Struts XDoclet處理form-beans時,只處理類型是Form的,對于類型是ValidatorForm的Form如果你不把對應的類文件放
?
到classpath下,XDoclet就會忽略它,所以struts的包一定要放到類路徑下,讓XDoclet有機會知道ValidatorForm是Form的子類。
?
還有每一個setXXX方法,有一些表示限制的注釋,這些幫助我們生成了
?
validation.xml
?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons
?
Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
?
<form-validation>
<!--
?
?
Define global validation config in validation-global.xml
-->
<formset>
<form name="blog.Create">
<field property="name" depends="required">
<arg0 key="blog.Create.name"/>
</field>
<field property="owner" depends="required">
<arg0 key="blog.Create.owner"/>
</field>
<field property="email" depends="required,email">
<arg0 key="blog.Create.email"/>
</field>
</form>
?
</formset>
</form-validation>
Ant+checkstyle 實現代碼檢查
1 checkstyle簡介
checkstyle是一個幫助開發者按照某種習慣編寫java代碼的工具,他實現了代碼檢查的自動化,幫助人們從這種繁瑣的工作中解放出來。
默認提供了對sun編程規范的支持,但是checkstyle是一個具有高可配置性的,你完全可以根據自己的要求來配置需要檢查的內容。
2 工具下載、安裝
2.1 Ant下載
最新版本1.6.1,下載地址:http://ant.apache.org/
解壓縮到c:/ant1.6.1,后面將引用為%ant_home%
2.2 cheskstyle下載
最新版本3.3,下載地址:http://checkstyle.sourceforge.net/
解壓縮到c:/checkstyle3.3,后面將引用為%checkstyle_home%
?
3 簡單配置
3.1 環境變量:
set path=%ant_home%/bin
set classpath=%checkstyle_home%/checkstyle-all-3.3.jar
3.2 build.xml文件
<taskdef resource="checkstyletask.properties"
?????????? classpath="%checkstyle_home%\checkstyle-all-3.3.jar"/>
<target name="checkstyle">
<target name="checkstyle">
<checkstyle config="sun_checks.xml" classpath="%checkstyle_home%\checkstyle-all-3.3.jar">
<fileset dir="build/classes" includes="**/*.java"/>
</checkstyle>
</target>
屬性說明:
config? 要使用的格式配置文件
classpath 要用到的jar文件
fileset??? 需要檢查的文件集合
4 說明
需要檢查的內容是基于module配置的,所以如果你不要檢查那些module,你可以將他去掉,下面是默認的sun代碼規范檢查的配置文件:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
??? "-//Puppy Crawl//DTD Check Configuration 1.1//EN"
??? "http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
?
<!--
?
? Checkstyle configuration that checks the sun coding conventions from:
?
??? - the Java Language Specification at
????? http://java.sun.com/docs/books/jls/second_edition/html/index.html
?
??? - the Sun Code Conventions at http://java.sun.com/docs/codeconv/
?
??? - the Javadoc guidelines at
????? http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
?
??? - the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html
?
??? - some best practices
?
? Checkstyle is very configurable. Be sure to read the documentation at
? http://checkstyle.sf.net (or in your downloaded distribution).
?
? Most Checks are configurable, be sure to consult the documentation.
?
? To completely disable a check, just comment it out or delete it from the file.
?
? Finally, it is worth reading the documentation.
?
-->
?
<module name="Checker">
?
??? <!-- Checks that a package.html file exists for each package.???? -->
??? <!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
??? <module name="PackageHtml"/>
?
??? <!-- Checks whether files end with a new line.??????????????????????? -->
??? <!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
??? <module name="NewlineAtEndOfFile"/>
?
??? <!-- Checks that property files contain the same keys.???????? -->
??? <!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
??? <module name="Translation"/>
?
?
??? <module name="TreeWalker">
?
??????? <!-- Checks for Javadoc comments.???????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_javadoc.html -->
??????? <module name="JavadocMethod"/>
??????? <module name="JavadocType"/>
??????? <module name="JavadocVariable"/>
??????? <module name="JavadocStyle"/>
?
?
??????? <!-- Checks for Naming Conventions.????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_naming.html -->
??????? <module name="ConstantName"/>
??????? <module name="LocalFinalVariableName"/>
??????? <module name="LocalVariableName"/>
??????? <module name="MemberName"/>
??????? <module name="MethodName"/>
??????? <module name="PackageName"/>
??????? <module name="ParameterName"/>
??????? <module name="StaticVariableName"/>
??????? <module name="TypeName"/>
?
?
??????? <!-- Checks for Headers??????????????????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_header.html?? -->
??????? <!-- <module name="Header">??????????????????????????? -->
??????????? <!-- The follow property value demonstrates the ability???? -->
??????????? <!-- to have access to ANT properties. In this case it uses -->
??????????? <!-- the
{basedir} property to allow Checkstyle to be run? -->
??????????? <!-- from any directory within a project. See property????? -->
??????????? <!-- expansion,???????????????????????????????????????????? -->
??????????? <!-- http://checkstyle.sf.net/config.html#properties??????? -->
??????????? <!-- <property????????????????????????????????????????????? -->
??????????? <!--???? name="headerFile"????????????????????????????????? -->
??????????? <!--???? value="
{basedir}/java.header"/>?????????????????? -->
??????? <!-- </module> -->
?
??????? <!-- Following interprets the header file as regular expressions. -->
??????? <!-- <module name="RegexpHeader"/>??????????????????????????????? -->
?
?
??????? <!-- Checks for imports????????????????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_import.html -->
??????? <module name="AvoidStarImport"/>
??????? <module name="IllegalImport"/> <!-- defaults to sun.* packages -->
??????? <module name="RedundantImport"/>
??????? <module name="UnusedImports"/>
?
?
??????? <!-- Checks for Size Violations.??????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_sizes.html -->
??????? <module name="FileLength"/>
??????? <module name="LineLength"/>
??????? <module name="MethodLength"/>
??????? <module name="ParameterNumber"/>
?
?
??????? <!-- Checks for whitespace?????????????????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_whitespace.html -->
??????? <module name="EmptyForIteratorPad"/>
??????? <module name="NoWhitespaceAfter"/>
??????? <module name="NoWhitespaceBefore"/>
??????? <module name="OperatorWrap"/>
??????? <module name="ParenPad"/>
??????? <module name="TypecastParenPad"/>
??????? <module name="TabCharacter"/>
??????? <module name="WhitespaceAfter"/>
??????? <module name="WhitespaceAround"/>
?
?
??????? <!-- Modifier Checks??????????????????????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_modifiers.html -->
??????? <module name="ModifierOrder"/>
??????? <module name="RedundantModifier"/>
?
?
??????? <!-- Checks for blocks. You know, those {}'s???????? -->
??????? <!-- See http://checkstyle.sf.net/config_blocks.html -->
??????? <module name="AvoidNestedBlocks"/>
??????? <module name="EmptyBlock"/>
??????? <module name="LeftCurly"/>
??????? <module name="NeedBraces"/>
??????? <module name="RightCurly"/>
?
?
??????? <!-- Checks for common coding problems?????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_coding.html -->
??????? <module name="AvoidInlineConditionals"/>
??????? <module name="DoubleCheckedLocking"/>??? <!-- MY FAVOURITE -->
??????? <module name="EmptyStatement"/>
??????? <module name="EqualsHashCode"/>
??????? <module name="HiddenField"/>
??????? <module name="IllegalInstantiation"/>
??????? <module name="InnerAssignment"/>
??????? <module name="MagicNumber"/>
??????? <module name="MissingSwitchDefault"/>
??????? <module name="RedundantThrows"/>
??????? <module name="SimplifyBooleanExpression"/>
??????? <module name="SimplifyBooleanReturn"/>
?
??????? <!-- Checks for class design???????????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_design.html -->
??????? <module name="DesignForExtension"/>
??????? <module name="FinalClass"/>
??????? <module name="HideUtilityClassConstructor"/>
??????? <module name="InterfaceIsType"/>
??????? <module name="VisibilityModifier"/>
?
?
??????? <!-- Miscellaneous other checks.?????????????????? -->
??????? <!-- See http://checkstyle.sf.net/config_misc.html -->
??????? <module name="ArrayTypeStyle"/>
??????? <module name="FinalParameters"/>
??????? <module name="GenericIllegalRegexp">
??????????? <property name="format" value="\s+
"/>
??????????? <property name="message" value="Line has trailing spaces."/>
??????? </module>
??????? <module name="TodoComment"/>
??????? <module name="UpperEll"/>
??? </module>
?
</module>
5 如何自己配置
可以看到,xml里面的內容都是類似<module name="PackageHtml"/>這樣的,這表示檢查這部分的規范,置于具體的規范內容,每個module的前面都有連接地址,大家可以去看。
以<module name="PackageHtml"/>為例:
他的要求是說,每個包下面都要有個package.html文件,如果你不想檢查這一項,你可以將這個元素從xml內容中刪除。
?
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1311665