jean
jean

Reputation: 2990

gradle copy file task not working in build

I am new to gradle, I want copy the jar file generated by gradlew build to another dir.

task myCopyTask(type: Copy) {
    from "build/libs/gs.jar"
    into "D:/bin/gs"
}

I add above task to the build.gradle which belong to gs module which will generate gs.jar.

The problem is the command gradlew build will not do the copy and this task indeed executed(I add println in myCopyTask). However, the command gradlew myCopyTask works.

First I thought maybe the copy task running too early, so I change it to

task myCopyTask(type: Copy) {
    doLast {
        from "build/libs/gs.jar"
        into "D:/bin/gs"
    }
}

This is not working even by gradlew myCopyTask. Only first version can work by command gradlew myCopyTask, the terminal will show: 1 actionable task: 1 executed

What is the problem?

Upvotes: 2

Views: 2494

Answers (1)

lance-java
lance-java

Reputation: 28071

You haven't wired the task into Gradle's DAG so currently it will only executed when you do gradlew myCopyTask

You'll probably do something like

apply plugin: 'base' // adds build and assemble lifecycle tasks
task myJarTask(type:Jar) {...} 
task myCopyTask(type: Copy) {
    dependsOn myJarTask
    ... 
} 
assemble.dependsOn myCopyTask

See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

Upvotes: 2

Related Questions