1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Java][Spring Boot] 自動デプロイする - NetBeansで始めるSpring Boot (4)

Posted at

NetBeansで始めるSpring Bootと題して、記事を3つ書きました。

  1. NetBeansで始めるSpring Boot
  2. Spring BootでJAX-RSを使う
  3. ログイン画面を作る

「NetBeansで〜」と題しながら、自動デプロイが動かず不便でした。

アプリケーションサーバとしてTomcatを動かし、自動デプロイさせるようにします。

アプリケーションクラスを変更

アプリケーションクラスをSpringBootServletInitializerから継承させるようにします。

src/main/java/com/example/DemoApplication.java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

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

mainメソッドは不要な気もするのですが、ないとビルド通らないです。

pom.xmlを変更

packagingをwar

packagingjarからwarへ変更します。

pom.xml
    <packaging>jar</packaging>

pom.xml
    <packaging>war</packaging>

組み込みTomcatを依存性から外す

scopeprovidedにします。

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

プロジェクトのプロパティを変更

プロジェクトを右クリック > [Properties]メニュー > [Run]ツリーとすすみ、ServerとしてTomcatを選択します。

Screenshot from 2016-11-01 09-24-22.png

[Actions]ツリーへすすみ、ActionsとしてRun project、Debug project、Profile projectをそれぞれ選択し、[Remove/Reset]ボタンをクリックします。

Screenshot from 2016-11-01 09-28-01.png

ビルドして実行……その前に

Tomcat 8.5以降を使用する場合は、NetBeans 8.2以降じゃないと、起動/停止に失敗します。

ビルドして実行

ちゃんとブラウザが起動します。

テンプレートを変更して保存すると、自動デプロイされて反映されます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?