🔥

Spring Bootのプロジェクト作成でハマったところ

2023/12/25に公開

はじめに

Spring Bootで以下のエラーがでてハマったので、その忘備録です。

エラー文

エラー概要: Gradleビルド中、org.springframework.boot:spring-boot-gradle-plugin:3.1.7の解決に失敗。
原因: Javaバージョン不一致。ビルドはJava 11互換性を要求しているが、利用可能なライブラリはJava 17でコンパイル。
追加問題: org.gradle.plugin.api-version属性が「8.5」と要求されているが、利用可能なバリアントではこの属性についての情報がない。

対応

最初に考えたのはJavaのバージョンの不一致です。Spring BootではJavaバージョンを17にしていましたが、EclipseではJavaバージョン11に設定していたことが原因と思いました。
しかしこちらをJavaバージョン11に変更しても改善しませんでした。

エラー文では「ビルドはJava 11互換性を要求している」と書いてあることを考えると、ビルドツールで使われている?Javaのバージョンが11の可能性があるのでは?と考え、設定>Grandleを見ると、Java Homeという項目にJava11のパスが書かれていた。
ここの部分を削除したところ、正常にSpring Bootでプロジェクトを開始することができた。

学んだこと

・Eclipseの設定でのGrandleにある、「Java Home」という項目は、Gradleがビルドを行う際に使用するJavaのインストールパスを指定するための設定である。

参考

https://qiita.com/pike3/items/487140520dc99c031598

エラー文(原文)

Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'DependencyInfectionSample4'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.7.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.7
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.7 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.7 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.7 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.7 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.7 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.7 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.7 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

CONFIGURE FAILED in 1s```

Discussion