【问题标题】:Import error even though dependencies exist即使存在依赖项,导入错误
【发布时间】:2019-05-21 21:51:25
【问题描述】:

当我将 import org.springframework.data.annotation.Id; 添加到我的 Java 文件时,我收到此错误:

The import org.springframework.data cannot be resolved

我的直接想法是我缺少一个依赖项,但我不认为是这种情况。

这是我的 build.gradle 的依赖项。

dependencies {
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.synchronoss.cloud:nio-multipart-parser')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
    compile('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('io.projectreactor:reactor-test')
}

另外,我已经应用了这些插件:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

我是否遗漏了什么,或者我该如何解决这个问题?

其他信息。

我跑了./gradlew clean build --refresh-dependencies --debug。以下是日志的摘录:

22:25:30.664 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ImageService.java:40: error: constructor Image in class Image cannot be applied to given types;
22:25:30.664 [ERROR] [system.err]                                       new Image(path.hashCode(),
22:25:30.664 [ERROR] [system.err]                                       ^
22:25:30.664 [ERROR] [system.err]   required: String
22:25:30.665 [ERROR] [system.err]   found: int,String
22:25:30.665 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.698 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:26: error: constructor Image in class Image cannot be applied to given types;
22:25:30.700 [ERROR] [system.err]                       new Image("1", "learning-spring-boot-cover.jpg"),
22:25:30.700 [ERROR] [system.err]                       ^
22:25:30.700 [ERROR] [system.err]   required: String
22:25:30.700 [ERROR] [system.err]   found: String,String
22:25:30.701 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.701 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:27: error: constructor Image in class Image cannot be applied to given types;
22:25:30.701 [ERROR] [system.err]                       new Image("2", "learning-spring-boot-2nd-edition-cover.jpg"),
22:25:30.702 [ERROR] [system.err]                       ^
22:25:30.702 [ERROR] [system.err]   required: String
22:25:30.702 [ERROR] [system.err]   found: String,String
22:25:30.702 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.705 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:28: error: constructor Image in class Image cannot be applied to given types;
22:25:30.706 [ERROR] [system.err]                       new Image("3", "bazinga.png")
22:25:30.706 [ERROR] [system.err]                       ^
22:25:30.706 [ERROR] [system.err]   required: String
22:25:30.706 [ERROR] [system.err]   found: String,String
22:25:30.707 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.713 [ERROR] [system.err] 4 errors
22:25:30.716 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Execute compile for :compileJava'
22:25:30.726 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Execute compile for :compileJava' completed
22:25:30.733 [DEBUG] [org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository] Fingerprinting property destinationDir (Output) for task ':compileJava'
22:25:30.737 [LIFECYCLE] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] 
22:25:30.737 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Opening cache fileHashes.bin (/Users/me/git/learning-spring-boot/learning-spring-boot-ch2/.gradle/4.10.2/fileHashes/fileHashes.bin)
22:25:30.702 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] 
22:25:30.702 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :compileJava FAILED
22:25:30.738 [DEBUG] [org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter] Removed task artifact state for {} from context.
22:25:30.739 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':compileJava'
22:25:30.740 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Task :compileJava'
22:25:30.740 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Task :compileJava' completed
22:25:30.741 [INFO] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] :compileJava (Thread[Task worker for ':',5,main]) completed. Took 59.344 secs.
22:25:30.741 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Task worker for ':': released lock on :
22:25:30.742 [DEBUG] [org.gradle.internal.work.DefaultWorkerLeaseService] Worker lease root.1.13 completed (1 worker(s) in use)
22:25:30.743 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Task worker for ':': released lock on root.1.13
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Daemon worker Thread 7,5,main]] finished, busy: 0.0 secs, idle: 59.358 secs
22:25:30.744 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':',5,main]] finished, busy: 59.353 secs, idle: 0.006 secs
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':' Thread 3,5,main]] finished, busy: 0.0 secs, idle: 59.358 secs
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':' Thread 2,5,main]] finished, busy: 0.0 secs, idle: 59.36 secs
22:25:30.748 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskExecutionGraph] Timing: Executing the DAG took 59.365 secs
22:25:30.748 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Run tasks'
22:25:30.748 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Run tasks' completed
22:25:30.749 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':compileJava'.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Compilation failed; see the compiler error output for details.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.  Run with --scan to get full insights.
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Get more help at https://help.gradle.org
22:25:30.751 [WARN] [org.gradle.internal.featurelifecycle.LoggingDeprecatedFeatureHandler] 
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warnings
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] 
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 1m 47s

我检查了./gradlew dependenciesorg.springframework.boot:spring-boot-starter-data-mongodb-reactive -> 2.2.0.BUILD-SNAPSHOT 包含org.springframework.data:spring-data-mongodb:2.1.4.BUILD-SNAPSHOT,其中包含org.springframework:spring-context:5.1.3.RELEASE -> 5.1.4.BUILD-SNAPSHOT。难道org.springframework:spring-context:5.1.3.RELEASE -> 5.1.4.BUILD-SNAPSHOT不是兼容版本?

【问题讨论】:

  • 你是否使用了应用插件:'io.spring.dependency-management'? (或弹簧靴)
  • 是的。我添加了应用于问题的插件。
  • 尝试清理您的项目并重新安装依赖项。
  • 尝试应用插件:'spring-boot'

标签: java spring-boot gradle importerror


【解决方案1】:

这个类org.springframework.data.annotation.Id属于Spring模块org.springframework.data:spring-data-commons。所以如果你想从这个模块中导入类,你应该给它添加一个依赖:

dependencies{
     compile("org.springframework.data:spring-data-commons")
     // others ...
}

注意您还应该能够在不显式依赖spring-data-commons 模块的情况下导入此类,因为该模块已经是您项目的传递依赖项的一部分(来自spring-boot-starter-data-mongodb-reactive)。这意味着使用您当前的build.gradle 设置,您应该能够正确导入此类。

./gradlew dependencies 

+--- org.springframework.boot:spring-boot-starter-data-mongodb-reactive -> 2.0.0.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:2.0.0.RELEASE (*)
|    +--- org.springframework.data:spring-data-mongodb:2.0.5.RELEASE
|    |    +--- org.springframework:spring-tx:5.0.4.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.0.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-context:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-beans:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-core:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-expression:5.0.4.RELEASE (*)
|    |    +--- org.springframework.data:spring-data-commons:2.0.5.RELEASE (*)
|    |    \--- org.slf4j:slf4j-api:1.7.25

因此,您的项目中可能存在同步问题,请尝试清理/关闭/重新打开它。

【讨论】:

    【解决方案2】:

    您确定data 是导入包的一部分吗?在我看来,您需要将 org.springframework.data 添加到您的 gradle 文件中。你可以看到here data 是不同的包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2020-09-28
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多