Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle - copying directory - Issue

I have an issue with copying files in gradle. Here is a snippet of build.gradle:

task mydir() {
    new File("build/models").mkdir()
}

task copyTask(dependsOn:mydir, type: Copy){ 

   from 'src/test/groovy/models'
   into 'build/models'

   from 'src/test/groovy/test.groovy'
   into 'build/'
}

The issue here is in the first copy, that is, test.groovy is copied into build. But the first copy, where I am copying all the files in src/../models into build/models is not working. It simply copies the files into build directory only. My build/models remain empty. Can someone please tell me where what I'm doing wrong? I also have a separate task to create build/models directory prior to copyTask execution.

like image 219
user3721344 Avatar asked Jul 03 '14 05:07

user3721344


People also ask

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is Gradle doLast?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build.

What is Gradle build directory?

The build directory of this project into which Gradle generates all build artifacts. 4. Contains the JAR file and configuration of the Gradle Wrapper.


1 Answers

The build script contains the following mistakes:

  1. The directory is created when mydir is configured, not when it is executed. (This means the directory will be created on every build invocation, regardless of which tasks get executed.)
  2. new File('some/path') creates a path relative to the current working directory of the Gradle process, not a path relative to the project directory. Always use project.file() instead.
  3. A Copy task can only have a single top-level into.

Here is a corrected version:

task mydir {
    doLast {
        mkdir('build/models')
    }
}

task copyTask(dependsOn: mydir, type: Copy) {
    into 'build'
    from 'src/test/groovy/test.groovy'
    into('models') {
        from 'src/test/groovy/models'
    }
}

The Copy task will create build/models automatically, so there is likely no need to have the mydir task. It's odd to use build as the target directory of a Copy task (should use a subdirectory).

like image 170
Peter Niederwieser Avatar answered Oct 21 '22 07:10

Peter Niederwieser