Gradle 以 Groovy/Kotlin DSL 取代冗長 XML,配合 Eclipse Buildship 插件,讓 Java Web 專案的依賴管理、多模組構建、IDE 整合變得優雅高效。本文記錄完整搭建流程。
GRADLE_HOME、PATHbuild.gradle、settings.gradle右鍵專案 → Properties → Project Facets → 勾選 Dynamic Web Module (版本 3.1+),自動生成 web.xml、WEB-INF 目錄。
build.gradle 依賴管理plugins { id 'java' id 'war' } // war 插件支援 Web 打包
repositories { jcenter() } // 包含 mavenCentral,速度更快
dependencies {
// Struts2
implementation 'org.apache.struts:struts2-core:2.3.24.1'
implementation 'org.apache.struts:struts2-spring-plugin:2.3.24.1'
implementation 'org.apache.struts:struts2-json-plugin:2.3.24.1'
// Spring 4.2
implementation 'org.springframework:spring-core:4.2.3.RELEASE'
implementation 'org.springframework:spring-web:4.2.3.RELEASE'
implementation 'org.springframework:spring-orm:4.2.3.RELEASE'
implementation 'org.springframework:spring-aop:4.2.3.RELEASE'
implementation 'org.springframework:spring-aspects:4.2.3.RELEASE'
implementation 'org.springframework:spring-jdbc:4.2.3.RELEASE'
// Hibernate 5
implementation 'org.hibernate:hibernate-core:5.0.1.Final'
// Common
implementation 'org.slf4j:slf4j-api:1.7.13'
testImplementation 'junit:junit:4.12'
}
Eclipse 右側 Gradle Tasks 視圖 → build → 雙擊 compileJava 或點擊 Refresh Gradle Project,Gradle 自動解析依賴圖、下載 JAR 到本地倉庫 (~/.gradle/caches),並更新 Eclipse Classpath。