差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

Both sides previous revision 前次修改
下次修改
前次修改
java:gradle:helloworld [2014/12/23 00:05]
tony [建立Source Folder]
java:gradle:helloworld [2023/06/25 09:48] (目前版本)
行 17: 行 17:
 build.gradle即build script,直接在專案目錄下建立此檔案即可。 build.gradle即build script,直接在專案目錄下建立此檔案即可。
 ==== 建立Source Folder ==== ==== 建立Source Folder ====
-由於我是java且要用於eclipse的專案,所以我在腳本中,引用了java與eclipse兩個plugin,而sourceSets分別宣告了src、test與tools三個。+由於我是java且要用於eclipse的專案,所以我在腳本中,引用了java與eclipse兩個pluginsourceSets分別宣告了src、test與tools三個資料夾,如果專案是按照慣例方式建立,並不需要特別去設立sourceSets
 <code bash> <code bash>
 apply plugin: '​java' ​ apply plugin: '​java' ​
行 40: 行 40:
 } }
 </​code>​ </​code>​
-如果專案是按照慣例方式建立資料夾,並不需要特別去設立sourceSets+在加入以上腳本後,對專案 ​點擊右鍵 > Gradle > Refresh Source Folders:​\\ 
 +{{:​java:​gradle:​gradle-refresh.png?​500|}}\\ 
 +完成後會將設定的資料夾設為Source Folders但因為沒加入任何相依的jar檔,應會出現編譯錯誤。 
 +==== 加入相依函式庫 ==== 
 +Gradle可以透過我們所定義的reposiotry尋找我們所相依的jar檔,這裡我直接使用Maven的Central Repository。而相依定義的部分,我使用了compile、runtime與testCompile, 
 +  * compile: 編譯production source所使用的函示庫。 
 +  * runtime: 執行期間所使用的函示庫。 
 +  * testCompile:​ 編譯test source所使用的函示庫。 
 +以下是我的定,供給大家參考:​ 
 +<code bash> 
 +repositories { 
 +    mavenCentral() 
 +
 +dependencies { 
 +    compile '​log4j:​log4j:​1.2.16'​ 
 +    compile '​org.slf4j:​slf4j-api:​1.7+'​ 
 +    compile '​net.java.dev.jna:​jna:​4+'​ 
 +    compile '​net.java.dev.jna:​platform:​3+' ​    
 +    compile '​javax.ws.rs:​javax.ws.rs-api:​2+'​ 
 +    compile '​org.glassfish.jersey.core:​jersey-client:​2.14'​ 
 +    compile '​org.glassfish.jersey.media:​jersey-media-json-jackson:​2.14'​  
 +     
 +    runtime '​org.ow2.asm:​asm:​5+'​ 
 +    runtime '​org.apache.commons:​commons-lang3:​3+'​ 
 +    runtime '​commons-logging:​commons-logging:​1.1.1'​ 
 +     
 +    testCompile '​org.powermock:​powermock-core:​1.6+'​ 
 +    testCompile '​org.powermock:​powermock-api-easymock:​1.6+'​ 
 +    testCompile '​org.powermock:​powermock-mockito-release-full:​1.6+'​ 
 +    testCompile '​org.easymock:​easymock:​3.3'​ 
 +    testCompile '​junit:​junit:​4.11'​ 
 +
 +</​code>​ 
 +完成後,對專案 點擊右鍵 > Gradle > Refresh Dependencies,即可看到程式碼通通編過了\\ 
 +{{:​java:​gradle:​gradle-project-done-tonylin.png?​400|}} 
 ===== 總結 ===== ===== 總結 =====
 +對於第一個專案,我要求先做到能夠在Eclipse上開發。在本文章學習到:​
 +  * 轉換既有專案為Gradle專案。
 +  * 設定Source資料夾。
 +  * 設定相依函式庫。
 +在下篇文章中,我會挑相依於此專案的專案來做修改。
  
 +===== 補充 =====
 +==== Source與Libraries刷新 ====
 +我們可以在Preference中,找到Gradle項目裡的Enale automatic refresh,去啟動自動刷新。\\
 +{{:​java:​gradle:​gradle-refresh-auto.png?​500|}}
 +==== Convension Over Configuration ====
 +在Gradle中,可以透過少少的設定就能做到大部分的事情。舉例來說,sourceSets是可以不必要的,前提是專案程式碼配置是依照maven"​慣例"​,如src/​main/​java或src/​test/​java。
 +===== Reference =====
 +  * [[http://​en.wikipedia.org/​wiki/​Convention_over_configuration|Convention_over_configuration]]
 +  * [[http://​www.gradle.org/​docs/​current/​userguide/​userguide.html|Gradle User Guide]]
 =====    ===== =====    =====
 ---- ----
 \\ \\
 ~~DISQUS~~ ~~DISQUS~~