M12i.

学術書・マンガ・アニメ・映画の消費活動とプログラミングについて

Spring BootアプリケーションをビルドしてWARファイルにする

f:id:m12i:20141129104243p:plain

Spring Bootのビルド成果としてWARを得る方法を調べました。前提として、Spring BootアプリケーションのビルドにはMavenを使っています(GradleやSTSの場合の手順については触れません)。

Spring BootでWebアプリケーションをビルドする場合、デフォルトではJARファイルが生成され、それを実行すると組み込み型のTomcatが使用されます。けれども実際の使用においては、すでに独立して動くTomcatが存在しており、その上で自身のアプリケーションを動かしたいというケースも多いと思います。

今回も例によってSpring公式のドキュメントを参照しましたが、チップスなので訳出まで載せるのはやめておきます。

Mavenビルド設定の変更

まずはpom.xmlを変更します。

パッケージングの設定を変更するために<packaging>タグを追加します:

<packaging>war</packaging>

次に、サーブレット・コンテナの設定を変更するために、<dependency>タグも追加します。"provided"というスコープ指定が重要だそうです:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

さらに、公式ドキュメントではとくに触れられていないのですが、私の環境では<repositories>タグと<pluginRepositories>タグの追加も必要でした:

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

上記の2つ(もしくは4つ)のタグを追加したあとのpom.xmlファイルのイメージとして、公式ドキュメントで紹介されているコードを次に示します:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-convert-jar-to-war-maven</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <start-class>hello.Application</start-class>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

サーブレット初期化子の追加

web.xmlなしにアプリケーションを起動するため、サーブレット・コンテナに対して、あなたのアプリケーションをどのように起動すべきかを示すJavaコードを追加します。

次のコードはあなたのアプリケーション・コードがhelloパッケージとそのサブ・パッケージにあると仮定した場合のものです:

package hello;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class WebInitializer extends SpringBootServletInitializer {

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

}

Application.class@ComponentScan@EnableAutoConfigurationが付与されたクラスです:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

WARファイルのビルド

次のコマンドを実行するとWARファイルのビルドが行われます:

mvn clean package

targetディレクトリの配下にできたWARファイルをTomcatのwebappsディレクトリに投入すれば自動で展開→起動されるはずです。

mvn spring-boot:runコマンドを実行した時にはhttp://localhost:8080/でアプリケーションが起動しますが、mvn clean packageコマンドを実行してWARをつくってデプロイした場合にはhttp://localhost:8080/(WARファイル名)/というURLでアプリケーションが起動します。


Spring関連の訳出記事まとめ - M12i.