0

In build.gradle I have the following code:

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'
}

I am trying to copy ApiClient.java.modified (renamed to ApiClient.java before copy) file into build/generated/src/main/java/com/client/invoker folder which already has ApiClient.java file. I am using a duplicate strategy to override APIClient.java from /invoker folder with ApiClient.java.modified.

If I rename the copying file to the file name not in /invoker folder, then it works but does not work with the duplicate file name. The file is not copied successfully.

Could you please help me to figure out what is going wrong?

Gradle -> 6.5.1
Java -> 11

2 Answers 2

1

the rename is same level as from not under from.

below example final file in build/gen content is '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'
}

run gradle clean copyModifiedSources

Sign up to request clarification or add additional context in comments.

Comments

0

I also faced same issue. duplicatesStrategy did not check if destination directory has the same files while copying the files from single source of directory. But duplicatesStrategy checks if multiple sources which are being copied into destination have the same files or not. If have then it will perform the action based on strategy. For example.

['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
        }
    }
}

For reference: https://blog.mrhaki.com/2015/04/gradle-goodness-handle-copying.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.