Vaadin 23 issue with production mode using spring boot with gradle task bootJar

Viewed 325

I'm trying to create an executable jar file. For this project I am using gradle. I am building executable jar file using gradle task "bootJar". Unfortunately after starting my jar file like this: java -jar -Dvaadin.productionMode=true /Users/fluento/IdeaProjects/respinewsbackend/build/libs/respinews.jar

or:

java -jar /Users/fluento/IdeaProjects/respinewsbackend/build/libs/respinews.jar

this error occurs in console:

Caused by: java.lang.IllegalStateException: Running project in development mode with no access to folder '/home/fluento/StudioProjects/respinewsbackend'. Build project in production mode instead, see https://vaadin.com/docs/latest/flow/production/overview

As stated below I have 'spring.profiles.active=prod' in my application.properties.file.

Please tell me what is wrong here. Running from IntelliJ Idea in development mode works fine.

Error from console:

Powered by Spring Boot 2.7.0
2022-05-20 17:10:35.935 ERROR 63143 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Exception sending context initialized event to listener instance of class [com.vaadin.flow.spring.VaadinServletContextInitializer$CompositeServletContextListener]

java.lang.RuntimeException: Unable to initialize com.vaadin.flow.spring.VaadinServletContextInitializer$VaadinAppShellContextListener
    at com.vaadin.flow.spring.VaadinServletContextInitializer$FailFastServletContextListener.contextInitialized(VaadinServletContextInitializer.java:191) ~[vaadin-spring-23.0.7.jar!/:na]
    at com.vaadin.flow.spring.VaadinServletContextInitializer$CompositeServletContextListener.lambda$contextInitialized$0(VaadinServletContextInitializer.java:213) ~[vaadin-spring-23.0.7.jar!/:na]
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) ~[na:na]
    at com.vaadin.flow.spring.VaadinServletContextInitializer$CompositeServletContextListener.contextInitialized(VaadinServletContextInitializer.java:213) ~[vaadin-spring-23.0.7.jar!/:na]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4766) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:835) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:486) ~[tomcat-embed-core-9.0.63.jar!/:na]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:123) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:479) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:184) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring-context-5.3.20.jar!/:5.3.20]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at pl.respinews.jvmbackend.App$Companion.main(App.kt:50) ~[classes!/:na]
    at pl.respinews.jvmbackend.App.main(App.kt) ~[classes!/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[respinews.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[respinews.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[respinews.jar:na]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[respinews.jar:na]
Caused by: java.lang.IllegalStateException: Running project in development mode with no access to folder '/home/fluento/StudioProjects/respinewsbackend'.
Build project in production mode instead, see https://vaadin.com/docs/latest/flow/production/overview
    at com.vaadin.flow.server.startup.AbstractConfigurationFactory.verifyFolderExists(AbstractConfigurationFactory.java:254) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.server.startup.AbstractConfigurationFactory.getConfigParametersUsingTokenData(AbstractConfigurationFactory.java:117) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.server.startup.DefaultApplicationConfigurationFactory.create(DefaultApplicationConfigurationFactory.java:121) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.server.startup.ApplicationConfiguration.lambda$get$0(ApplicationConfiguration.java:67) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.server.VaadinServletContext.getAttribute(VaadinServletContext.java:73) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.server.startup.ApplicationConfiguration.get(ApplicationConfiguration.java:50) ~[flow-server-23.0.7.jar!/:23.0.7]
    at com.vaadin.flow.spring.VaadinServletContextInitializer$VaadinAppShellContextListener.failFastContextInitialized(VaadinServletContextInitializer.java:543) ~[vaadin-spring-23.0.7.jar!/:na]
    at com.vaadin.flow.spring.VaadinServletContextInitializer$FailFastServletContextListener.contextInitialized(VaadinServletContextInitializer.java:187) ~[vaadin-spring-23.0.7.jar!/:na]
    ... 50 common frames omitted

2022-05-20 17:10:35.936 ERROR 63143 --- [           main] o.apache.catalina.core.StandardContext   : One or more listeners failed to start. Full details will be found in the appropriate container log file
2022-05-20 17:10:35.937 ERROR 63143 --- [           main] o.apache.catalina.core.StandardContext   : Context [] startup failed due to previous errors
2022-05-20 17:10:36.017 ERROR 63143 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring-context-5.3.20.jar!/:5.3.20]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at pl.respinews.jvmbackend.App$Companion.main(App.kt:50) ~[classes!/:na]
    at pl.respinews.jvmbackend.App.main(App.kt) ~[classes!/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[respinews.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[respinews.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[respinews.jar:na]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[respinews.jar:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:142) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:479) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:184) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) ~[spring-boot-2.7.0.jar!/:2.7.0]
    ... 17 common frames omitted
Caused by: java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.rethrowDeferredStartupExceptions(TomcatWebServer.java:187) ~[spring-boot-2.7.0.jar!/:2.7.0]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:126) ~[spring-boot-2.7.0.jar!/:2.7.0]
    ... 22 common frames omitted

application.properties:

spring.profiles.active=prod

application-dev.properties:

server.port=8080
server.ssl.enabled=false
spring.application.name=AppName
vaadin.productionMode=false
vaadin.servlet.productionMode=false
vaadin.compatibilityMode=false

application-prod.properties:

server.port=8080
server.ssl.enabled=false
spring.application.name=AppName
vaadin.productionMode=true
vaadin.servlet.productionMode=true
vaadin.compatibilityMode=false

build.gradle:

plugins {
    id "java"
    id "application"
    id 'org.springframework.boot' version '2.7.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'com.vaadin' version '23.0.9'
    id("org.jetbrains.kotlin.jvm") version "${kotlinVersion}"
    id("org.jetbrains.kotlin.kapt") version "${kotlinVersion}"
    id("org.jetbrains.kotlin.plugin.allopen") version "${kotlinVersion}"
    id "org.jetbrains.kotlin.plugin.jpa" version "${kotlinVersion}"
}

apply plugin: 'kotlin'

group = 'pl.respinews'
version = '1.0.0'
sourceCompatibility = '11'

repositories {
    mavenCentral()
    maven {
        url "https://maven.vaadin.com/vaadin-addons"
    }
    maven { url 'https://jitpack.io' }
    jcenter()
}

ext {
    set('vaadinVersion', "23.0.9")
}


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'com.fasterxml.jackson.core'
        exclude group: 'com.fasterxml.jackson.datatype'
        exclude group: 'com.fasterxml.jackson.module'
    }
    implementation 'org.springframework.boot:spring-boot-starter-tomcat:2.6.7'
    implementation 'org.springframework.boot:spring-boot-starter-parent:2.6.7'
    implementation 'org.springframework.boot:spring-boot-starter-mail:2.6.7'
    implementation 'org.springframework.boot:spring-boot-configuration-processor:2.6.7'
    implementation 'com.vaadin:vaadin-spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-actuator:2.6.7'
    implementation 'org.springframework.boot:spring-boot-loader:2.6.7'
    implementation 'com.vaadin:vaadin-bom:23.0.9'
    runtimeOnly 'io.micrometer:micrometer-registry-prometheus'


    implementation 'com.google.code.gson:gson:2.9.0'
    implementation group: 'org.sql2o', name: 'sql2o', version: '1.6.0'
