VS Code + Spring Boot + Maven + JPA + H2 で最速 Web API 環境の構築(Step1)
VS Code での Spring Boot 実行環境の構築
「VS Code + Spring Boot + Maven + JPA + H2 で最速 Web API 環境の構築」と題しましたが、全4Stepにしたいと思います。操作に慣れていれば、全行程1時間程度あれば十分書けると思うのですが、まあ、ゆっくり進みましょう。
初回の Step1 では、「VS Code での Spring Boot 実行環境の構築」を行います。VSCodeでSpring Bootのプロジェクトを実行する - Qiita を参考にしています。先に一読してくれると、進めやすいかと思います。今回はまず、Spring Boot 実行環境を構築することが目的ですので、H2、JPA とかは次回以降のステップで対応します。
前提条件
プラグインのインストール
JDK、Maven のインストールは終えているものとします。VS Code に下記のプラグインをインストールします。
プロジェクトの作成
Spring Initializr を利用して、Spring Boot プロジェクトを作成します。先のプラグインのインストールにより、VS Code 上で操作が完了できます。ここでは、依存関係を Web, Lombok だけにしときます。JPA と、H2 は後で追加予定。
- Ctrl + Shift + P > Spring Initializr: Generate a Maven Project
- Specify project language: Java
- Input Group Id for your project.: com.example
- Input Artifact Id for your project.: sandbox
- Specify Spring Boot version.: 2.1.2
- Search for dependencies.: Web, Lombok
- 適当なディレクトリに配置
動作確認
動作確認を行います。F5 を押下し、「デバッグの開始」を行ってください。初回実行時は、自動で設定ファイル(launch.json)を構築して、確認を促されます。launch.json が生成されたら、中身を確認してもう一度 F5 を押下し、デバッグを開始します。デバッグコンソールで、Tomcat の起動が確認できれば、起動成功です。
{ // IntelliSense を使用して利用可能な属性を学べます。 // 既存の属性の説明をホバーして表示します。 // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "java", "name": "Debug (Launch) - Current File", "request": "launch", "mainClass": "${file}" }, { "type": "java", "name": "Debug (Launch)-DemoApplication<sandbox>", "request": "launch", "mainClass": "com.example.sandbox.DemoApplication", "projectName": "sandbox" } ] }
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.2.RELEASE) ・・・・ 略 ・・・・ 2019-01-23 11:18:04.897 INFO 10040 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-01-23 11:18:04.898 INFO 10040 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1445 ms 2019-01-23 11:18:05.149 INFO 10040 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-01-23 11:18:05.387 INFO 10040 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-01-23 11:18:05.390 INFO 10040 --- [ main] com.example.sandbox.DemoApplication : Started DemoApplication in 2.318 seconds (JVM running for 3.922)
JUnit の実行
ついでに、JUnit の実行も確認しておきましょう。
Test Explorer の表示
画面左側にあるアクティビティバーより、実験マーク(Test Explorer)をクリックしてください。もちろん、コマンドパレット(Ctrl+Shift+P)から、「Test Explorer」でも構いません。JUnit の実行
右上の矢印から、「Run Test」を実行してください。実行の進捗は、画面下部のステータスバーに表示されています。テストが完了したら、ステータスバーの結果をクリックして、View test report を確認してください。
git へ登録
.gitignore の作成
折角なので、この段階までを Git に登録します。不要なものが多いので、.gitignore を作成します。gitignore.io - Create Useful .gitignore Files For Your Projectこのサイトが便利です。今回は、[Web, Java, Maven, VisualStudioCode] を指定しました。生成された .gitignore をコピーして、自身のプロジェクトに反映してください。
git へ登録
git プロジェクトを作成して、通常通り登録すればOKです。下記は参考までに、github のいつものコマンド。ちなみに、VS Code 上からも、Ctrl+@ でターミナルが利用できます。
echo "# sandbox" >> README.md git init git add README.md git commit -m "first commit" git remote add origin git@github.com:smd101/web-api.git git push -u origin master
その他、github で匿名メールアドレスを利用するのに、下記のサイトも参考にしました。
ここまでのコードを github 上で公開しています。良かったら、利用してください。