我如何从自定义插件中的 Spring Dependencies Managment Plugin 获取所有托管版本依赖项?

How i can take all managed version dependencies from Spring Dependencies Managment Plugin within my custom plugin?

我正在编写自定义 java gradle 插件来检查依赖关系。我也申请 Spring Gradle Dependency Managment plugin。但是我不能从这个插件中获取托管库版本的列表。我使用下一个代码:

import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.*;

import java.util.Map;

public class MyDependencyManagementPlugin implements Plugin<Project> {

    public void apply(Project project) {

        for (Configuration config : project.getConfigurations()) {

            config.resolutionStrategy(
                    strategy -> {
                        final DependencyManagementExtension container = project.getExtensions().getByType(DependencyManagementExtension.class);
                        Map<String, String> managedVersions = container.getManagedVersions();
                        System.out.println("Libraries from Spring Dependency Management Plugin: " + managedVersions.toString());

                        strategy.eachDependency(new Action<DependencyResolveDetails>() {
                            @Override
                            public void execute(DependencyResolveDetails dependencyResolveDetails) {

                                System.out.println("!!!! " + dependencyResolveDetails.getRequested().getGroup() + ':' + dependencyResolveDetails.getRequested().getName() + ':' + dependencyResolveDetails.getRequested().getVersion() + " -> " + dependencyResolveDetails.getTarget().getVersion());

                            }
                        });
                        strategy.failOnVersionConflict();
                    }
            );
        }
    }
}

在我的测试项目中,我使用了 Spring 框架:

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework:spring-core:4.2.5.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

但是在应用我的任务后,它总是returns 空地图。此外,如果我从 io Spring 依赖管理插件调用 dependencyManagement 任务,它也会 returns 空列表。 如果我不从我的自定义插件调用 etManagedVersions(),来自 io Spring 依赖管理插件的 dependencyManagement 任务工作正常。

谢谢

听起来你在调用 getManagedVersions() 之前任何东西都有机会配置任何依赖管理。尝试通过在 Configuration

上使用 beforeResolve 来推迟该逻辑