- 获取项目buildscript中依赖的classpath
project.rootProject.buildscript.configurations.each {
if (it.name == ScriptHandler.CLASSPATH_CONFIGURATION) { // classpath
it.resolvedConfiguration.firstLevelModuleDependencies.each {
println("${it.moduleGroup}:${it.moduleName}:${it.moduleVersion}")
}
}
}
- 获取project下所有dependencies
project.configurations.each {
Configuration conf ->
Logger.info("当前:Configuration--->" + conf.name)
if (conf.name.toLowerCase().contains(classpath)) {
Map<String, List<MoonDependency>> listMap = new HashMap<>()
// 获取所有依赖信息
conf.incoming.resolutionResult.root.dependencies.each { dr ->
resolveDependencies(dr, listMap)
}
}
}
static Map<String, List<MoonDependency>>
resolveDependencies(DependencyResult dr, Map<String, List<MoonDependency>> listMap) {
def depName = dr.requested.displayName
if (dr == null || depName.contains("project")) {
Logger.warn("使用project依赖--->" + depName)
return
}
String[] depSplit = depName.split(":")
if (depSplit.length < 3) {
Logger.warn("本地依赖--->" + depName)
return
}
MoonDependency dependency = new MoonDependency()
dependency.groupId = depSplit[0]
dependency.artifactId = depSplit[1]
dependency.version = depSplit[2]
if (depSplit[2].contains("{strictly")) {
dependency.isReally = true
dependency.version = depSplit[2]
.replace("{strictly", "")
.replace("}", "")
.replace(" ", "")
}
def name = depSplit[0] + ":" + depSplit[1]
def list = listMap.get(name)
if (list == null) {
list = new ArrayList<MoonDependency>()
listMap.put(name, list)
}
list.add(dependency)
return listMap
}
class MoonDependency {
String groupId
String artifactId
String version
boolean isReally
}