Search code examples
javagradlegradlew

Gradle application plugin: Modify workingDir property


I simply want to modify the directory where the program is run. Normally, it's run from the project root, which annoys me a little bit, because testing the program out can be quite annoying, since my program generates files and folders where it is being run.

A JavaExec task has a property called JavaExec#workingDir, which would be this exact property I wanted to modify to something different of my choice.

My question is: How do I modify the gradle run task in order to access this property?


Solution

  • You can access a property of a task by using tasks.<TaskToModify>.property = YourValue.

    So, in this case, you would have to do this:

    File runningDir = new File('build/run/')
    runningDir.mkdirs()
    tasks.run.workingDir = runningDir
    

    The File#mkdirs() call is neccessary, since if the directories do not exist, the call to your system-dependent java executable will cause a error.