Spring 启动多个 gradle 项目依赖管理 -> 无法更改配置的依赖

Spring boot multiple gradle projects dependcy mangament -> Cannot change dependencies of configuration

我有几个项目都具有类似的结构

projectA
| - projectA-module1
| - projectA-module2
| - projectA-service

project-service -> 该模块应用了spring boot plugin,可部署所有其他模块都是该模块的依赖项。每个模块只需要 ex 的特定 spring 依赖项。 module1 需要 mail api,module2 只需要 rabbitmq api 等。我想避免添加 spring 引导插件及其对每个模块的所有依赖项。所以我想我可以使用 dependency-management-plugin 并添加到每个 moduleX 并配置它,对于模块服务只需配置它,因为它已经被插件 spring boot 添加了。在我的自定义插件中,我有这个功能可以做到这一点:

protected def addAndConfigureDependencyManagementExtension(){
    if(project.name.matches("[a-z]*-service")){
        LOGGER.info("Project ${project.name} have allready applied plugin ${SPRING_DEPENDENCY_PLUGIN_ID}. Just configure it")
        validateSpringBootVersions()
    }else {
        DependencyManagementPlugin dependencyManagementPlugin = new DependencyManagementPlugin()
        dependencyManagementPlugin.apply(project)
    }
    project.extensions.configure(DependencyManagementExtension, new ClosureBackedAction<DependencyManagementExtension>({
        imports {
            mavenBom("org.springframework.cloud:spring-cloud-dependencies:Brixton.BUILD-SNAPSHOT")
        }
    }))
}

这是我的 moduleX 构建。

apply plugin: 'myplugin'

dependencies {
    compile 'org.springframework:spring-context'
    compile 'org.springframework:spring-web'
}

这是我的服务模块的build.gradle:

buildscript {
    ext {
        springBootVersion = '1.3.2.RELEASE'
    }

    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'spring-boot'
apply plugin: 'my-plugin'

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-remote-shell')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile project(':my-project')
}

问题是,当我尝试 运行 ./gradlew 构建时,我得到:

Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.

我不知道为什么会这样,也不知道如何解决。在此先感谢您的任何建议,提示。也许那是更简单的方法 |或已经可用的解决方案来实现依赖一致性?

编辑: 这是堆栈跟踪:

