Spring Boot not updating static html (with Kotlin/Intellij) - spring-boot

I built a pretty simple static content web server using Spring Boot, Kotlin and Intellij. I run my program and it properly serves my html on localhost:8080. However, if I update that html while the server is running then force reload the page in the browser my changes are never shown. I've read a bunch of things that could be the problem. Something about thymeleaf cache. Spring Boot Devtools. Intellij auto builds or something. Nothing seems to work. Maybe an extra set of eyes on my code could pick up something I'm missing.
Here's my build.gradle
buildscript {
ext.kotlin_version = "1.2.0"
ext.httpcomponents_version = "4.5.4"
ext.springframework_version = "1.5.9.RELEASE"
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springframework_version"
}
}
group "com.atomicslurry"
version "1.0"
apply plugin: "java"
apply plugin: "kotlin"
apply plugin: "spring-boot"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "org.apache.httpcomponents:httpclient:$httpcomponents_version"
compile "org.apache.httpcomponents:fluent-hc:$httpcomponents_version"
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2"
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$springframework_version"
compile "org.springframework.boot:spring-boot-devtools:$springframework_version"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Here's my kotlin app main
package com.atomicslurry.sentinel
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
#SpringBootApplication
open class App
fun main(args: Array<String>) {
SpringApplication.run(App::class.java, *args)
}
Finally, my static html is in src/main/resources/static
Any ideas?

The general workflow that is working for me:
Edit HTML
Press Build Project (Ctr+F9) in IntelliJ, then wait until changes completed
Go to Browser and refresh

add this lines to application.properties
spring.web.resources.static-locations[0]=file:./src/main/resources/public/
spring.web.resources.static-locations[1]=classpath:/public/

If you're using Spring Boot with static resources at src/main/resources/public, this can be done by add the following to application.properties:
spring.web.resources.static-locations[0]=file:src/main/resources/public/
spring.web.resources.static-locations[1]=classpath:/public/
The "file:" makes the content update on refreshing the browser,
see related issue.
Alternatively, the file resource locations can be discovered at runtime and added programmatically.
See also documentation and tutorial.

Related

Spring Boot + Kotlin + Gradle - Error: Main method not found in class

I'm learning spring boot with Kotlin (since I come from Android with Kotlin). I set it up with gradle. In my local machine everything works just fine. But I'm having a few issues while trying to deploy it to Heroku.
This is the error I'm getting:
Error: Main method not found in class com.markoid.packit.PackitApplication, please define the main method as:
2021-07-01T20:58:51.075484+00:00 app[web.1]: public static void main(String[] args)
2021-07-01T20:58:51.075581+00:00 app[web.1]: or a JavaFX application class must extend javafx.application.Application
I read on other posts that I need to add system.properties file in the root, so I did, but nothing changes.
system.properties
java.runtime.version=11
And this is my build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.10"
kotlin("plugin.spring") version "1.5.10"
}
group = "com.markoid"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
// Spring Boot Core
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-mail")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
// Joda Time library
implementation("joda-time:joda-time:2.10")
// Json Web Token
implementation("io.jsonwebtoken:jjwt-impl:0.11.1")
implementation("io.jsonwebtoken:jjwt-api:0.11.1")
implementation("io.jsonwebtoken:jjwt-jackson:0.11.1")
// Serializers
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
// Documentation
implementation("io.springfox:springfox-swagger2:2.6.1")
// Kotlin related
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Testing Frameworks
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "com.markoid.packit.PackitApplication"
}
}
My app file is as simple as this:
#SpringBootApplication
class PackitApplication
fun main(args: Array<String>) {
runApplication<PackitApplication>(*args)
}
Does someone know what I'm missing? This is the first project on spring I'm trying to deploy on heroku, so please bare with me.
Thank you in advance.
I've faced with the same issue. My problematic configuration is:
Kotlin 1.5.21
Spring boot 2.5.2
Gradle 7.1.1
The problem was that in IDEA it can be run, but cannot not using gradlew command line. I went and checked all old projects. They were built and run without any problem. The difference was in versions. All my previous project had lower versions in all positions I mentioned above. So I suggested that the problem was in the version, but what tool it was? Kotlin, Spring, Gradle? I have not yet found the guilty (and no time to go into deep of this problem now), but I found a solution.
If you open your jar file in any archiver (eg. WinRAR) and look at the MANIFEST.MD you will see a line starting with 'Start-Class', there is your main class and it must end with 'Kt' suffix, for example
Start-Class: me.sigest.fiveplus.FiveplusApplicationKt
In my failed jar it was not. To fix it I changed the code in build.gradle.kts file and set
springBoot {
mainClass.set("com.example.MyMainClassKt")
}
Despite the fact that in reality my class is called MyMainClass (without suffix Kt) and it helped
P.S. all my old boot jars contain the correct name of main class with Kt. I suppose the problem is in Gradle
I just had to a few things to make it work:
In the build.gradle.kts, I removed the tasks with type jar, and added this:
springBoot {
mainClass.set("com.markoid.packit.PackitApplicationKt")
}
I needed to add a Procfile, with the following:
web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/packit-1.0.0-SNAPSHOT.jar
Such file will tell heroku the specific command I want to be executed to run the generated jar.
Have you tried changing the main method like it is being suggested in the error message, i.e. public static void main(String[] args)

