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?

GradleとTomcatを使ったJava Servletアプリのデプロイ方法

Last updated at Posted at 2024-08-25

EclipseなどのIDEは使わずCLIのみでHello Worldアプリのデプロイまでおこなった。備忘録。

確認済み環境

  • Mac OS
  • OpenJDK 17.0.12
  • Gradle 8.10
  • Tomcat 10.1.28

手順

  1. ディレクトリ作成

    $ mkdir HelloWorldServlet && cd HelloWorldServlet
    
  2. Terminalでgradle initを実行。実行後聞かれる設定は以下。

    • Select type of build to generate: 1. Application
    • Select implementation language: 1. Java
    • target Java version: 17
    • Project name: HelloWorldServlet
    • Select application structure: 1. Single application project
    • Select build script DSL: 2. Groovy
    • Select test framework: 4. JUnit Jupiter
    • Generate build using new APIs and behavior?: no
  3. 以下のファイルを作成。

    ./app/src/main/java/org/example/HelloWorldServlet.java
    package org.example;
    
    import java.io.IOException;
    import jakarta.servlet.ServletException;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    
    @WebServlet("/hello")
    public class HelloWorldServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("<h1>Hello, World!</h1>");
        }
    }
    
  4. ./app/build.gradleを編集

    ./app/build.gradle
    plugins {
        id 'war'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'jakarta.servlet:jakarta.servlet-api:6.0.0'
        providedRuntime 'org.apache.tomcat:tomcat:10.1.28'
    }
    
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }
    
    tasks.named('war') {
        archiveFileName = 'helloworld.war'
    }
    
  5. web.xmlを作成

    ./app/src/main/webapp/WEB-INF/web.xml
    <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                                 https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
             version="6.0">
    </web-app>
    
  6. ディレクトリ構造は以下のようになっていればOK。

    $ tree .
    .
    ├── app
    │  ├── build.gradle
    │  └── src
    │     ├── main
    │     │  ├── java
    │     │  │  └── org
    │     │  │     └── example
    │     │  │        └── HelloWorldServlet.java
    │     │  └── resources
    │     └── webapp
    │     │  └── WEB-INF
    │     │     └── web.xml
    │     └── test
    │        ├── java
    │        │  └── org
    │        │     └── example
    │        │        └── AppTest.java
    │        └── resources
    ├── gradle
    │  ├── libs.versions.toml
    │  └── wrapper
    │     ├── gradle-wrapper.jar
    │     └── gradle-wrapper.properties
    ├── gradlew
    ├── gradlew.bat
    ├── README.md
    └── settings.gradle
    
  7. gradleでビルドする。今回は-x testを付けてテストをスキップする。

    $ gradle build -x test
    
  8. warファイルができるので、それをデプロイしてtomcatを起動。その後http://localhost:8080/helloworld/helloにアクセスするとHello World!が表示される。

    $ cp ./app/build/libs/helloworld.war /usr/local/Cellar/tomcat/10.1.28/libexec/webapps/
    $ catalina start
    $ open http://localhost:8080/helloworld/hello
    
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?