Beri
Beri

Reputation: 11610

Wrong order of task execution in gradle 3.3

I want to define methods inside my script file, and use them to define build tasks for each project individual (custom library).

ext.buildDockerImage = { imageName ->

    def distDir = "${getProject().getBuildDir()}/docker"
    copy {
        from "${project.getProjectDir()}/src/docker/"
        into distDir
    }
    println 'Build docker image'
}

In my project build.gradle I have created a task:

apply plugin: "war"
apply plugin: "jacoco"

dependency {
  // all dependencies
}

task buildDocker() {
    apply from: "${project.getRootDir()}/docker/scripts.gradle"
    buildDockerImage("admin")
}

The problem is that whenever I am running gradle build, this tasks executes also:

 $ gradle build -xtest
Build docker image
# rest of build

As you can see , all I want is to create a custom library that will hold methods, used to create tasks for each project. But currently I cannot import those methods without breaking the build. Method buildDockerImage will work only after war file is added to build directory, so this task must be executed on demand only, I don't want to be included in the process all the time.

My questions:

Upvotes: 1

Views: 519

Answers (1)

Ekansh Rastogi
Ekansh Rastogi

Reputation: 2526

Your task buildDocker() defines everything in configuration phase. So when you run your gradle build this will always run.

task buildDocker() {
    apply from: "${project.getRootDir()}/docker/scripts.gradle"
    buildDockerImage("admin")
}

If you want to run this task as a standalone task, define your stuff in execution phase of the task. something like below

task buildDocker() {
  apply from: "${project.getRootDir()}/docker/scripts.gradle"
  doLast{
    buildDockerImage("admin")
  }
}

Read This article

This might help

Upvotes: 1

Related Questions