0

I am using gradle 6.0.1

I am trying to write my on task, but I want first the the build task is executed but without tests.

I tried (from build.gradle):

task startEnv(type: GradleBuild) {
    tasks = ['build']
    doLast {
        // START ENV CODE
    }
}

However, I don't manage to find a way to call build without running tests, as I would run

gradle build -x test

Is it possible to achieve this functionality?

Another option I can use, is to check inside my startEnv task whether build already exists and run this task only if build exists - Is there a way to query whether build exists? (this is a multi module projects, so I am not sure it is enough to check whether build directory exists on the root project).

I followed the comments and tried the solution mentioned at Skip a task when running another task

I added to build.gradle:

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(startEnv)) {
        println("DEBUG1")
        test.enabled = false
    }
}

task startEnv(type: GradleBuild) {
    tasks = ['build']
    doLast {
       // START ENV CODE
    }
}

But when I run ./gradlew startEnv - it still fails with some tests that in current phase I know they should fail. I can see the DEBUG1 print when I execute this command but the build fails with tests that are failing.

Thank you,

user1002065
  • 595
  • 1
  • 8
  • 19
  • Does this answer your question? [Skip a task when running another task](https://stackoverflow.com/questions/40649712/skip-a-task-when-running-another-task) – tkruse Feb 29 '20 at 02:32
  • Hi, thank you, I tried that, but it does not help. I am updating my question – user1002065 Feb 29 '20 at 18:39
  • 1
    Why task `startEnv` is of type `GradleBuild` - it seems it's too heavy. Instead define a simple task and set dependencies. Also, are you sure `test.enabled` refers to a task? Please try `project.tasks.test.enabled = false`. This may make a difference. – Opal Feb 29 '20 at 20:23
  • As Opal said, just depend on build, similar to this example https://stackoverflow.com/questions/32907275/gradle-custom-task-which-runs-multiple-tasks. Note that it might be possible to depend on more specific subtasks than build, and then you might not need to exclude the test task. – tkruse Mar 01 '20 at 00:53
  • @Opal At first I did try this approach, however the build was not running. I am having a multi module project which the top build.gradle actually is not doing much, when I did that approach and I set 'dependsOn "build"' the modules (sub projects) did not build and I found on that same thread that using GradleBuild task works well. I will try now the approach with project.tasks.test.enabled and will update. Thank you – user1002065 Mar 01 '20 at 21:24
  • Hi @Opal, I tried project.tasks.test.enabled, but it didn't help, so I took on step further and tried to iterate over the subprojects and for each subproject set test.enabled = false, but it also didn't help, I still got the tests failing and failing my build fail – user1002065 Mar 01 '20 at 21:36

0 Answers0