* Exception is:
org.gradle.api.InvalidUserDataException: Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.validateMutation(DefaultConfiguration.java:597)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.run(DefaultConfiguration.java:145)
    at org.gradle.api.internal.DefaultDomainObjectCollection.assertMutable(DefaultDomainObjectCollection.java:267)
    at org.gradle.api.internal.DefaultDomainObjectCollection.add(DefaultDomainObjectCollection.java:158)
    at org.gradle.api.internal.DelegatingDomainObjectSet.add(DelegatingDomainObjectSet.java:81)
    at java_util_Set$add[=17=].call(Unknown Source)
    at io.spring.gradle.dependencymanagement.DependencyManagement$_resolve_closure2.doCall(DependencyManagement.groovy:150)
    at io.spring.gradle.dependencymanagement.DependencyManagement.resolve(DependencyManagement.groovy:150)
    at io.spring.gradle.dependencymanagement.DependencyManagement.resolveIfNecessary(DependencyManagement.groovy:130)
    at io.spring.gradle.dependencymanagement.DependencyManagement.getExclusions(DependencyManagement.groovy:124)
    at io.spring.gradle.dependencymanagement.DependencyManagement$getExclusions.call(Unknown Source)
    at io.spring.gradle.dependencymanagement.DependencyManagementContainer.getExclusions(DependencyManagementContainer.groovy:104)
    at io.spring.gradle.dependencymanagement.DependencyManagementContainer$getExclusions.call(Unknown Source)
    at io.spring.gradle.dependencymanagement.exclusions.ExclusionConfiguringAction.findExcludedDependencies(ExclusionConfiguringAction.groovy:99)
    at io.spring.gradle.dependencymanagement.exclusions.ExclusionConfiguringAction.applyMavenExclusions(ExclusionConfiguringAction.groovy:76)
    at io.spring.gradle.dependencymanagement.exclusions.ExclusionConfiguringAction.this$applyMavenExclusions(ExclusionConfiguringAction.groovy)
    at io.spring.gradle.dependencymanagement.exclusions.ExclusionConfiguringAction.execute(ExclusionConfiguringAction.groovy:71)
    at io.spring.gradle.dependencymanagement.exclusions.ExclusionConfiguringAction.execute(ExclusionConfiguringAction.groovy)
    at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:93)
    at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:82)
    at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:44)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:79)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:30)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy21.beforeResolve(Unknown Source)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.performPreResolveActions(DefaultConfiguration.java:384)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.resolveGraphIfRequired(DefaultConfiguration.java:365)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.resolveNow(DefaultConfiguration.java:342)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.getResolvedConfiguration(DefaultConfiguration.java:335)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated.getResolvedConfiguration(Unknown Source)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getFiles(DefaultConfiguration.java:661)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.getFiles(DefaultConfiguration.java:288)
    at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated.getFiles(Unknown Source)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext$FileTreeConverter.convertInto(DefaultFileCollectionResolveContext.java:200)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext.doResolve(DefaultFileCollectionResolveContext.java:107)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext.resolveAsFileTrees(DefaultFileCollectionResolveContext.java:78)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext$FileTreeConverter.convertInto(DefaultFileCollectionResolveContext.java:186)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext.doResolve(DefaultFileCollectionResolveContext.java:102)
    at org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext.resolveAsFileTrees(DefaultFileCollectionResolveContext.java:78)
    at org.gradle.api.internal.changedetection.state.DefaultFileCollectionSnapshotter.visitFiles(DefaultFileCollectionSnapshotter.java:85)
    at org.gradle.api.internal.changedetection.state.DefaultFileCollectionSnapshotter.snapshot(DefaultFileCollectionSnapshotter.java:54)
    at org.gradle.api.internal.changedetection.rules.TaskUpToDateState.<init>(TaskUpToDateState.java:56)
    at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactStateRepository$TaskArtifactStateImpl.getStates(DefaultTaskArtifactStateRepository.java:132)
    at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactStateRepository$TaskArtifactStateImpl.isUpToDate(DefaultTaskArtifactStateRepository.java:70)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.access[=17=]0(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter.proceed(DefaultBuildExecuter.java:43)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:154)
    at org.gradle.internal.Factories.create(Factories.java:22)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151)
    at org.gradle.initialization.DefaultGradleLauncher.access0(DefaultGradleLauncher.java:32)
    at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:99)
    at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:93)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
    at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:77)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:47)
    at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
    at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:170)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)

编辑 2

我创建了两个 github 项目来重现问题。这是链接 project and plugin。我注意到在 Intellij 15 上刷新 gradle 项目不起作用,因为 "Error:Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved." 但是 gradle 从命令行构建几乎可以工作。新设置中存在的唯一问题是从我的插件配置依赖管理插件似乎不起作用。我需要在 test-service/build.gradle 中重复 dependencyManagement。代码有问题吗?

在您的复制中,您的插件包含此逻辑:

@Override
void apply(Project project) {
    if(project.name.matches("[a-z]*-service")){
        println("Skipping dependency managament plugin for ${project.name}")
        return
    }else {
        println("Added dependency managament plugin for ${project.name}")
        DependencyManagementPlugin dependencyManagementPlugin = new DependencyManagementPlugin()
        dependencyManagementPlugin.apply(project)
    }
    project.extensions.configure(DependencyManagementExtension, new ClosureBackedAction<DependencyManagementExtension>({
        imports {
            mavenBom("org.springframework.cloud:spring-cloud-dependencies:Brixton.BUILD-SNAPSHOT")
        }
    }))
}

你说:

Only problem that exists in new setup is that configuring dependency management plugin from my plugin seems not to working. I need to repeat dependencyManagement in test-service/build.gradle

这是由于第一个 if 块中的 return 语句。您不仅跳过了应用依赖管理插件的逻辑,还跳过了导入 Spring Cloud bom 的逻辑。

没有那个 return 语句,你的示例项目为我成功构建 ./gradlew clean build