Home > Blockchain >  Ensuring a task is always re-run when any of its dependents are not UP-TO-DATE in gradle
Ensuring a task is always re-run when any of its dependents are not UP-TO-DATE in gradle

Time:01-29

I have a multi-project gradle repo with separate projects for handing database migrations (schema) and code generation (codegen). The schema project has a :migrate task for applying migrations to a database, and the codegen project has a :generate task, which depends on a migrated database, to generate some java code. For the sake of example, lets say a third project exists which depends on codegen:generate, named app.

In CI, the gradle build makes use of a remote build cache. This works well in most cases. Changes within schema cause a run that looks like this:

./gradlew app:build

> schema:migrate
> codegen:generate
> app:build

SUCCESS

Changes to only app also works well:

./gradlew app:build

> schema:migrate UP-TO-DATE
> codegen:generate UP-TO-DATE
> app:build

SUCCESS

The problem exists when there are changes within the codegen project, but no changes to schema:

./gradlew app:build

> schema:migrate UP-TO-DATE
> codegen:generate
  (Error here because a migrated database does not exist in CI,
   because `:migrate` did no work)

FAILURE

How can I ensure that codegen:migrate is always re-run when there are changes to either schema or codegen?

CodePudding user response:

If migrate should run when there are codegen changes, then those are inputs.

I would merge the schema and codegen projects into one, then configure the inputs such that the files that trigger generate to run are also declared as inputs of migrate.

  • Related