Customize working directory run gradle run task / project structure for web application

Viewed 3440

I'm working on small web app and I want to serve static files. I'm using application plugin. Part of it is distribution plugin which copies src/main/dist files into distribution zip. Running application with gradlew run uses project dir as working directory. For this reason static files are not found.

Is there a simple way to change working directory for run task?

Is there any other recommended structure for web app gradle project setup?

2 Answers

You need to add the following to your build.gradle file to modify the working directory of a gradle task.

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

Check this thread for reference.

I have had a similar case when running './gradlew run' of a Kotlin (console) app. The default working directory is the application directory, but IntelliJ uses the root project directory by default.

I have managed to change the working directory by setting the workingDir property of the run task. The list of properties can be found on https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html

application {
    mainClass.set("myproject.HelloKt")

    // Change the working directory for the run task. Here is an example:
    tasks.run.get().workingDir = rootProject.projectDir
}
Related