almost 6 years ago
盡管 Spring 官方都在推 Make Jar, Not War 但很多公司也沒有轉換那麼快,所以還是會有用 War 部屬的時候,但是我們可以用 SpringBoot 開發!!喔耶~~
版本上限制
SpringBoot 建議使用 JDK 8 ,包成 war 則建議部屬在 servlet 3.0 以上,如果你想放在 servlet 2.5 ex:tomcat6 上執行
雖然官方有降版的套件(org.springframework.boot:spring-boot-legacy) 號稱可解決 But....
珍惜生命...不要浪費生命乖乖去寫傳統 spring 配置吧
官網 Packaging executable jar and war files
恩....文件又漏寫了一點點,跑不起來~~我已經麻痺了(挖鼻)
buildscript {
ext {
springBootVersion = '1.5.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = 'passport'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.microsoft.sqlserver:mssql-jdbc')
compileOnly('org.projectlombok:lombok')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
1.加上 apply plugin: 'war'
2.加上 war
3.加上 providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
打包時要移除附帶的 servlet container 避免跟外面的衝突
照官網教的以為就可以嗎? 沒有!!
4.繼承 SpringBootServletInitializer
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PassportApplication.class);
}
}
好啦~~就可以去 build ,會產出 myapp-0.5.0.war 名稱改一下丟到 Tomcat 就可以看到頁面啦