java 板


主要內容 1. 常用task(這是很主觀的xd) :: mkdir :: copy :: delete :: move :: javac :: echo :: java 2. 執行class 3. target相依性 行前講習 * 在一些稍具模規的project大致上都會把source directory 和output directory分開,output directory也許是lib也許是bin 沒有一定的。要達到這些目的,在ant中提供了幾個task能幫得上忙。 (copy. move. delete. mkdir) * 一個project可能有多許`組`程式,你可以為這些程式分別撰寫個自的target 但是,有些target必需限制在某些target之後執行。這問題就需要利用 target的相依屬性(depends)來設定了:) 重新規劃nice project project basedir / src/ bin/ build.xml 我們重新規劃project如上述目錄結構:)所以,我們這回把*.java放在src 之中。產生出的*.class則放在bin之中。但我並不打算自己建立bin dir build.xml (原先的) ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ ls build.xml src slayer@ubuntu:~/test/anttest$ cat build.xml <?xml version="1.0" encoding="utf-8" ?> <project name="nice" basedir="."> <property name="src.dir" value="."/> <target name="i-need-kitty" description="just get me a hello kitty"> <echo>Kitty Kitty</echo> <javac srcdir="${src.dir}" /> </target> </project> build.xml (指定src) :: 如之前介紹的,更改src.dir變數內容即可:) ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ cat build.xml <?xml version="1.0" encoding="utf-8" ?> <project name="nice" basedir="."> <property name="src.dir" value="src"/> <target name="i-need-kitty" description="just get me a hello kitty"> <echo>Kitty Kitty</echo> <javac srcdir="${src.dir}" /> </target> </project> slayer@ubuntu:~/test/anttest$ ant i-need-kitty Buildfile: build.xml i-need-kitty: [echo] Kitty Kitty BUILD SUCCESSFUL Total time: 1 second slayer@ubuntu:~/test/anttest$ ls -l src/ 總計 8 -rw-r--r-- 1 slayer slayer 340 2006-04-08 13:02 HelloKitty.class -rw-r--r-- 1 slayer slayer 108 2006-04-08 08:26 HelloKitty.java slayer@ubuntu:~/test/anttest$ =================================================================== 結果發現,編譯好的class檔跑到src內了。所以我們還需要再修改一下 build.xml來符合我們所期待的結果。為了達成我們的目的,有幾種改變的方式 你可以在i-need-kitty的target中,直接使用mkdir等相關的task。 或是把建立bin directory獨立為一個target。 build.xml :: 採用在i-need-kitty加上move的動作 http://ant.apache.org/manual/CoreTasks/move.html ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ cat build.xml <?xml version="1.0" encoding="utf-8" ?> <project name="nice" basedir="."> <property name="src.dir" value="src"/> <property name="bin.dir" value="bin"/> <target name="i-need-kitty" description="just get me a hello kitty"> <echo>Kitty Kitty</echo> <javac srcdir="${src.dir}" /> <move todir="${bin.dir}"> <fileset dir="${src.dir}"> <include name="*.class" /> </fileset> </move> </target> </project> 執行i-need-kitty ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ ant i-need-kitty Buildfile: build.xml i-need-kitty: [echo] Kitty Kitty [javac] Compiling 1 source file [move] Moving 1 file to /home/slayer/test/anttest/bin BUILD SUCCESSFUL Total time: 2 seconds 觀察結果,確實是我們期待的那樣:) ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ ls -l * -rw-r--r-- 1 slayer slayer 585 2006-04-08 13:18 build.xml bin: 總計 4 -rw-r--r-- 1 slayer slayer 340 2006-04-08 13:19 HelloKitty.class src: 總計 4 -rw-r--r-- 1 slayer slayer 108 2006-04-08 08:26 HelloKitty.java ====================================================================== 細心的人一定發現了。我們並沒有mkdir:) 這大概算move的特異功能xd 沒有先mkdir出來。它就幫你自動mkdir了。 接著那編好了之後,能不能執行來跑看看呢? 這當然也是一個ez job 首先,先讓我招喚一下error message:) build.xml :: 含java HelloKitty ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ cat build.xml <?xml version="1.0" encoding="utf-8" ?> <project name="nice" basedir="."> <property name="src.dir" value="src"/> <property name="bin.dir" value="bin"/> <target name="i-need-kitty" description="just get me a hello kitty"> <echo>Kitty Kitty</echo> <javac srcdir="${src.dir}" /> <move todir="${bin.dir}"> <fileset dir="${src.dir}"> <include name="*.class" /> </fileset> </move> </target> <target name="kitty"> <java classname="HelloKitty" /> </target> </project> slayer@ubuntu:~/test/anttest$ ant kitty Buildfile: build.xml kitty: [java] Could not find HelloKitty. Make sure you have it in your classpath [java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:170) [java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:710) [java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178) [java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84) [java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275) [java] at org.apache.tools.ant.Task.perform(Task.java:364) [java] at org.apache.tools.ant.Target.execute(Target.java:341) [java] at org.apache.tools.ant.Target.performTasks(Target.java:369) [java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216) [java] at org.apache.tools.ant.Project.executeTarget(Project.java:1185) [java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40) [java] at org.apache.tools.ant.Project.executeTargets(Project.java:1068) [java] at org.apache.tools.ant.Main.runBuild(Main.java:668) [java] at org.apache.tools.ant.Main.startAnt(Main.java:187) [java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246) [java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67) BUILD SUCCESSFUL Total time: 0 seconds ====================================================================== 別害怕錯誤訊息太多。通常看第一個發生的錯誤就是了。如果不確定, 就找與你自己的程式有關的部分來看就好。其他package產生的錯誤只是連帶而來:) <% Could not find HelloKitty. Make sure you have it in your classpath %> 所以,直覺就是沒有為java屬性加上classpath: <% <java classname="HelloKitty" classpath="${bin.dir}" /> %> 再次執行: ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ ant kitty Buildfile: build.xml kitty: [java] Get Hello Kitty atSat Apr 08 13:45:39 CST 2006 BUILD SUCCESSFUL Total time: 0 seconds ====================================================================== 雖然,到目前為止看起來是正常的。但是我們忽略了執行順序的問題。如果沒有 先執行i-need-kitty就會發生問題。同樣地,我們砍除bin來招喚出error message 來欣賞一下^^ <% slayer@ubuntu:~/test/anttest$ rm -rf bin slayer@ubuntu:~/test/anttest$ ant kitty Buildfile: build.xml kitty: [java] Could not find HelloKitty. Make sure you have it in your classpath ................................................後面同上:) %> 你會發現這和沒有寫classpath的error message是相同的。 所以,當發生這種error message時,您必需檢查這二種情況:) 言歸正傳,我們還是要用target提供的depends屬性來做簡單的處理: <% <target name="kitty" depends="i-need-kitty"> %> 再次執行 ---------------------------------------------------------------------- slayer@ubuntu:~/test/anttest$ ant kitty Buildfile: build.xml i-need-kitty: [echo] Kitty Kitty [javac] Compiling 1 source file [move] Moving 1 file to /home/slayer/test/anttest/bin kitty: [java] Get Hello Kitty at Sat Apr 08 13:53:17 CST 2006 BUILD SUCCESSFUL Total time: 1 second ====================================================================== 大致上,ant基本就是這些功能嚕:) 看倌可以自行實作2個target 1. usage: 用echo顯示訊息 2. clean: 用delete刪除bin PS. 本篇將相依、classpath的問題過於簡化:) 有興趣的朋友可以上網找相關文章 :) -- 夫兵者不祥之器物或惡之故有道者不處君子居則貴左用兵則貴右兵者不祥之器非君子 之器不得已而用之恬淡為上勝而不美而美之者是樂殺人夫樂殺人者則不可得志於天下 矣吉事尚左凶事尚右偏將軍居左上將軍居右言以喪禮處之殺人之眾以哀悲泣之戰勝以 喪禮處之道常無名樸雖小天下莫能臣侯王若能守之萬物將自賓天地相合以降甘露民莫 之令而自均始制有名名亦既有夫亦將知止知止可以 pc210-59-94-148.nutn.edu.tw 作者在 06/04/08 12:39:21 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:29:04 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:32:22 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:46:48 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:56:49 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:58:50 pc210-59-94-148.nutn.edu.tw 修改這篇文章 作者在 06/04/08 13:59:09 pc210-59-94-148.nutn.edu.tw 修改這篇文章







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草
伺服器連線錯誤,造成您的不便還請多多包涵!
「贊助商連結」






like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Tech_Job站內搜尋

TOP