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.
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.
Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.
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.
The build directory of this project into which Gradle generates all build artifacts. 4. Contains the JAR file and configuration of the Gradle Wrapper.
The build script contains the following mistakes:
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.)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.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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With