Spring Bootプロジェクトを作成する

最終更新:2021年10月24日

[AD] scalapediaでは記事作成ボランティアを募集しています

この記事では、新規にSpring Bootのプロジェクトを作成する方法を解説します。

sbt newコマンドを使ってプロジェクトを生成する

Spring Bootプロジェクトを生成するための、sbtで使えるテンプレートが用意されています。

以下のコマンドをシェルで実行してください。

shell
sbt new kijuky/springboot-scala.g8

コマンドを実行すると以下のような対話式のコンソールが始まります。

shell
% sbt new kijuky/springboot-scala.g8 [info] welcome to sbt 1.4.6 (AdoptOpenJDK Java 11.0.7) [info] loading global plugins from ./.sbt/1.0/plugins/project [info] loading settings for project global-plugins from plugins.sbt ... [info] loading global plugins from ./.sbt/1.0/plugins [warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings. [info] set current project to dev (in build file:./dev/) [info] set current project to dev (in build file:./dev/)

プロジェクト名の入力を求められます。入力してreturnキーを押します。

空欄のままreturnキーを押すと、デフォルトのspringboot-scalaが設定されます。

shell
name [springboot-scala]:

組織名の入力を求められます。お好きな文字列を入力してください。

ここではcom.noriakihoriuchiを指定します。

shell
organization [com.example]: com.noriakihoriuchi

パッケージ名の入力を求められます。このままでよければ空欄のままreturnキーを押してください。

変更したい場合は入力した後returnキーを押してください。

shell
package [com.noriakihoriuchi.springbootscala]:

これにて入力完了です。テンプレートからプロジェクトが作成されました。

shell
Template applied in ./springboot-scala user@macbook-pro dev %

作成されたプロジェクトのディレクトリに移動し、サーバーを起動してみましょう。

shell
cd ./springboot-scala docker compose up -d sbt server/run

依存性が解決されます

shell
... 100.0% [##########] 680.1 KiB (583.8 KiB / s) [info] Fetched artifacts of [info] Updating [info] compiling 1 Scala source to ./dev/springboot-scala/shared/.jvm/target/scala-2.13/classes ... [info] Resolved dependencies [info] Fetching artifacts of [info] Fetched artifacts of [info] Non-compiled module 'compiler-bridge_2.13' for Scala 2.13.6. Compiling... [info] compiling 1 Scala source to ./dev/springboot-scala/shared/.js/target/scala-2.13/classes ... [info] Compilation completed in 7.092s. [info] compiling 1 Scala source to ./dev/springboot-scala/client/target/scala-2.13/classes ... [info] Fast optimizing ./dev/springboot-scala/client/target/scala-2.13/client-fastopt [info] compiling 2 Scala sources to ./dev/springboot-scala/server/target/scala-2.13/classes ... [info] running com.noriakihoriuchi.springbootscala.server.ServerApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.0) [run-main-0] INFO scala.App - Starting App using Java 11.0.7 on macbook-pro.local with PID 55545 (./Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.6/scala-library-2.13.6.jar started by noriaki in ./dev/springboot-scala) [run-main-0] INFO scala.App - No active profile set, falling back to default profiles: default [run-main-0] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode. [run-main-0] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 6 ms. Found 0 JPA repository interfaces. [run-main-0] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http) ...

http://localhost:8080 を開き、以下のように表示されればOKです。

使用したテンプレート

今回プロジェクトを生成する際に使用したテンプレートはこちらです。

サイト内検索


カテゴリ「Spring Boot入門」の記事


カテゴリ「Spring Boot入門」の記事