前言 ant是java開發(fā)者工具箱的重要一環(huán),junit,xdoclet等都與它緊密關(guān)聯(lián),程序員可能習(xí)慣了IDE提供的自動構(gòu)建,甚至部署的功能,從而忽略了ant本身,其實,主流的IDE通常是內(nèi)置ant任務(wù)來完成這些工作的,熟悉ant內(nèi)在的機理,可以閱讀或簡單修改build.xml無疑可以幫助你更靈活地集成、管理應(yīng)用項目,如果需要學(xué)習(xí)maven這種開源項目管理解決方案,也是要以理解ant為基礎(chǔ)的喲。另外,使用ant的過程實際上對構(gòu)建進行了文檔化,它是無關(guān)于IDE的,想象一下,你的同事中可能三分之一在用JbuilderX,三分之一用eclipse,還有一些是別的。 本人使用eclipse3.0.1,以前的構(gòu)建和發(fā)布工作都由myeclipse插件作了,趁周末實踐了一下手動構(gòu)建,記此備忘。 實踐 準(zhǔn)備工作:這是我的個人習(xí)慣,把所有公用的類庫jar置于一個固定目錄,分好類,不要丟在一個文件夾下,如jakarta-commons、hibernate、spring、struts等,這些是源碼構(gòu)建時需要用到的,在部署時可能有一些不用再打進去了,比如servlet.jar。如果你們有自己的framework,也一并放在這里。然后,打開eclipse,進入Windows->Preferences->Java->User Libraries,增加一個自己的庫,比如說mylib,把剛才那些公共的jar全部添入,這樣有個好處,在eclipse項目中,不用再看到煩人的長長的jar列表了,比較整潔。 下來正式進行: 1.新建一個Java Project,此時就不要再選你的j2ee插件內(nèi)置的一些選項了,至簡即可。 2.在root下建幾個文件夾,我們在網(wǎng)上下載的開源項目中經(jīng)??梢钥吹竭@些,比如: src - 源碼 classes - 編譯 web - jsp等 lib - 庫,這里可以簡單地把mylib下的東東copy過來,便于將來發(fā)布源碼。 dlist - 輸出的jar或war 當(dāng)然,我們要建一個build.xml,eclipse中會出現(xiàn)一個螞蟻的小圖標(biāo),一般這個文件建立后,下一個項目簡單的copy過去,稍加改動就可以了。 3.打開項目的屬性頁,在Java Build Path的庫選項中,加入我們自定義的公共庫mylib.至于Builders方式就不用改了,使用默認的Java Builer即可,我只是項目部署時使用ant,平常的排錯工作就交給IDE吧。 4.重中之重,寫你的build.xml,網(wǎng)上文章很海,我這里就不再?嗦了,基本上就分那幾個任務(wù): 4.1 先要聲明一些路徑變量,如 ?。紁roperty name="war.dir" value="dlist" /> 也可以將其寫至properties文件中,在這里引用; 4.2 聲明編譯的類路徑,如下: ?。紁ath id="master-classpath"> <fileset dir="${lib.root}/struts"> ?。糹nclude name="struts-menu-2.3.jar" /> ?。糹nclude name="struts.jar" /> </fileset> ?。糵ileset dir="${lib.root}/jakarta-commons"> ?。糹nclude name="commons-*.jar" /> ?。?fileset> ?。糵ileset dir="${lib.root}/ibatis2.0.9"> <include name="ibatis-*.jar" /> ?。?fileset> ?。糵ileset dir="${lib.root}/jdbcdriver"> <include name="jtds-0.9-rc2.jar" /> ?。?fileset>s ...... ?。?path> 4.3 清空輸出目錄,如web,dlist等。 4.4 編譯構(gòu)建: ?。紅arget name="build" description="Compile main source tree java files into class files, generate jar files"> <mkdir dir="${build.dir}" /> ?。糺avac destdir="${build.dir}" source="1.3" target="1.3" debug="true" deprecation="false" optimize="false" failonerror="true"> <src path="${src.dir}" /> ?。糲lasspath refid="master-classpath" /> </javac> ?。糲opy todir="${build.dir}" preservelastmodified="true"> ?。糵ileset dir="${src.dir}"> <include name="**/*.xml" /> ?。糹nclude name="**/*.properties" /> ?。?fileset> </copy> ?。?-- ============================================= --> <!-- 據(jù)測試,資源文件不能被打到j(luò)ar文件中,其余均可 --> ?。?-- ============================================= --> <copy todir="${webclasses.dir}/conf" preservelastmodified="true"> ?。糵ileset dir="${src.dir}/conf"> <include name="springResources*.properties" /> ?。?fileset> ?。?copy> ?。糾kdir dir="${weblib.dir}" /> <jar jarfile="${weblib.dir}/${name}.jar" compress="true"> ?。糵ileset dir="${build.dir}"> <include name="**" /> ?。?fileset> </jar> ?。糲opy todir="${weblib.dir}" preservelastmodified="true"> ?。糵ileset dir="${lib.root}"> ?。糹nclude name="log4j-1.2.8.jar" /> ?。?fileset> <fileset dir="${lib.root}/struts"> ?。糹nclude name="struts-menu-2.3.jar" /> <include name="struts.jar" /> ?。?fileset> ?。糵ileset dir="${lib.root}/jakarta-commons"> <include name="commons-*.jar" /> ?。?fileset> <fileset dir="${lib.root}/spring-1.1.3"> ?。糹nclude name="spring.jar" /> ?。糹nclude name="aopalliance.jar" /> ?。?fileset> ...... ?。?copy> </target> ?。?-- ============================================= --> <!-- Compile main Java sources and copy libraries --> ?。?-- ============================================= --> ?。紅arget name="warfile" description="Build the web application archive"> <mkdir dir="${dist.dir}" /> ?。紈ar warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml"> ?。糹nclude name="*" /> <include name="WEB-INF/*.*" /> ?。糴xclude name="WEB-INF/web.xml" /> ?。糹nclude name="WEB-INF/classes/*.*" /> ?。糹nclude name="WEB-INF/lib/**" /> ?。糴xclude name="**/.*" /> ?。?war> </target> 4.5 打成war ?。紅arget name="warfile" description="Build the web application archive"> ?。糾kdir dir="${dist.dir}" /> <war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml"> ?。糹nclude name="*" /> ?。糹nclude name="WEB-INF/*.*" /> ?。糴xclude name="WEB-INF/web.xml" /> ?。糹nclude name="WEB-INF/classes/*.*" /> ?。糹nclude name="WEB-INF/lib/**" /> ?。糴xclude name="**/.*" /> ?。?war> </target> 4.6 把幾個任務(wù)串起來,弄一個default target ?。紅arget name="all"> ?。糰ntcall target="clean" /> <antcall target="build" /> ?。糰ntcall target="warfile" /> </target> 打完收功。在實踐中發(fā)現(xiàn),一些配置文件,如struts-config.xml ibatis和spring的xml都可以打進jar文件,spring資源文件好象不行,得單獨copy至WEB-INFclasses下,另外,你的web文件夾下,事先得放好web.xml,以及一些tld文件喲! |
|