WillD
WillD

Reputation: 875

Is it not possible to copy from and to the Project directory using the Gradle Copy task?

Looking at a build file like:

task busted(type: Copy){
    from "${projectDir}/foo"
    into projectDir
    rename 'foo','bar'
    expand(baz:'qux')
}

task alsoBusted(type: Copy){
    from 'foo'
    into projectDir
    rename 'foo','bar'
    expand(baz:'qux')
}

task moreBusted(type: Copy){
    from projectDir
    into projectDir
    include 'foo'
    rename 'foo','bar'
    expand(baz:'qux')
}

task notBusted(type: Copy){
    from 'foo'
    into "${projectDir}/quux"
    rename 'foo','bar'
    expand(baz:'qux')
}

task surprisinglyNotBusted(type: Copy){
    from 'quux'
    into "${projectDir}/quux"
    include 'foo'
    rename 'foo','bar'
    expand(baz:'qux')
}

It seems that while it's possible to copy a file from and to the same directory (task surprisinglyNotBusted) it's not possible to copy from and to the projectDir, which throws a big fat IOException:

org.gradle.api.UncheckedIOException: java.io.IOException: The process cannot access the file because another process has locked a portion of the file

I'm trying to use Copy to do some simple templating, which may not be wise, but is there a reason this doesn't function? Is there a better way to go about it?

TIA.....

Upvotes: 1

Views: 1486

Answers (2)

VinceMok
VinceMok

Reputation: 11

Try the below

task copyFiles(){
    doLast{
         ant.copy( todir: "c:\temp\destinationfolder" ){
             fileset( dir: "c:\temp\sourcefolder" )
         }
    }

}

Upvotes: 1

Matt
Matt

Reputation: 10823

This is a bug when copying file directly into project directory. This seems to be a problem whenever task's outputs is set to project directory. For example, the following exhibits the same behavior:

task notCopyButStillBusted {
  outputs.dir projectDir

  doLast {
    println "IOException on Windows sadly"
  }
}

The only workaround I have found so far is to define your own copy behavior:

task copyWorks {
  inputs.file "/path/to/Makefile"
  outputs.dir "$projectDir/Makefile"

  doLast {
    copy {
      from "/path/to/Makefile"
      into projectDir
    }
  }
}

This works for explicit file, could be harder when copying multiple files at once.

You can vote for GRADLE-3002 to drive more attention around this bug.

Upvotes: 3

Related Questions