I'm using Spring Boot (2.4.2
) for a GraphQL API.
The application is run via bootRun
. Recently I have to integrate co.uzzu.dotenv.gradle
plugin in order to retrieve the value for some environment variables (from a .env
file) and provide them to the Gradle's bootRun
task:
import org.springframework.boot.gradle.tasks.run.BootRun
// ...
tasks.withType(BootRun) {
final def arguments = ["--spring.profiles.active=${ -> env.SPRING_PROFILE.value}"]
args(*arguments)
environment = [
"DATASOURCE_PASSWORD": "${ -> env.DATASOURCE_PASSWORD.value}"
]
jvmArgs = []
}
As soon as I set the environment
value, the application cannot start because I don't have access to the C:\WINDOWS
folder, and looks like somehow the default temp dir is changed into that location:
[x80486@laptop graphql-api]$ ./gradlew clean bootRun
> Configure project :
> Task :clean
> Task :generateLombokConfig UP-TO-DATE
> Task :compileJava
> Task :generateGitProperties
> Task :processResources
> Task :classes
> Task :bootRunMainClassName
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
03-25-2021 15:53:27,955 |- INFO in com.domain.Application:55 [main] - Starting Application using Java 11.0.10 on laptop with PID 6740 (C:\Users\x80486\Workshop\graphql-api\build\classes\java\main started by x80486 in C:\Users\x80486\Workshop\graphql-api)
03-25-2021 15:53:27,957 |- DEBUG in com.domain.Application:56 [main] - Running with Spring Boot v2.4.2, Spring v5.3.3
03-25-2021 15:53:27,957 |- INFO in com.domain.Application:664 [main] - The following profiles are active: sandbox
03-25-2021 15:53:31,378 |- WARN in o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext:596 [main] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to C:\WINDOWS\
03-25-2021 15:53:31,418 |- ERROR in o.s.boot.SpringApplication:856 [main] - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to C:\WINDOWS\
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144)
at com.domain.Application.main(Application.java:22)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to C:\WINDOWS\
at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.createTempDir(AbstractConfigurableWebServerFactory.java:195)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:187)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:159)
... 8 common frames omitted
Caused by: java.nio.file.AccessDeniedException: C:\WINDOWS\tomcat.9580.17466432609499016423
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:509)
at java.base/java.nio.file.Files.createDirectory(Files.java:690)
at java.base/java.nio.file.TempFileHelper.create(TempFileHelper.java:135)
at java.base/java.nio.file.TempFileHelper.createTempDirectory(TempFileHelper.java:172)
at java.base/java.nio.file.Files.createTempDirectory(Files.java:1007)
at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.createTempDir(AbstractConfigurableWebServerFactory.java:189)
... 11 common frames omitted
> Task :bootRun FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootRun'.
> Process 'command 'C:\Apps\amazon-corretto\jdk11.0.10_9\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
7 actionable tasks: 6 executed, 1 up-to-date
So I wonder if this is a Gradle or Spring Boot issue? If I don't set anything on environment
this works fine — but of course, I don't have the values from the .env
file 😅
Comment From: wilkinsona
Your use of =
means that you're calling JavaExec.setEnvironment(Map)
. You need to call environment(Map)
or environment(String, Object)
instead.