2
votes

I have a current setup in my build.gradle that I'm trying to understand. I need a number of tasks to go off in a very specific order and execute all with one task call. The setup I want looks like this:

1.) Run liquibase changeset into predefined database 2.) Run a number of tests against database 3.) Rollback all changes made with the previous changeset

I want the database in a 'clean' state every time I test it. It should have only the changes I expect and nothing else. The liquibase is set up with the Gradle plugin for it and the changeset is applied/updated. However, I don't want to call the command manually. This will be something that needs to run in continuous integration, so I need to script it so I simply have our CI call one task and it then runs each task, in order, until the end. I'm unsure of how to call the Gradle command-line task from inside of itself (ie inside the build.gradle file) and then also pass parameters to it (since I'll need to call some type of rollback command task to get the database to be what it was before calling the update).

Right now, all I'm doing is calling the command line tasks like this:

$ gradle update
$ gradle test
$ gradle rollbackToDate -PliquibaseCommandValue=2016-05-25

Again, I can't call them by the command line alone. I need a custom task inside Gradle so that I could just call something like:

$ gradle runDatabaseTests

...And I would have it do everything I expect.

1

1 Answers

1
votes

There is no gradle way to invoke/call a task from another task directly. What you can do instead is to use dependsOn or finalizedBy to setup task dependencies which will force the prereq tasks to run first.

If you declare a task:

task runDatabaseTests(dependsOn: [update, test, rollbackToDate]) << {
    println "I depend on update, test and rollbackToDate"
}

when you call

gradle runDatabaseTests -PliquibaseCommandValue=2016-05-25

it will force update, test and rollbackToDate first. You can control the order in which they're run, if you care about that, by using mustRunAfter and/or shouldRunAfter