在Eclipse使用gradle build
將Spring Boot專案打包為可佈署的WAR檔(deployable war)設定如下。
範例環境:
- Windows 64 Bit
- Java 8
- Eclipse Version: 2019-03 (4.11.0)
- Spring Boot版本2.1.7.RELEASE
- Eclipse Gradle Buildship plug-in
在專案的build.gradle
加入以下設定。
build.gradle
apply plugin: 'war' // 打包為war
war { enabled = true } // gradle build的時候Task :war才不會被SKIPPED
dependencies {
...
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // 排除embedded tomcat
...
}
SpringBootApplication類要繼承SpringBootServletInitializer
並覆寫configure(SpringApplicationBuilder application)
方法如下。
package com.abc.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringbootGradleApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootGradleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootGradleApplication.class, args);
}
}
然後在功能選單的Run -> Run Configurations...
中的[Gradle Project]新增一個Run設定如下。
[Gradle Task]輸入build
或war
。
[Working Directory]用右邊的Workspace...設為要build的專案。
設定完後按Run執行。
接著就會開始執行gradle build,並在Eclipse console印出下面訊息。
Working Directory: D:\MyProject\workspace\springboot-gradle
Gradle user home: C:\Users\matt\.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 5.6
Java Home: D:\Applications\Java\jdk.1.8.0_171
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: build
> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :bootWar
> Task :war
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build
BUILD SUCCESSFUL in 3s
4 actionable tasks: 3 executed, 1 up-to-date
執行結束後build好的WAR檔會在Eclipse的workspace
目錄該專案目錄下的/build/libs
。
然後就可以拿WAR檔佈署在Tomcat或JBoss了。
參考:
沒有留言:
張貼留言