Gradle应用程序插件:修改workingDir属性

Gradle应用程序插件:修改workingDir属性

问题描述:

我只是想修改程序运行的目录.通常,它是从项目根目录运行的,这使我有点恼火,因为测试程序可能会很烦人,因为我的程序会在运行该程序的位置生成文件和文件夹.

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.

JavaExec 任务具有名为 JavaExec#workingDir ,这正是我要修改为选择的其他属性的确切属性.

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.

我的问题是:如何修改gradle run任务以访问此属性?

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

您可以使用tasks.<TaskToModify>.property = YourValue访问任务的属性.

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

File#mkdirs() 调用是必需的,因为如果目录不存在,则对依赖于系统的Java可执行文件的调用将导致错误.

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.