LoginSignup
12
13

More than 5 years have passed since last update.

MavenでJettyを動かす

Last updated at Posted at 2015-12-03

某所で似たような記事を書きましたが、Javaな話はこちらにまとめつつ備忘録として。

必要なもの

必要なものは以下の通り。細かいバージョンは最新を使えば良いかと思います。

JAVA_HOME および、Mavenへのパスを通しておきましょう。

プロジェクト作成

mvnコマンドでプロジェクトのひな形を作ります(以下、改行済み)

> mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp 
-DgroupId=test.webapp -DartifactId=test-server -Dpackage=test.webapp
-DinteractiveMode=false
  • アーキタイプは通常のWebアプリを想定している
  • グループIDと、アーティファクトID、パッケージ名は適宜命名
  • インタラクティブモードをオフに

以下、コマンド実行時の出力(初回はもっと沢山のダウンロードメッセージが表示されるかもしれません)

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: d:\temp\sample
[INFO] Parameter: package, Value: test.webapp
[INFO] Parameter: groupId, Value: test.webapp
[INFO] Parameter: artifactId, Value: test-server
[INFO] Parameter: packageName, Value: test.webapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: d:\temp\sample\test-server
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.179 s
[INFO] Finished at: 2015-12-03T22:33:13+09:00
[INFO] Final Memory: 13M/219M
[INFO] ------------------------------------------------------------------------

d:\temp\sample> 

アーキタイプが古いとINFOされますが、今風の指定方法はがあるのかは未調査。フォルダ構成は以下の通りです。

D:.
└─test-server
    └─src
        └─main
            ├─resources
            └─webapp
                └─WEB-INF

pom.xml の編集

pom.xml は、Mavenのプロジェクト構成ファイル(Project Object Model)です。依存関係などを記述します。http://mvnrepository.com/ から必要なライブラリを探してきます

  • Dependencyには、Jetty Webサーバーを指定
  • Pluginには、Jetty-Maven プラグインを指定

以下に pom.xml の全部を貼り付けておきます。バージョンは適宜変更し、Jettyのポート番号もデフォから変えています。

<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>test.webapp</groupId>
  <artifactId>test-server</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test-server Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <jetty.version>9.3.6.v20151106</jetty.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jetty.version}</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>${jetty.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>test-server</finalName>
    <plugins>
      <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
        <configuration>
            <httpConnector>
                <port>8989</port>
            </httpConnector>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

実行

実行は、mvn jetty:run を実行するだけです。

d:\temp\sample\test-server>mvn jetty:run
mvn jetty:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test-server Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:9.3.6.v20151106:run (default-cli) > test-compile @ test-server >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-server ---
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-server ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-server ---
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory d:\temp\sample\test-server\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-server ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< jetty-maven-plugin:9.3.6.v20151106:run (default-cli) < test-compile @ test-server <<<
[INFO] 
[INFO] --- jetty-maven-plugin:9.3.6.v20151106:run (default-cli) @ test-server ---
[INFO] Logging initialized @2102ms
[INFO] Configuring Jetty for project: test-server Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = D:\temp\sample\test-server\target\classes
[INFO] Context path = /
[INFO] Tmp directory = D:\temp\sample\test-server\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///D:/temp/sample/test-server/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = D:\temp\sample\test-server\src\main\webapp
[INFO] jetty-9.3.6.v20151106
[INFO] Started o.e.j.m.p.JettyWebAppContext@469d003c{/,file:///D:/temp/sample/test-server/src/main/webapp/,AVAILABLE}{file:///D:/temp/sample/test-server/src/main/webapp/}
[INFO] Started ServerConnector@3b0ee03a{HTTP/1.1,[http/1.1]}{0.0.0.0:8989}
[INFO] Started @2819ms
[INFO] Started Jetty Server

http://localhost:8989/ にアクセスして、Hello Worldが表示されればOKです。

2015-12-03 22_55_07-http___localhost_8989_ - Internet Explorer.png

以上

12
13
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
12
13