LoginSignup
0
0

More than 5 years have passed since last update.

servletをlocal tomcat上で動かすまで

Posted at

仕事でmavenを使っているのですが、pomとかは使いまわすことが多いので、
いざ自分のPCでやってみたらちょっと時間かかったので、メモ。

環境

PC: Macbook Pro (macOS Sierra)
JAVA:1.8.0_40
Maven:3.5.0
IDE:IntelliJ CE 2017.1

やりたいこと

とにかくローカルでservletをtomcat7:runで起動させたい

Mavenのインストール

brewが入っている前提ですが、

brew install maven

そうでない場合は、
http://maven.apache.org/install.html
を参考に。仕事のPCは手動で入れたので、リンクを参考に下感じになります。

確認コマンド
$ mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T04:39:06+09:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_40, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.4", arch: "x86_64", family: "mac"

IntelliJでプロジェクト作成

New
-> Project
-> Maven
からプロジェクトを作成します。
Create from archetypeにチェックをいれます。
archetypeに
org.glassfish.jersey.archetypesjersey-quickstart-webappを利用しますので
Add Archetypeから
* groupId:org.glassfish.jersey.archetypes
* artifactId:jersey-quickstart-webapp
* version:2.16

を追加して選択します。
image

あとはNextで画面のとおりに。
Finishを押すと、がががとMavenが動いて
ディレクトリ構成とサンプルのリソースpom.xml
などを生成してくれます。

こんな感じで生成されます。
gitとかは必要に応じて。
image

Tomcat Pluginの追加

TomcatPluginをpom.xmlに追加します。
これを
build -> plugins
配下に追加します。

pom.xml
:
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>9090</port>
                    <server>localhost</server>
                    <!--<path>/foo</path>-->
                </configuration>
            </plugin>
:

さあ実行だ!

そして実行

command
mvn tomcat7:run

ぐりぐり動いてこんなメッセージが出てきます。

terminal
[INFO] Running war on http://localhost:9090/JerseySample
[INFO] Creating Tomcat server configuration at /Users/userName/Develop/Github/MyJerseySample/target/tomcat
[INFO] create webapp with contextPath: /JerseySample
4 29, 2017 10:37:28 午後 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["http-bio-9090"]
4 29, 2017 10:37:28 午後 org.apache.catalina.core.StandardService startInternal
情報: Starting service Tomcat
4 29, 2017 10:37:28 午後 org.apache.catalina.core.StandardEngine startInternal
情報: Starting Servlet Engine: Apache Tomcat/7.0.47
4 29, 2017 10:37:30 午後 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["http-bio-9090"]

ブラウザでアクセスしてみます。
image

Jersey resourceをクリックして
image

ちゃんとAPI叩けていますね!
ローカルで実行できると動作が目に見えるので、捗りますね

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