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

428 Views Asked by At

I'm writing my custom java gradle plugin for checking dependencies. Also i apply Spring Gradle Dependency Managment plugin. But i cannot take a list of managed library version from this plugin. I use the next code:

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();
                    }
            );
        }
    }
}

In my test project i use including Spring framework:

repositories {
    mavenCentral()
}

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

But after applying my task, it always returns empty map. Also if i call dependencyManagement task from io Spring Dependency Management plugin it also returns empty list. If i don't invoke etManagedVersions() from my custom plugin, dependencyManagement task from io Spring Dependency Management plugin works fine.

Thank you

1

There are 1 best solutions below

1
On BEST ANSWER

It sounds like you're calling getManagedVersions() before anything's had a chance to configure any dependency management. Try deferring that logic by using beforeResolve on the Configuration