Search code examples
antgradlegradle-2

Checking inputs before running dependent tasks


I've got a Ant build script which I need to compile part of my project (it links into a third-party build system for the JavaScript libraries we use).

I want to wrap Gradle around this, so I've imported the Ant build, and I can successfully invoke the Ant targets via Gradle. I've even added input and output checking to the targets, so that they won't run if they don't need to

The Ant targets have setup work that they do - mostly importing configurations and settings. They do this via a dependency on an init target, which takes about 4-5 seconds to run. What I would like to do is prevent that init target running if the inputs on the main task have been satisfied.

Any suggestions?

Example Ant build script (build.xml):

<?xml version="1.0" encoding="utf-8"?>
<project name="MyProject" default="build">
  <target name="init" />
  <target name="build" depends="init">
    <echo message="hello" file="output.txt" />
  </target>
</project>

Example Gradle script to go with it (build.gradle):

ant.importBuild 'build.xml'

build {
  inputs.dir file('src')
  outputs.file file('output.txt')
}

Ideally, when I run gradle build, I don't want init to run if build is up-to-date.

Any suggestions?


Solution

  • The up-to-date check for build will only happen after init has run. What you can do is to declare the same inputs for init, and if it has no file outputs, outputs.upToDateWhen { true }. Perhaps this meets your needs.