//  implementation 'org.hibernate.orm:hibernate-core:6.0.0.Final'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.6.7'
    implementation 'com.h2database:h2:2.1.212'
    implementation 'org.postgresql:postgresql:42.3.4'

    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.19'
    implementation 'de.mkammerer:argon2-jvm:2.11'
    implementation("com.rfksystems:blake2b:1.0.0")
//    implementation group: 'org.json', name: 'json', version: '20190722'
    implementation 'org.apache.tika:tika-core:2.3.0'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
    implementation group: 'commons-codec', name: 'commons-codec', version: '1.11'
    implementation group: 'commons-io', name: 'commons-io', version: '2.6'

    implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1')
    implementation('org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.6.1')
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.6.21'

    compileOnly 'org.projectlombok:lombok:1.18.24'

    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5',
            // Uncomment the next line if you want to use RSASSA-PSS (PS256, PS384, PS512) algorithms:
            //'org.bouncycastle:bcprov-jdk15on:1.60',
            'io.jsonwebtoken:jjwt-jackson:0.11.5' // or 'io.jsonwebtoken:jjwt-gson:0.11.2' for gson

    implementation 'com.google.firebase:firebase-admin:8.1.0'
    implementation 'com.sun.mail:javax.mail:1.6.2'

    //https://codecouple.pl/2017/01/07/9-spring-boot-swagger2-dokumentujemy-api/
//  implementation 'io.springfox:springfox-swagger2:3.0.0'
//  implementation 'io.springfox:springfox-swagger-ui:3.0.0'

    implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'
    implementation 'org.springdoc:springdoc-openapi-kotlin:1.6.8'

    implementation 'org.jsoup:jsoup:1.14.3'
    implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.5.0'

    implementation 'com.github.oshi:oshi-core:6.1.6'
    implementation 'org.imgscalr:imgscalr-lib:4.2'

    implementation 'org.vaadin.miki:superfields:0.13.0'
//    implementation 'org.vaadin:tinymce-for-flow:3.2.0'
    implementation 'com.wontlost:ckeditor-vaadin:2.3.4'
//    implementation 'org.vaadin.addons:vaadin-wrapper-for-tinymce:5.1'
//    implementation 'org.vaadin.klaudeta:quill-editor:1.0.1'
    implementation 'com.github.gregwhitaker:roboto-springboot:0.1.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}"
}

dependencyManagement {
    imports {
        mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
    }
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

application {
    mainClass.set("pl.respinews.jvmbackend.App")
}

vaadin {
    pnpmEnable = true

    generateBundle(true)
    optimizeBundle(true)
    vaadinPrepareFrontend
    vaadinBuildFrontend
}

jar {
//    archivesBaseName = archivesBaseName + '-' + archiveVersion
    exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
    from {
        configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

}

springBoot {
    buildInfo()
}

bootJar {
    enabled = true
    archiveFileName = 'respinews.jar'
    archiveBaseName.set('respinews')
    mainClassName = 'pl.respinews.jvmbackend.AppKt'
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}

tasks.named('test') {
    useJUnitPlatform()
}
2 Answers

As @Knoobie pointed out, you don't simply "turn on" production mode for Vaadin. You need to build the jar with the production profile instead, and then, executing it in the same way you have been doing, should work fine.

Adding the productionMode to your .gradle file should do the trick, as explained here.

vaadin {
    pnpmEnable = true
    productionMode = true
    generateBundle(true)
    optimizeBundle(true)
    vaadinPrepareFrontend
    vaadinBuildFrontend
}
Related