Web系開発メモ

Java, C#, HTML, CSS, JavaScript のことなどを書いてます。

MavenでWarを作成する方法(Hello Wrold出力アプリ)

Maven で WAR を作成して、Hello World を出力する方法を書いていきます。

バージョン

ブログ執筆時の製品バージョンは以下の通りです。

ディレクトリ階層の作成

以下のコマンドで、サンプルプロジェクト sample-warディレクトリ階層を作成します。

mkdir sample-war
cd sample-war
mkdir src\main\java\org\sample\war
mkdir src\main\webapp\WEB-INF

資源の配置先は以下の通りです;

  • src/main/java/**Java のコード
  • src/main/webapp/**: Web資源(HTML, CSS, JS, テンプレート, etc)

pom.xml の作成

以下の pom.xml を作成します。

sample-war/pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.sample</groupId>
  <artifactId>sample-war</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>sample-war</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
      </plugin>
    </plugins>
  </build>
</project>

<finalName> で war の名前を指定しています。指定しないと、アーティファクトID+バージョン(sample-war-1.0.0.war)になります。

サーブレットの作成

以下のサーブレットを作成します。

sample-war/src/main/java/org/sample/war/HelloServlet

package org.sample.war;

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")
@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
      res.setContentType("text/plain");
      res.setCharacterEncoding("UTF-8");
      res.getWriter().println("Hello World");
  }
}

HTML の作成

以下の HTML を作成します。

sample-war/src/main/webapp/index.html

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sample WAR</title>
</head>
<body>
  <p>Hello World</p>
</body>
</html>

WAR の作成

sample-war の下で以下のコマンドを実行します。

mvn package

テストをスキップしたい場合は、以下のコマンドを実行します。

mvn compile war:war

実行すると、target/sample-war.war が作成されています。

デプロイ・コンテナ起動

Tomcatwebapps 配下に war を配置してコンテナを起動します。

動作確認

ブラウザで以下の URL にアクセスすると、Hello World が表示されます。

http://localhost:8080/sample-war/hello
http://localhost:8080/sample-war/index.html

参考文献

Maven WAR Plugin