Issues using Exec task

Hi,

We have a step in our build process to generate stub CPP files with help of Java class before compiling them into a shared library.

For this I thought of using Exec task which will execute java command and generate cpp stub files and libarries cppsourceset will depend on this task.

syntax used is as below

task generateCPPStubs(dependsOn: removePrevFiles) {

doLast {

exec {

executable ${JAVA}

args ${LOGGING}

args ${ENUM}

args ‘-cp’, ${CP}

args ‘’

args ‘-v’, ${DD_VERSION}

args ‘-s’, ${SERVICES}

args ‘-i’, ${ITEMS}

args ‘-co’, ${CONTAINERS}

args ‘-c’, ${CLASS_MANAGER}

args ‘-l’, cpp

}

}

}

and then

sources {

MyLib {

generateCPPStubsOutput(CppSourceSet) {

generatedBy tasks.generateCPPStubs

}

}

}

but I am getting error

FAILURE: Build failed with an exception.

  • What went wrong: A problem occurred configuring project ‘:MY.lib’. > Exception thrown while executing model rule: org.gradle.nativebinaries.plugins.NativeComponentModelPlugin$Rules#configureGeneratedSourceSets(org.gradle.language.base.ProjectSourceSet)

Could not find property ‘sourceDir’ on task ‘:MY.lib:generateCPPStubs’.

Can you please point out what is wrong? where should I specify sourceDir property.

Hi,

Figured this out. It was issue with generatedBy.

but still I am left with one issue regarding Exec task.

task test1(type: Exec) {

commandLine ‘ls’, ‘-lrt’ }

works but if I define it like

def testexe = “”“ls”"" def testarg1 = “”"-lrt""" task test2(type: Exec) {

commandLine ‘${testexe}’, ${testarg1} }

then it does not work. gradle can not resolve variable value in this case. Can you please help me resolve this issue?

Reason I need this behavior is 1. for different environments we have different exe paths and based on various conditions we have multiple combinations of arguments as well. I want to decide arguments and executables beforehand and use just one defination of gradle exec task.

Or else I have to define multiple tasks whereas in reality there is only one command whcih will be invoked per build.

Is there any workaround for this?

String interpolation (’${}’) only works in double-quoted strings. In above case, you don’t need string interpolation, and can just do ‘commandLine testexe, testarg1’.

Thanks Peter.

this worked…