1
3

Spring Boot 依存関係の追加(プロジェクト作成後)

Last updated at Posted at 2024-01-28

はじめに

Spring Bootプロジェクト作成後にdependencies(依存関係)を追加する方法を紹介します。

前提条件

  • ビルドツールにMavenまたはGradleを利用していること
  • Spring Bootプロジェクトが作成済みであること
    もしプロジェクト未作成であれば以下の記事を参考にしてください。※以下の記事はIDEにIntelliJを使用しています
    https://qiita.com/high_high/items/263cfcbd712ada6cddd3

対象者

  • 開発を進める途中で依存関係を新しく追加したい方

依存関係追加手順

※本記事では例としてPostgreSQLを使用するための依存関係を追加する手順となっております。
 また、ビルドツールがMaven or Gradleの場合で手順が多少異なります

  1. Spring Initializrにアクセス

  2. 「Maven」を選択(Gradleの場合は「Gradle - Groovy」を選択)
    スクリーンショット 2024-01-28 17.07.04.png

  3. 「ADD DEPENDENCIES...」をクリック
    スクリーンショット 2024-01-28 17.13.29.png

  4. 依存関係を検索して追加(例:PostgreSQL Driver)
    スクリーンショット 2024-01-28 16.52.13.png

  5. 「EXPLORE」をクリック
    スクリーンショット 2024-01-28 17.17.16.png

  6. dependenciesの中に追加された対象の依存関係をコピー
    スクリーンショット 2024-01-28 17.40.16.png
    spring-boot-starterspring-boot-starter-testはデフォルトで追加されているものなので気にしない

  7. 依存関係を追加したいプロジェクトを開き、pom.xml(Gradleの場合はbuild.gradle)に 6 でコピーしたものをペースト

    pom.xml
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            --- 追加 ここから ---
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
            --- 追加 ここまで ---
        </dependencies>
    
    build.gradle
    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter'
    	testImplementation 'org.springframework.boot:spring-boot-starter-test'
    	runtimeOnly 'org.postgresql:postgresql' // ←追加
    }
    

おわりに

手順は以上で終了です。
ここまで読んでいただきありがとうございます。

1
3
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
1
3