https://kotlinlang.org logo
Title
r

rjhdby

12/04/2018, 2:10 PM
Hello. I'm sorry fo stupid question, but... How in gradle kotlin dsl I can add task for copy file from one location to other? (Unfortunatly, I can't understand this example https://github.com/gradle/kotlin-dsl/blob/master/samples/copy/build.gradle.kts)
t

Torbilicious

12/04/2018, 3:13 PM
What about the given sample don't you understand?
a very simple example would be
tasks.create<Copy>("copyStuff") {
    from("/some/source")
    into("/some/destination")
}
Is that what you are looking for?
or in the syntax from the example:
tasks {
    register("copyStuff", Copy::class) {
        from("/some/source")
        into("/some/destination")
    }
}
g

gildor

12/04/2018, 4:03 PM
register<Copy> also will work on Gradle 5 Also register is lazy API, create is eager
r

rjhdby

12/05/2018, 8:19 AM
@Torbilicious @gildor Both variants does not work for me.
tasks{
    create<Copy>("copyStuff") {
        from(".")
        into("out/production")
        include("properties/monportalCache.properties")
    }
    register("copyStuff", Copy::class) {
        from(".")
        into("out/production")
        include("properties/monportalCache.properties")
    }
    copy {
        from(".")
        into("out/production")
        include("properties/monportalCache.properties")
    }
}
First and second does not work. Third is only working variant. Where I'm wrong?
g

gildor

12/05/2018, 8:26 AM
@rjhdby All 3 work for me with Gradle 5. Did you tried to build it? Maybe just some IDE issue. Also, do you try to use those APIs in build.gradle.kts or in some custom plugin?
Also 3rd is actually not a task (so can be moved out off
tasks
block), this is project’s method, part of project evaluation lifecycle https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:copy(org.gradle.api.Action)
r

rjhdby

12/05/2018, 8:43 AM
@gildor In fact, if I use
copy
method then inside
into
directory I see
properties/blabla
, if I use
register
or
create
- I don't see anything
g

gildor

12/05/2018, 8:44 AM
@rjhdby Do you run task
copyStuff
?
because project.copy invokes by default, tasks are not and you have to run them explicitly or add as dependency to some other task in a project graph
Just checked, if I run
copyStuff
task I see output
Also, side point to be clear and point again that there is no difference in syntax between
create
and
register
task builders, both support
register("copyStuff", Copy::class)
and
register<Copy>("copyStuff")
syntax, just second one is more idiomatic and Kotlin friendly IMO, difference only about eager/lazy task creation https://docs.gradle.org/current/userguide/task_configuration_avoidance.html
r

rjhdby

12/05/2018, 10:56 AM
@gildor You are right. I do not execute copy task at all. 🤦‍♂️ Thank you!