# Issues using Exec task

**URL:** <https://discuss.gradle.org/t/issues-using-exec-task/5882>\
**Category:** Old Forum Archive\
**Created:** [November 28, 2014, 9:53am UTC](https://discuss.gradle.org/t/issues-using-exec-task/5882 "2014-11-28T09:53:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minal\_Lamture](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Minal\_Lamture](https://discuss.gradle.org/u/Minal_Lamture)\
**Post date:** [November 28, 2014, 9:53am UTC](https://discuss.gradle.org/t/issues-using-exec-task/5882/1 "2014-11-28T09:53:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Minal\_Lamture](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Minal\_Lamture](https://discuss.gradle.org/u/Minal_Lamture)\
**Post date:** [November 28, 2014, 12:41pm UTC](https://discuss.gradle.org/t/issues-using-exec-task/5882/2 "2014-11-28T12:41:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [November 28, 2014, 2:07pm UTC](https://discuss.gradle.org/t/issues-using-exec-task/5882/3 "2014-11-28T14:07:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Minal\_Lamture](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Minal\_Lamture](https://discuss.gradle.org/u/Minal_Lamture)\
**Post date:** [December 1, 2014, 4:40am UTC](https://discuss.gradle.org/t/issues-using-exec-task/5882/4 "2014-12-01T04:40:00Z")

</div>

Thanks Peter.

this worked…
