Gradle Copy任务问题,文件重复

qf9go6mv  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(173)

build.gradle中,我有以下代码:

task copyModifiedSources(type:Copy) {
    from ('ApiClient.java.modified') {
        rename 'ApiClient.java.modified', 'ApiClient.java'
    }
    setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
    into 'build/generated/src/main/java/com/client/invoker'
}

我试图复制ApiClient.java.modified(重命名为ApiClient.java复制前)文件到build/generated/src/main/java/com/client/invoker文件夹已经有ApiClient.java文件。我使用了一个重复的策略,用ApiClient.java.modified覆盖/invoker文件夹中的APIClient.java
如果我将复制文件重命名为不在/invoker文件夹中的文件名,则它可以工作,但不使用重复的文件名。文件未成功复制。
你能帮我找出哪里出了问题吗?

Gradle -> 6.5.1
Java -> 11
pwuypxnk

pwuypxnk1#

rename与不低于from的水平相同。
下面的构建/生成内容中的最终文件示例为“override”

task setupFiles() {
    doLast {
        mkdir 'build/override' 
        mkdir 'build/gen'
        new File('build/gen/ApiClient.java').text = "orig"
        new File('build/override/ApiClient.java.modified').text = "override"
    }
}

task copyModifiedSources(type: Copy, dependsOn: setupFiles) {
    from 'build/override'
    include '*.modified'
    rename '(.*).modified', '$1'

    setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
    into 'build/gen'
}

运行gradle clean copyModifiedSources

swvgeqrz

swvgeqrz2#

我也面临同样的问题。duplicatesStrategy在从单个源目录复制文件时,未检查目标目录是否具有相同的文件。但是duplicatesStrategy会检查多个源复制到目标的文件是否相同。如果有,则它将基于策略执行操作。举个例子。

['warn', 'include', 'exclude', 'fail'].each { strategy ->
    task "copyDuplicatesStrategy${strategy.capitalize()}"(type: Copy) {

        def sourceFile = 'src/main/resources/Folder2/abc.txt'
        def destDir = 'src/main/resources/Folder1_'
        def finalOutput = "$destDir/abc.txt"

        from 'src/webroot'
        from sourceFile
        from 'src/manual'

        into "$destDir"

        // Only the value for this property differs for
        // each created task.
        duplicatesStrategy = strategy

        // Print the used duplicates strategy when
        // the task starts.
        doFirst {
            println "Copying with duplicates strategy '${strategy}'."
        }

        // Print the contents of the copied file COPY.txt.
        doLast {
            println "Contents of COPY.txt: Exist :: ${file(sourceFile).exists()}, Loc: ${file(sourceFile).getPath()}"
            println file(sourceFile).text

            println "Contents of COPY.txt: Exist :: ${file(finalOutput).exists()}, Loc: ${file(finalOutput).getPath()}"
            println file(finalOutput).text
        }
    }
}

供参考:https://blog.mrhaki.com/2015/04/gradle-goodness-handle-copying.html

相关问题