LoginSignup
20
28

More than 5 years have passed since last update.

「Spring 徹底入門」の開発環境を IntelliJ IDEA で構築する

Posted at

「Spring 徹底入門 Spring Framework による Java アプリケーション開発」の開発環境を IntelliJ IDEA で構築する方法をメモっとく。

新しいプロジェクトを作成する

IntelliJ IDEA を起動して Create New Project をクリックする。

welcome-to-intellij-idea.png

Maven プロジェクトを選択し Create from archetype をチェックし maven-archetype-webapp を選択して Next をクリックする。

new-project-1.png

GroupId と ArtifactId を付与して Next をクリックする。

new-project-2.png

内容を確認して Next をクリックする。

new-project-3.png

Project name を確認して Finish をクリックする。

new-project-4.png

プロジェクトが作成されると Maven タスクが実行されるので完了するまで待とう。

ファイルを修正・追加する

pom.xml と web.xml を修正する。また include.jsp を追加する。

pom.xml

pom.xml を下記のように修正する。

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>example</groupId>
    <artifactId>firstapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>firstapp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>2.0.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-jstlel</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>firstapp</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

web.xml

web.xml を下記のように修正する。

src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <include-prelude>/WEB-INF/include.jsp</include-prelude>
        </jsp-property-group>
    </jsp-config>
</web-app>

include.jsp

include.jsp を下記のような内容で追加する。

src/main/webapp/WEB-INF/include.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Maven プロジェクトを再インポートする

プロジェクトツリーのプロジェクト名を右クリックして Maven > Reimport をクリックする。

maven-reimport.png

アプリケーションを Tomcat にデプロイする

IntelliJ IDEA でアプリケーションを Tomcat にデプロイする。

Tomcat をインストールする

Homebrew で Tomcat をインストールするには下記のコマンドを実行する。

$ brew install tomcat

Tomcat を追加する

メニューの Run > Edit Configurations をクリックする。

Run/Debug Configurations が表示されたら+のアイコンをクリックする。

run-debug-configurations-1.png

設定一覧の N items more (irrelevant) をクリックして続きを表示させて Tomcat Server を選択し Local をクリックする。

add-new-configuration-1.png

add-new-configuration-2.png

add-new-tomcat-server-configuration.png

Tomcat Home を指定して OK をクリックする。

tomcat-server.png

追加された Tomcat Server を選択し Deployment タブをクリックする。

run-debug-configurations-2.png

+のアイコンをクリックし Artifact をクリックする。

run-debug-configurations-3.png

run-debug-configurations-4.png

デプロイするプロジェクト名を選択する。通常はホットデプロイしたいので exploded がついてる方を選択して OK をクリックする

select-artifacts-to-deploy.png

これでアプリケーションがデプロイ対象として追加された。

run-debug-configurations-5.png

最後にファイルを保存したときにホットデプロイされるように On frame deactivation を Update classes and resources に変更しておこう。

run-debug-configurations-6.png

Tomcat を起動してアプリケーションにアクセスする

メニューの Run > Run 'Tomcat v8.5' を実行して Tomcat を起動する。もしくは Application Servers ペインの Tomcat v8.5 を選択して実行アイコンをクリックして起動する。

サーバーの起動後にブラウザーが起動して Hello World! というページが表示されれば開発環境の構築は完了である。

20
28
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
20
28