Passing correct path to a local WSDL file in wsdl2Java task in Gradle Spring Boot project

For this web project I need to generate Java classes from some WSDL files (which I have locally in my resources folder) in order to use them in my service. The final aim is to run jar file on web server but the program can't find path to WSDL file while running as a jar.
I am using wsdl2java to generate these classes and the code, where I do that in my build gradle file, looks like the following:
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.12'
}
}
plugins {
id 'org.springframework.boot' version '2.3.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
apply plugin: 'no.nils.wsdl2java'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
wsdl2java {
wsdlsToGenerate = [
['-p', 'wsdlOne', file("${projectDir}/src/main/resources/wsdl/wsdlOne.wsdl")],
['-p', 'wsdlTwo', file("${projectDir}/src/main/resources/wsdl/wsdlTwo.wsdl")],
['-p', 'wsdlThree', file("${projectDir}/src/main/resources/wsdl/wsdlThree.wsdl")],
['-p', 'wsdlFour', file("${projectDir}/src/main/resources/wsdl/wsdlFour.wsdl")],
['-p', 'wsdlFive', file("${projectDir}/src/main/resources/wsdl/wsdlFive.wsdl")],
]
}
// More lines here...
Project works fine locally, but when I run jar file on the server and test the service which uses WSDL, it crashes with the following exception:
Exception: Failed to access the WSDL at: file:/D:/projects/my_project/src/main/resources/wsdl/wsdlOne.wsdl. It failed with:
/D:/projects/my_project/src/main/resources/wsdl/wsdlOne.wsdl (No such file or directory).
I could replace local addresses of WSDL files with their real http addresses on the web, but I am required to leave them as they are now.
I researched a lot and tried all the possible solutions but I could not find any that would work for my case. I have looked thoroughly (I hope so) through these questions and answers but without results.
If useful in anything, here is the link to see the list of all the optional arguments that can be used to customize code generated from WSDL files by wsdl2Java and you can view the description of wsdl2java gradle plugin here.
I would be very grateful if anyone could give even a slightest hint about what I am missing here.

Don't know how to include joinfaces into a multi module gradle build

An old application of mine is a Java 8 Enterprise mololith and i want to migrate it.
I want to migrate it into a spring boot project including primefaces,
myfaces and omnifaces by using joinfaces and split it into smaller parts (multi-modules-project)
for better maintainance.
So i split the project into the following modules to became an working prototype:
Short description of the modules:
Common / ComDb : Here are only generated JPA entities / classed directly from the database which classes can be used by all modules
Libraries / LibPrimeFaces : Here are only the latest primefaces elite JAR's which should be used in SpringBoot
Libraries / LibPrimeFacesTheme : Here are a buyed primefaces theme which will be bundeled and included by a generated JAR.
Services / * : Each spring boot service which should be used is located here, so it can be used in the main application or from an external service over REST
Application : Here is the spring boot application which should include all of the things above - even a runnable jar-file at last
The problem now for me is the usage of gradle which is completly new for me:
I dont't know how to integrate joinfaces in combination with my buyed primefaces-8.0.5.jar from elite subscription, omnifaces 3 and myfaces by using gradle.
The most manuals are for maven, but it seems that they are not working for gradle if i
convert the scripts.
Currently the whole project compiles and start without any errors but now i am at the point where i can't find a working example of using joinfaces and gradle in a multi module environment like mine.
Here are the main gradle-scripts:
The root-scripts:
gradle.properties (Holding the versions):
VersionSpringBoot=2.3.5.RELEASE
VersionSpringDependencyManagement=1.0.10.RELEASE
VersionPrimeFaces=8.0.5
settings.gradle (includes and names):
rootProject.name = 'EcoCalcDD4Web'
include 'Common:ComDb'
include 'Libraries:LibPrimeFaces'
include 'Services:ServiceConfigWebManagement'
include 'Application'
build.gradle (Main build file):
buildscript {
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${VersionSpringBoot}")
}
}
allprojects {
group = 'com.skf.rocket'
version = '1.0.0'
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
}
subprojects {
apply plugin: 'java-library'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven {
url './Libraries/LibPrimeFaces'
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${VersionSpringBoot}")
}
}
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
}
And here the application build.gradle - file where JoinFaces should be used with my primefaces, omnifaces and myfaces.
Here i don't know what to add to get all started with joinfaces:
apply plugin: 'org.springframework.boot'
bootJar {
mainClassName = "com.skf.rocket.EcoCalcDd4WebRocket"
}
// Maven dependencies
dependencies {
// Internal dependencies
api project(':Common:ComDb')
// Implementation project('Libraries:LibPrimeFacesTheme')
api project(':Services:ServiceConfigWebManagement')
// External dependencies
// JoinFaces + MyFaces + PrimeFaces - TODO ??
// Development tools with HotReDeploy
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// Automatically configuration
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
// Lombok - Utility
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// Global cache
implementation 'org.springframework.boot:spring-boot-starter-cache'
// Data access + MS SQL driver
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
// Data validation
implementation 'org.springframework.boot:spring-boot-starter-validation'
// Web + RestService
implementation 'org.springframework.boot:spring-boot-starter-web'
// Session management
implementation 'org.springframework.session:spring-session-core'
// Boot Acturator - Application monitoring and alive checks
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
// Unit test
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
At first, you should have a look at these resources:
https://docs.gradle.org/current/userguide/intro_multi_project_builds.html
https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver
https://docs.joinfaces.org/4.3.4/reference/#_gradle
https://docs.joinfaces.org/4.3.4/reference/#_example_using_myfaces_instead_of_mojarra
https://github.com/joinfaces/joinfaces-gradle-jar-example
To get started with JoinFaces, you need a dependency on org.joinfaces:jsf-spring-boot-starter.
Because you want to use MyFaces instead of Mojarra, you have to exclude it and pull MyFaces instead.
I'd start with these dependencies:
implementation ("org.joinfaces:jsf-spring-boot-starter") {
exclude module: "mojarra-spring-boot-starter"
}
implementation "org.joinfaces:myfaces-spring-boot-starter"
implementation "org.joinfaces:omnifaces1-spring-boot-starter"

Kotlin Require Library with Gradle Build

I'm trying to add the library Exposed to my project. So, it leads me to the bintray page where it says to use compile 'org.jetbrains.exposed:exposed:0.8.5'. I open my file build.gradle and place that file into the dependencies segment:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile 'org.jetbrains.exposed:exposed:0.8.5'
}
IntelliJ auto builds it and I get the following error
Warning:root project 'DB-Table-To-Orm': Unable to build Kotlin
project configuration Details:
java.lang.reflect.InvocationTargetException: null Caused by:
org.gradle.api.artifacts.ResolveException: Could not resolve all
dependencies for configuration ':compileClasspath'. Caused by:
org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not
find org.jetbrains.exposed:exposed:0.8.5. Searched in the following
locations:
https://repo1.maven.org/maven2/org/jetbrains/exposed/exposed/0.8.5/exposed-0.8.5.pom
https://repo1.maven.org/maven2/org/jetbrains/exposed/exposed/0.8.5/exposed-0.8.5.jar
Required by:
project :
So, I look in the repo and there is no path beyond jetbrains with the exposed directory.
How do I install the Exposed library with Gradle? Do they have the path written down incorrectly? Should I put in a bug report with the project? Or am I just putting the compile statement in the wrong location?
Sorry, if this seems like a silly request, I'm new to Javaland and Kotlin and IntelliJ. Coming for the .NET world.
Update
Here's the build.gradle in its entirety:
group 'com.awebsite.db-table-to-orm'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile 'org.jetbrains.exposed:exposed:0.8.5'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
As far as I know Exposed isn't in the main bintray repo (aka jcenter). To make gradle search in Exposed's repo you need to add this:
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
to your repositories section.
Example:
repositories {
mavenCentral()
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
Then just rebuild and it should work just fine

Cannot access Guava in Android Studio

I am new to Android Studio.
I'm trying to add Guava to a module but somehow the reference is not resolved. This is my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.1'
}
}
repositories {
mavenCentral();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.5'
compile 'com.google.appengine:appengine-endpoints:1.9.5'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.5'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.googlecode.objectify:objectify:5.0.2'
compile 'com.google.guava:guava:17.0'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
endpoints {
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}
As I can see in some other posts, the main mistake that was made is the missing reference to mavenCentral() at root level. This seems to be correct here, but the following import does not work:
import com.google.guava;
"Cannot resolve symbol 'guava'."
I have re-synced the IDE, which did not help. I have also tried to refresh dependencies:
gradlew --refresh-dependencies
I have then added an older version number to see if gradle recognizes the newer version, and it does show me in gradle.build that a newer version exists.
Further, all other references resolve fine.
Anybody has an idea what's missing?
Guava functions and data structures live under the com.google.common.* package (not com.google.guava, as the maven URL would suggest).
See documentation here: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/index.html

Resources