Bind Spring Boot application name to rootProject.name from Gradle? - spring-boot

I would like to set the application name in Spring Boot to the value from rootProject.name. Is there a way to do that?

Spring Boot reads the application name from the configuration property spring.application.name. Gradle can generate a configuration file containing that property.
Put in the src/main/resources/application.properties file:
spring.application.name=${rootProject.name}
Add the Gradle task:
processResources {
filesMatching("application.properties") {
expand project.properties
}
}

Related

Generate MANIFEST.MF using Spring Boot and Gradle

I need to add the following headers to the MANIFEST.MF file using Gradle and Spring Boot:
Name
Specification-Title
Specification-Version
Specification-Vendor
Is there a way to configure the Gradle build process to read these values from settings.gradle and build.gradle and write them to the MANIFEST.MF file?
Writing them to the manifest file is described here: add manifest file to jar with gradle
To populate these values from settings.gradle and gradle.properties, consider the following:
build.gradle
plugins {
id "idea"
id "java"
}
jar {
manifest {
attributes 'Specification-Title': gradle.ext.jonesVar
attributes 'Specification-Version': bibibi
}
}
settings.gradle
gradle.ext.jonesVar = "jones"
gradle.properties
bibibi=3
The result from ./gradlew clean build gives build/libs/tmp.jar containing this manifest:
MANIFEST.MF
Manifest-Version: 1.0
Specification-Title: jones
Specification-Version: 3
If you need to do this for the bootJar, use bootJar { instead of jar { in your build.gradle.

Changing Spring Boot application version through gradle.properties while using Spring Boot gradle-plugin

I'm using the Spring boot gradle plugin and have put this in my application.yml:
spring:
application:
name: myapp
version: ${version}
I process the resource file for tokens using:
processResources {
filesMatching('application.yml') {
expand(project.properties)
}
}
And I use this annotation in my controller:
public class MyController {
#Value("${spring.application.version}")
private String appversion
...
And it all works, yay! The problem is that I can't figure out what is actually controlling the version because this reports version 0.0.1-SNAPSHOT even though I specify a different version in gradle.properties.
I tried updating the springBoot DSL thusly:
springBoot {
buildInfo {
properties {
version = "${project.version}"
}
}
}
But it has no effect. Could someone help me understand the proper way to increment/manage the version using the Spring Boot gradle plugin?
As said in my comment, the way you have set the version in gradle.properties and how you configured the application.yml is correct.
I guess you still have a version property defined in your main build.gradle, which overrides the value set in the gradle.properties with value 0.0.1-SNAPSHOT

Inject Gradle properties into Spring Boot application.yml, not working in IntelliJ IDEA

I've managed to inject the Gradle proj.ver into application.yml and after that injected it into service application.
My application.yml looks like this:
project:
version: ${version}
But it works only if I started the app from cli with:
gradle bootRun
If I'm trying to start the app from IntelliJ, it didn't work and it failed with:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"
I read all the answers from Stackoverflow and they suggested two solutions:
Use spring profiles
Modify run configuration and run before launch the gradle task: processResources
I'd prefer something like a default value for proj.ver when I'm running from IntelliJ. Is that possible? Or are any better solutions for this situation ?
Thanks
As M. Deinum said above in the comment, I managed to run the app from IntelliJ, but now the gradle bootRun started to fail with:
Caused by: groovy.lang.MissingPropertyException: No such property: unknown for class: SimpleTemplateScript2
After some more research it seems that ${version?:unknown}(with the question mark) it works either from the IDE or from cli.
I've updated the response, in order for others to know how to inject Gradle build info into Spring-boot:
1) Tell Gradle to pass the build data towards a Spring yml file like this:
processResources {
filesMatching('appInfo.yml') {
expand(project.properties)
}}
2) The appInfo.yml will look like:
project:
version: ${version?:unknown}
3) Inject the version of the build into Spring services like:
#Value("${project.version}")
private String applicationVersion;
Just to complete for Kotlin user, what works for me was :
build.gradle.kts
tasks.processResources { filesMatching("**/application.properties") { expand(project.properties) } }
application.properties
project.version= ${version}
Service.kt
#Value("\${project.version}") lateinit var version: String

How to specify the Launcher in Spring Boot Gradle?

There three launcher in Spring Boot: JarLauncher/PropertiesLauncher/WarLauncher.
For executable jar, JarLauncher will be used by default. Now I want to use PropertiesLauncher instead so that I could use external classpath. How could I specify that is spring boot gradle plugin?
Accoring to D3.1 of this doc D.3.1 Launcher manifest, I can specify the main class in MANIFEST.MF like this:
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.mycompany.project.MyApplication
However, in Spring Boot Gradle, the following code snippet actually specifies the Start-Class, not the Main-Class:
springBoot {
mainClass = "com.sybercare.HealthServiceApplication"
}
Do I need to create the MANIFIEST.MF manually or there is a better way to do this?
Thanks!
Add the layout property:
springBoot{
mainClass = "com.sybercare.HealthServiceApplication"
layout = "ZIP"
}
layout = ZIP Triggers SpringBoot to use the PropertiesLauncher
The layout property defaults to a guess based on the archive type (jar or war). For the PropertiesLauncher the layout is ZIP (even though the output might be a jar file).
https://docs.spring.io/autorepo/docs/spring-boot/1.2.0.M2/maven-plugin/usage.html
The other answers are outdated now. The current answer seems to be:
tasks.getByName<BootJar>("bootJar") {
manifest {
attributes("Main-Class" to "org.springframework.boot.loader.PropertiesLauncher")
}
}
as per https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable.configuring.properties-launcher

Spring Boot: Publish Thymeleaf template without restarting the server

Is there a way to publish Thymeleaf template without running and building the war file?
This is how my gradle file looks:
apply plugin: 'war'
war {
baseName = 'bootBlog'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("junit:junit")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
The way thymeleaf works is by caching all thymeleaf templates as the server is booting up. This is the reason you are not getting the latest template. To stop the caching, there is an application setting that is in the application.properties called:
spring.thymeleaf.cache=false
Turning this off prevents caching and allows the templates to be refreshed without restarting the server.
Once you entered in the configuration, stop the server and start it with gradle bootRun. From now on you will be able to get the latest thymeleaf templates without a refresh.
If you still want to use Springboot rather than Gradle, you can add two more properties in your properties file :
project.base-dir (it is not a springboot known property, see it as a
variable to define the path to your project)
spring.thymeleaf.prefix
To summerize, your properties file should contain these properties :
project.base-dir=file:///path/to/your/project/base/dir
spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/
spring.thymeleaf.cache=false

Resources