Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional logic in gradle build to only execute certain code when a specific task is running

So, I am using a plugin in my gradle build (the plugin is org.flywaydb.flyway but that is not really relevant). I want to validate the caller has passed in a runtime parameter when tasks from this plugin are executing but not when other tasks are executing.

I pass options to the flyway plugin based on a supplied parameter. I want an error to be returned when a flywayTask is being executed and no parameter is supplied. When a non-flyway task is being run, I do not want to validate if the parameter is supplied.

gradle -PmyParam=myValue flywayMigration

=> should run code and there should be no error

gradle flywayMigration

=> should run code and should produce error (as no parameter supplied)

gradle jar

=> should not run code and no error should be produced

I have been reading about gradle configuration and execution which is fine but I still can't find a way to only run the code when the flyway plugin is bveing executed OR specific flyway tasks are being executed.

This is my current code:

if(gradle.taskGraph.hasTask("flywayMigrate")) {
    flyway {

        def dbCode, dbUser, dbPassword, dbUrl

        if (!project.hasProperty("db_env")) {
            throw new GradleException("Expected db_env property to be supplied for migration task. Can be passed" +
                    " at command line e.g. [gradle -Pdb_env=ex1 flywayMigrate]")

        } else {
            // do stuff

        }

        user = balh
        password = blah
        url = blah
        driver = 'oracle.jdbc.OracleDriver'
        cleanDisabled = true
        baselineOnMigrate = true
        baselineVersion = '1.0.0'

    }
}

To be clear, I only want this code:

if (!project.hasProperty("db_env")

to run for flyway tasks.

The code above throws this error:

Task information is not available, as this task execution graph has not been populated.

I've tried a few things here, any advice would be appreciated.

like image 761
CodeClimber Avatar asked Nov 24 '16 17:11

CodeClimber


People also ask

How do I exclude tasks in Gradle?

You can exclude a task from being executed using the -x or --exclude-task command-line option and providing the name of the task to exclude.

Which task is executed to use Gradle build in plugin?

You can simply execute the task named init in the directory where you would like to create the Gradle build. There is no need to create a “stub” build. gradle file in order to apply the plugin. The Build Init plugin also uses the wrapper task to generate the Gradle Wrapper files for the build.


1 Answers

It's not really clear to me, what exactly do you want to do in case if this property is provided, but I think, you can do it without accesing task graph, just try to use doFirst Closure of the flywayMigrate task. Just something like this:

flywayMigrate.doFirst {
    if(!project.hasProperty("db_env")) {
        throw ...
    } else {
        //Do something
    }
}

And leave your plugin configuration free of any additional logic.

As for exception, have you tried to wait until graph is ready? It's usualy done as follows:

gradle.taskGraph.whenReady {taskGraph ->
    if(gradle.taskGraph.hasTask("flywayMigrate")) {
        ...
    }
}

Update: to answer the question from the comments

if I can attach doFirst to multiple tasks?

Yes, you can use somthing like:

//declare task names
def names = ["taskA", "taskB", "taskC"]
tasks.findAll {it ->
    //filter tasks with names       
    if (it.name in names)
        return it
}.each { it -> 
    //add some extra logic to it's doFirst closure
    it.doFirst {
        println 'hello'
    }
}

Just check, that all the tasks are exists before this configuration.

like image 62
Stanislav Avatar answered Oct 06 '22 23:10

Stanislav