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

时间:2017-01-26 11:12:09

标签: gradle dependency-management gradle-plugin spring-io

我正在编写自定义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'
}

但在应用我的任务后,它总是返回空地图。此外,如果我从io Spring Dependency Management插件调用 dependencyManagement 任务,它也会返回空列表。 如果我不从我的自定义插件调用 etManagedVersions(),那么来自io Spring Dependency Management插件的 dependencyManagement 任务可以正常工作。

谢谢

1 个答案:

答案 0 :(得分:1)

听起来你在任何事情都有机会配置任何依赖关系管理之前都会调用getManagedVersions()。尝试使用beforeResolve

上的Configuration推迟该逻辑