我正在编写我的自定义 java gradle 插件来检查依赖项。我还应用 了 Spring Gradle 依赖管理插件。但是我无法从此插件中获取托管库版本的列表。我使用下一个代码:
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任务可以正常工作。
谢谢