我正在编写用于检查依赖关系的自定义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依赖关系管理插件中的dependencyManagement任务可以正常工作。
谢谢
这听起来像是在任何东西都没有机会配置任何依赖关系管理之前就调用了getManagedVersions()
。尝试通过在Configuration
上使用beforeResolve
来推迟该逻辑