6
11

More than 1 year has passed since last update.

Visual Studio Code で java spring framework 開発環境設定

Last updated at Posted at 2021-08-13

まえがき

VSCode が評判いいので Java (Spring MVC) の開発環境が移行できるか試行錯誤してみました。
結果的に割と使えるっぽい感じです(小さめのプロジェクトの場合)。
Eclipse の方が色々揃ってる気がしますが、何しろ起動は軽いです。
Eclipse で開発中のソースを移行する想定です。
普通にデバッグボタンを押して tomcat デバッグがしたい!

デバッグ面がちょっと要改良の印象です。
(サイズが大きいプロジェクトだとデバッグが起動しない場合あり。タスク実行・コマンド直打ちだと起動する。)

環境

Windows10 64bit
VSCode 1.59.0
JDK 1.8(プロジェクト用) , OpenJDK 11(Javaの拡張機能用)
maven 3.6.3
tomcat 7 (古い…)

準備

VSCode のインストール

Javaの拡張機能のインストール

Gitの拡張機能のインストール

フロント系拡張機能のインストール

他にも便利な拡張機能が色々あるので探すのも楽しいですね。

settings.json 設定

Javaの拡張機能は JDK 1.8 だと動かないので OpenJDK11 を入れました。
環境に合わせて設定してください。

settings.json
    "java.configuration.runtimes": [
        {
            "default": true,
            "name": "JavaSE-1.8",
            "path": "C:/Program Files/Java/jdk1.8.0_251"
        },
        {
            "name": "JavaSE-11",
            "path": "C:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot"
        }
    ],
    "java.debug.settings.hotCodeReplace": "auto",
    "java.help.firstView": "gettingStarted",
    "java.home": "C:/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot",
    "java.jdt.ls.vmargs": "-Dfile.encoding=UTF-8",
    "java.project.importOnFirstTimeStartup": "automatic",
    "maven.terminal.customEnv": [
        {
            "environmentVariable": "MAVEN_OPTS",
            "value": "-Dfile.encoding=UTF-8 -Dproject.build.sourceEncoding=UTF-8 -Dproject.reporting.outputEncoding=UTF-8"
        }
    ]

Git からチェックアウト

Eclipse (EGit) とは使い方が結構違いますね・・・。

ワークスペースを保存 ~ ビルド

・「ファイル」→「名前をつけてワークスペースを保存」
・ライブラリ的なGitリポジトリがある場合はチェックアウト(Ctrl+Shift+p からの Git: Clone)
して、「ファイル」→「フォルダーをワークスペースに追加」すると関連付けられます。
・JAVA PROJECTSバーの右側の「…」をクリック→「Configure Java Runtime」をクリックして設定が合っているか確認
・合っていれば MAVEN バーから package したり install ができると思います。

Tomcat サーバーの設定

・TOMCAT SERVERSバーの右側にある「+」をクリック
・Tomcatサーバのディレクトリを指定 (ない場合はダウンロードして下さい)

・ここで指定したディレクトリで動かすわけではなく、ユーザーの VSCode のディレクトリにコピーして動かしています。
・作成したサーバーを右クリック→ 「Open Server Configuration」で作業用ディレクトリの中にある設定ファイルが編集できます。
・サーバーを右クリック→「Start」でちゃんと起動すればOKです。

Warファイルのデバッグ

・mvn package で作った war ファイル (自分の場合はmaven-war-pluginで作成) を右クリック
・「Debug on Tomcat Server」でデバッグができます。
・ブレイクポイントで停止することを確認。

一応これでもデバッグできますが、毎回パッケージするのが微妙なので maven plugin と連携して動かします。

tomcat7-maven-plugin (cargo-maven3-plugin)

プロジェクトの pom.xml(build) を編集

pom.xml
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <uriEncoding>utf-8</uriEncoding>
                        </configuration>
                    </plugin>

cargo-maven3-plugin の場合はコチラ

pom.xml
                    <plugin>
                        <groupId>org.codehaus.cargo</groupId>
                        <artifactId>cargo-maven3-plugin</artifactId>
                        <version>1.9.6</version>
                        <configuration>
                            <!-- Container configuration -->
                            <container>
                            <containerId>tomcat7x</containerId>
                            <type>embedded</type>
                            </container>
                        </configuration> 
                    </plugin>

tasks.json 編集

・プロジェクトディレクトリの .vscode/tasks.json を編集します。(ない場合は作成)
label が run-tomcat のほうは tomcat7-maven-plugin、
run-tomcat-cargo の方が cargo-maven3-plugin です。

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run-tomcat",
            "type": "shell",
            "command": "$env:MAVEN_OPTS += ' -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n'; mvn tomcat7:run",
            "group": "build",
            "isBackground": true,
            "problemMatcher": [{
                "pattern": [{
                    "regexp": "\\b\\B",
                    "file": 1,
                    "location": 2,
                    "message": 3
                }],
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "^.*Listening for",
                    "endsPattern": "^.*transport dt_socket at address.*"
                }
            }]
        },
        {
            "label": "run-tomcat-cargo",
            "type": "shell",
            "command": "$env:MAVEN_OPTS += ' -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n'; mvn cargo:run",
            "group": "build",
            "isBackground": true,
            "problemMatcher": [{
                "pattern": [{
                    "regexp": "\\b\\B",
                    "file": 1,
                    "location": 2,
                    "message": 3
                }],
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "^.*Listening for",
                    "endsPattern": "^.*transport dt_socket at address.*"
                }
            }]
        },
        {
            "label": "stop-tomcat",
            "type": "shell",
            "command": "echo ${input:terminate}}",
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "id": "terminate",
            "type": "command",
            "command": "workbench.action.tasks.terminate",
            "args": "run-tomcat"
        }
    ]
}

launch.json 編集

・プロジェクトディレクトリの .vscode/launch.json を編集します。(ない場合は作成)

launch.json
{
    "version": "0.2.0",
    "configurations": [

        {
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 8000,
            "preLaunchTask": "run-tomcat",
            "postDebugTask": "stop-tomcat"
        }
    ]
}

cargo の場合は preLaunchTask を run-tomcat-cargo にして下さい。

デバッグ準備完了

・「実行」メニュー→「デバッグの開始」で tomcat が起動してサービスが立ち上がります。
(プロジェクトが大きいと立ち上がらない場合があります…バージョンアップしたらまた試してみようかと。)
・設定したブレイクポイントでちゃんと止まればOK!
・停止は「実行」メニュー→「デバッグの停止」

プロパティファイルが置換されない場合 (jdbc.propertiesなど)

・Warファイル作成時にファイルをコピーする設定になっていたりする場合(自分がそうでした)
・org.springframework.context.support.PropertySourcesPlaceholderConfigurer の設定で
起動時に置換して読み込んでいるので、設定してあげると置換されるようになります。

暫く色々VSCodeで試してみようと思います。
以上、お疲れさまでした!

6
11
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
6
11