0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SpringbootをWAR形式でビルドしてTomcatにデプロイする方法

Posted at

【Spring Boot + Gradle 8.4】WAR形式でビルドしてTomcatにデプロイする方法

本記事では、Spring Boot 3.4.0 + Gradle 8.4 環境で WARファイルをビルドして外部Tomcatにデプロイする方法 を解説します。


🎯 前提条件

  • Spring Boot 3.4.0
  • Gradle 8.4
  • マルチプロジェクト対応も可能
  • 外部Tomcatにデプロイ(内蔵Tomcatではなく)

📦 Gradleの設定(build.gradle

1. war プラグインを追加

plugins {
    id 'java'
    id 'war'                             // ← WAR形式に必須
    id 'org.springframework.boot' version '3.4.0'
    id 'io.spring.dependency-management' version '1.1.4'
}

2. WARとJARの出力制御

bootJar {
    enabled = false                     // fatJarは無効化
}

bootWar {
    enabled = true                      // WAR出力を有効化
    archiveFileName = 'your-app-name.war' // 任意のファイル名
}

3. 外部Tomcat用の依存関係設定

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

    // Tomcatをprovided扱いに(内蔵しない)
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

🧱 SpringBootServletInitializer の設定

外部Tomcatにデプロイするには、SpringBootServletInitializer を継承する必要があります。

@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(YourApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

🏗️ WARファイルの出力と確認
以下のコマンドでビルド:

./gradlew clean build

出力先:

build/libs/your-app-name.war

このWARをTomcatの webapps/ ディレクトリに配置すれば、通常通り起動可能です。

🧩 補足:マルチプロジェクト構成の場合

以下のように settings.gradle でプロジェクトを定義し、各サブプロジェクトにも同様の設定を施すことで、マルチWAR構成に対応可能です。

// settings.gradle
include 'hoge', 'fuga'

各サブプロジェクトの build.gradle に上述の war 設定を行えばOKです。

✅ まとめ

設定項目 内容
war プラグイン id 'war' を追加
bootJar を無効化 bootJar { enabled = false }
bootWar を有効化 bootWar { enabled = true }
Tomcatの依存関係 providedRuntime 'spring-boot-starter-tomcat'
外部Tomcat用のクラス SpringBootServletInitializer の継承

💬 おわりに

Spring BootアプリはデフォルトでJAR形式ですが、WAR形式にも柔軟に対応できます。既存のTERASOLUNAやStruts系のアーキテクチャからSpring Bootへ移行する際の参考になれば幸いです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?