最早以前沒在用管理工具,都是直接複製jar檔就直接開始用了,也沒覺得哪裡不好,
後來開始用Spring了,套件數就急速增加,也還在可控制範圍,
但是在後來公司需求加上CXF,套件數已經達到了我不想管的地步了XD(而且還有衝突)
後來開始想找工具來管這麻煩的事,原先練習了Maven,但是覺得不是很好使用,
後來又找到這Gradle工具,就決定試用看看
專案大多在eclipse上執行所以還是拿eclipse來安裝,
先開啟Eclipse Marketplace
搜尋Gradle
因為我是用luna版所以安裝Gradle Integration for Eclipse(4.4)
一路確認到安裝完成會重新啟動Eclipse,再來新增一個Dynamic Web Project,在專案根目錄下手動新增一個build.gradle檔案
import org.gradle.plugins.ide.eclipse.model.Facet
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.7 // 設置 JDK 版本
webAppDirName = 'WebContent' // 設置 WebApp 根目錄
sourceSets.main.java.srcDir 'src' // 設置 Java 源碼所在目錄
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
// 設置 maven 庫地址
repositories {
mavenCentral() // 中央庫
// maven { url 'http://maven.oschina.net/content/groups/public/' } // 自定義庫地址
}
// 設置依賴
dependencies {
def httpclientVersion = '4.4.1'
providedCompile 'javax.servlet:javax.servlet-api:3.0.1' // 編譯期
providedRuntime 'javax.servlet:jstl:1.2' // 運行時
compile 'org.apache.httpcomponents:httpclient:${httpclientVersion}',
'org.apache.httpcomponents:httpcore:${httpclientVersion}'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
// 設置 Project Facets
eclipse {
wtp {
facet {
facet name: 'jst.web', type: Facet.FacetType.fixed
facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
facet name: 'jst.java', type: Facet.FacetType.fixed
facet name: 'jst.web', version: '3.0'
facet name: 'jst.java', version: '1.7'
facet name: 'wst.jsdt.web', version: '1.0'
}
}
}
基本配置完成後就先將專案轉為Gradle專案
再執行Refresh All,這動作會讓Gradle去檢查設定檔與下載相關套件
接下來就去把自己習慣用的套件編號都找出來加進去
dependencies {
providedCompile 'javax.servlet:javax.servlet-api:3.0.1' // 編譯期
providedRuntime 'javax.servlet:jstl:1.2' // 運行時
testCompile group: 'junit', name: 'junit', version: '4.+'
//apache共用
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'org.apache.httpcomponents:httpclient:4.3.6'
compile 'org.apache.httpcomponents:httpcore:4.3.3'
compile 'org.apache.httpcomponents:httpcore-nio:4.3.3'
//CXF
compile 'org.apache.cxf:cxf-rt-core:2.7.13'
compile 'org.apache.cxf:cxf-api:2.7.13'
compile 'org.apache.cxf:cxf-bundle:2.7.13'
//Spring
compile 'org.springframework:spring-aop:3.2.12.RELEASE'
compile 'org.springframework:spring-beans:3.2.12.RELEASE'
compile 'org.springframework:spring-context:3.2.12.RELEASE'
compile 'org.springframework:spring-context-support:3.2.12.RELEASE'
compile 'org.springframework:spring-core:3.2.12.RELEASE'
compile 'org.springframework:spring-expression:3.2.12.RELEASE'
compile 'org.springframework:spring-jdbc:3.2.12.RELEASE'
compile 'org.springframework:spring-tx:3.2.12.RELEASE'
compile 'org.springframework:spring-web:3.2.12.RELEASE'
compile 'org.springframework:spring-webmvc:3.2.12.RELEASE'
//資料庫用
compile 'org.mybatis:mybatis:3.2.8'
compile 'org.mybatis:mybatis-spring:1.2.2'
//log類
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'ch.qos.logback:logback-core:1.1.2'
compile 'ch.qos.logback:logback-access:1.1.2'
compile 'ch.qos.logback:logback-classic:1.1.2'
//工具類
compile 'org.bouncycastle:bcprov-jdk16:1.46'
compile 'com.alibaba:fastjson:1.2.1'
}
一樣Refresh All後,你可以透過介面執行Gradle Task
秀出所有可執行的任務
task --all
秀出套件相依性
dependencies
下完指令按下Enter就會執行並顯示在Console
這邊看一下是如何來幫我們管理套件
其實應該不難看出他的表達方式,第二層就是第一層需要的套件,並會一併下載回來
但是套件一用多就是痛苦的開始(衝突不斷),來看看Gradle如何幫忙解決
從上圖你可以看出來org.apache.cxf:cxf-bundle:2.7.13相依這麼多東西,你專案那一大堆jar檔是這麼來的
再來你可以看commons-lang:commons-lang:2.4 -> 2.6,因為這在cxf-bundle原本定義相依版本是2.4,但是我有指定版本了,所以Gradle做了幫我升級的動作
再更深層的org.springframework:spring-aop:3.0.7.RELEASE -> 3.2.12.RELEASE他也幫我比對出來並做出一併升級的動作
來看看專案內實際的jar檔
只出現我指定的org.springframework:spring-aop:3.2.12.RELEASE,不會出現CXF相依的版本3.0.7.RELEASE,而且這過程是自動的,真是深得我心(Maven好像要自己排除)
再來完成後只需要執行war任務就可以完成打包War檔(因為設定檔內有含apply plugin: 'war'的插件)
war檔會在mservice\build\libs下面
後面就跟一般專案差不多啦
參考
Gradle用戶手冊(英文)
Gradle 用户手册(半中文)
http://www.codedata.com.tw/java/understanding-gradle-7-java-project-dependencies/
Gradle命令行黑魔法