【问题标题】:DependencyReportTask "concatentation" with a multi gradle moduleDependencyReportTask“连接”与多gradle模块
【发布时间】:2021-12-16 20:53:46
【问题描述】:

我有一个多模块 gradle 项目。

我正在尝试为每个子项目(实际上是每个所有项目)运行 DependencyReportTask....然后连接到单个文件。

如果我这样做:

allprojects {    
    
    /* create a .txt file for each subproject (and the root project */
    task generateSingleProjectDependencies(type: DependencyReportTask) {
        outputFile = file('singleproject.dependencies.txt')
    }

}


task concatenateAllProjectsDependencyFiles {
    doLast {
        /* create a FileTree with all the subproject .txt files */
        FileTree singleProjectsFileTree = fileTree('.') {
            include 'singleproject.dependencies.txt'
        }
        if (singleProjectsFileTree.empty) {
            println 'singleProjectsFileTree was empty'
        } else {
            copy {
                println 'allprojects.dependencies.txt is being created, how many times did I fire?'
                /* write out the multiple single-file-txts into a master .txt file */
                def outputFileName = "allprojects.dependencies.txt"
                def output = new File(outputFileName)
                output.write('')
                singleProjectsFileTree.each {
                    f -> output << f.text
                }
            }
        }
    }
}

/* use dependsOn to get the sequence correct */
concatenateAllProjectsDependencyFiles.dependsOn generateSingleProjectDependencies

/* now "finalize" the over-all "build" task..with our custom dependency-all task */
build.finalizedBy concatenateAllProjectsDependencyFiles

其中“concatenateAllProjectsDependencyFiles”在“allprojects”之外,我只得到根项目的结果。

(所以这不太对):(

============

如果我将 concatenateAllProjectsDependencyFiles 移动到“allprojects”内部

allprojects {

    /* create a .txt file for each subproject (and the root project */
    task generateSingleProjectDependencies(type: DependencyReportTask) {
        outputFile = file('singleproject.dependencies.txt')
    }


    task concatenateAllProjectsDependencyFiles {
        doLast {
            /* create a FileTree with all the subproject .txt files */
            FileTree singleProjectsFileTree = fileTree('.') {
                include 'singleproject.dependencies.txt'
            }
            if (singleProjectsFileTree.empty) {
                println 'singleProjectsFileTree was empty'
            } else {
                copy {
                    println 'allprojects.dependencies.txt is being created, how many times did I fire?'
                    /* write out the multiple single-file-txts into a master .txt file */
                    def outputFileName = "allprojects.dependencies.txt"
                    def output = new File(outputFileName)
                    output.write('')
                    singleProjectsFileTree.each {
                        f -> output << f.text
                    }
                }
            }
        }
    }

/* use dependsOn to get the sequence correct */
    concatenateAllProjectsDependencyFiles.dependsOn generateSingleProjectDependencies

/* now "finalize" the over-all "build" task..with our custom dependency-all task */
    build.finalizedBy concatenateAllProjectsDependencyFiles



}

我明白了

allprojects.dependencies.txt is being created, how many times did I fire?

显示 N+1 次(N 表示子项目数加上一个根项目)。

(所以这不太对):(

这不仅仅是触发 N+1 次,我得到了好坏参半的结果....可能是创建 singleproject.dependencies.txt 的线程竞争条件。

如何让 concatenateAllProjectsDependencyFiles 只触发一次,并“找到”所有子项目的“singleproject.dependencies.txt”文件?

【问题讨论】:

    标签: gradle build.gradle gradlew


    【解决方案1】:

    我想通了。这需要一点巫术。

    这个答案给出了最重要的提示:

    https://discuss.gradle.org/t/task-depends-on-subproject-tasks/15893

    我将发布答案(来自上述 gradle.org)以避免 d3adlink 综合症。

    Well you could use a little Groovy syntax sugar to make it prettier like so:
    
    task publishAndSayHi {
        subprojects.each { dependsOn("${it.name}:publishToMavenLocal") }
        dependsOn('sayHi')
    }
    Alternatively, if you don’t want to do this for all subprojects you can just define an Array.
    

    现在,我的“它有效”代码如下。

    allprojects {
    
        /* other stuff here */
    
        /* define a task that will create a .txt file for each subproject (and the root project) */
        task generateSingleProjectDependencies(type: DependencyReportTask) {
            outputFile = file('singleproject.dependencies.txt')
        }
    
    }
    
    task concatenateAllProjectsDependencyFiles {
    
        /* call the root project task */
        dependsOn("generateSingleProjectDependencies")
    
        /* call each subproject generateSingleProjectDependencies manually.  since 'empty-folders' show up in the allproject list, use a "scr" folder exists trick */
        subprojects.findAll { new File(it.projectDir, "src").exists() }.each { dependsOn("${it.path}:generateSingleProjectDependencies") }
    
        doLast {
    
            /* create a FileTree with all the subproject .txt files */
            FileTree singleProjectsFileTree = fileTree('.') {
                include '**/singleproject.dependencies.txt'
            }
    
            println "singleProjectsFileTree print each item START"
            singleProjectsFileTree.each { println "singleProjectsFileTree.each::::::>>>>>>'${it.path}'" }
            println "singleProjectsFileTree print each item END"
    
            if (singleProjectsFileTree.empty) {
                println 'singleProjectsFileTree was empty'
            } else {
    
                copy {
                    println 'allprojects.dependencies.txt is being created.'
                    /* write out the multiple single-file-txts into a master .txt file */
                    def outputFileName = "allprojects.dependencies.txt"
                    def output = new File(outputFileName)
                    output.write('')
                    singleProjectsFileTree.each {
                        f -> output << f.text
                    }
                }
            }
        }
    }
    
    
    /* now "finalize" the over-all "build" task..with our custom dependency-all task */
    build.finalizedBy concatenateAllProjectsDependencyFiles
    

    也许这对将来的某人有帮助。

    但现在所有 N+1(子项目数 = N 和一个根模块/项目)都被写出。

    关键词

    分级 依赖列表报告 根任务依赖于 allprojects 子项目任务 子项目包含空文件夹

    其他有用的答案:

    How do I concatenate multiple files in Gradle?

    How to make tasks execute one after another?

    https://discuss.gradle.org/t/i-want-to-copy-the-output-from-gradlew-dependencies-into-a-file-at-build-time/21981

    https://github.com/kensipe/gradle-samples/blob/master/dot-report/build.gradle

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多