How to execute external shell command on an appropriate level?


(Maciek Opała) #1

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?


(René Groeschke) #2

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é


(Maciek Opała) #3

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?


(Maciek Opała) #4

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