LoginSignup
4
8

More than 3 years have passed since last update.

Spring Initializr のパッケージング JAR と WAR の差異

Last updated at Posted at 2019-11-12

Spring Initializr で Web アプリケーションを生成

Spring Initializr にて以下の設定で Web アプリケーションの雛形を生成する。

  • Project: Gradle Project
  • Language: Java
  • Spring Boot: 2.2.1
  • Java: 8
  • Dependencies: Spring Web

Packaging の項目を Jar および War にしたものをそれぞれ生成して diff コマンドで差分を取った。

Jar と War の差異

$ diff -r jar war
diff -r jar/demo/build.gradle war/demo/build.gradle
4a5
>   id 'war'
16a18
>   providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
Only in war/demo/src/main/java/com/example/demo: ServletInitializer.java

Gradle War Plugin

War のアプリケーションには Gradle の War Plugin が追加されている。

id 'war'

War Plugin は war ファイルを生成するタスクを追加するプラグイン。

参考: The War Plugin

Spring Boot Tomcat Starter

War のアプリケーションには Spring Boot Tomcat Starter が providedRuntime で指定されている。

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

これはローカル環境での実行時には組み込み Tomcat が使われるが、providedRuntime を指定することで war ファイル生成時には組み込み Tomcat を含めない設定になっている。
参考: Maven Repository: org.springframework.boot » spring-boot-starter-tomcat

ServletInitializer

War のアプリケーションには ServletInitializer が追加されている。

package com.example.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
  }

}

これは、war ファイルをデプロイして動作させる環境で必要な WebApplicationInitializer インターフェースを実装したクラスになる。
参考: SpringBootServletInitializer (Spring Boot Docs 2.2.1.RELEASE API)

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8