Can't add "dependsOn" dependency to existing "cpp" task


(Steven Emmerson) #1

I can’t add a “dependsOn” dependency to an existing task from the “cpp” plugin. The gradle build-file

apply plugin: ‘cpp’

task checkExpat(type: Exec) {

ignoreExitValue = true

standardOutput = new ByteArrayOutputStream()

commandLine ‘yum’, ‘info’, ‘expat-devel’

}

task ensureExpat(type: Exec, dependsOn: ‘checkExpat’) {

onlyIf { checkExpat.execResult.getExitValue() != 0 }

commandLine ‘yum’, ‘install’, ‘expat-devel’

}

sources {

lib {

c {

source { srcDirs = [“src/lib”] }

}

}

}

libraries {

udunits2 {

source sources.lib

}

}

udunits2SharedLibrary.dependsOn ensureExpat

results in the following:

~/udunits2: gradle dependencies

FAILURE: Build failed with an exception.

  • Where:

Build file ‘/home/steve/udunits2/problem.gradle’ line: 23

  • What went wrong:

A problem occurred evaluating root project ‘udunits2’.

Could not find property ‘udunits2SharedLibrary’ on root project ‘udunits2’.

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.852 secs

even though “udunits2SharedLibrary” is a valid task according to the output of ‘gradle tasks’


(Peter Niederwieser) #2

Is this a multi-project build? In that case, the task might belong to a different project. Another thing you could try is to wrap the line with ‘gradle.projectsEvaluated { … }’.


(Steven Emmerson) #3

No. It’s a single-project build. If it matters, I’m using a nightly-build from last week because version 1.7 doesn’t support C sources, apparently.


(René Groeschke) #4

Hello Steve, the task ‘udunits2SharedLibrary’ is created after the source section is evaluated. This evaluation is done after the whole build script is parsed. At the moment there is no other way than deferring the task dependency setup by using an afterEvaluate hook:

afterEvaluate{
 udunits2SharedLibrary.dependsOn ensureExpat
 }

cheers, René