即使存在依赖项,导入错误

时间:2018-12-21 12:04:54

标签: java spring-boot gradle importerror

当我将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 dependencies org.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-SNAPSHOTorg.springframework:spring-context:5.1.3.RELEASE -> 5.1.4.BUILD-SNAPSHOT可能不是兼容版本吗?

2 个答案:

答案 0 :(得分:2)

此类org.springframework.data.annotation.Id属于Spring模块org.springframework.data:spring-data-commons。因此,如果要从此模块导入类,则应向其添加依赖项:

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

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

build.gradle

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

答案 1 :(得分:-1)

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