Gradle非常用脚本

  1. 获取项目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}")
        }
    }
}
  1. 获取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
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容