How to execute external shell command on an appropriate level?

Hi, I’ve the following project structure

  • monitoring

  • client

  • server

In client’s build.gradle I’ve the following piece of code:

task compileCoffee << {

def proc = ‘coffee -c -o js src/main/coffee’.execute()

proc.in.eachLine {line -> println line}

proc.err.eachLine {line -> println 'ERROR: ’ + line}

proc.waitFor() }

war.dependsOn(compileCoffee)

When I run client’s build.gradle everything works fine, but when I execute monitoring’s (root) build.gardle I obtain the following exception (error):

ERROR: File not found: src/main/coffee.coffee

Of course coffee sources are present under src/main/coffee and the reason is probably that command from compileCoffee is run on monitoring’s level. How to solve this problem? How to force the coffee command to be run on an appropriate level?

Hello Opal, you can change the commandline to use absolute paths:

task compileCoffee << {
     def proc = "coffee -c -o js ${file('src/main/coffee').absolutepath}".execute()
     proc.in.eachLine {line -> println line}
     proc.err.eachLine {line -> println 'ERROR: ' + line}
     proc.waitFor()
 }

this should do the trick. The file method always resolves the path relative to the current project directory.

regards, René

Workaround:

task compileCoffee << {

def user_dir = System.properties[‘user.dir’]

if (!user_dir.endsWith(‘client’)) {

user_dir += ‘/client’

}

def proc = ‘coffee -c -o js src/main/coffee/’.execute(null, new File(user_dir))

proc.in.eachLine {line -> println line}

proc.err.eachLine {line -> println 'ERROR: ’ + line}

proc.waitFor()

}

Any better idea?

Thank You Rene, will try later. Hope will help.