I want to define two different versions of the bootRun task in a Spring boot application using Gradle. This is my attempt, which worked for customizing a single bootRun task. But with multiple tasks containing bootRun, the last bootRun overrides the previous ones.
task local {
bootRun {
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
}
local.finalizedBy bootRun
task prod {
bootRun {
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
}
prod.finalizedBy bootRun
Here, when I run ./gradlew :local, the spring profiles are only postgres. When I comment out the prod task, I get both dev,postgres as expected.
As currently written, your local and prod tasks are default tasks that aren't really doing anything. Your use of bootRun within their configuration is altering the configuration of the existing bootRun task that Spring Boot's Gradle plugin defines.
When you define the tasks, you need to tell Gradle that they are a BootRun task:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
// …
}
You also need to configure the main class name and the classpath of your new task. You probably want those to be the same as the default bootRun task:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
}
You can then customize the system properties and JVM arguments as you were before. Putting this all together, you can configure your local and prod tasks like this:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
task prod(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
Related
I need to open some javafx modules and I'm told to put it in the build.gradle.kts. I have no idea how to add jvmArgs to the run task with the kotlin DSL? Can someone show me how please.
You can configure default JVM arguments with JavaApplication extension:
plugins { application }
...
application {
applicationDefaultJvmArgs = listOf("-Xmx2048m")
}
How can I change the name of war?
I already tried (I found these params in documentation https://docs.gradle.org/4.10.2/dsl/org.gradle.api.tasks.bundling.War.html)
war {
baseName = 'service'
archiveName 'service.war'
}
However, this is not working. I am still getting a name with a snapshot version.
./build/libs/search-0.0.1-SNAPSHOT.war
I am using Gradle 4.10 and Spring Boot 2.1.2.RELEASE.
Please refer to this documentation : Spring Boot Gradle reference
To sum up: when applying the Spring Boot gradle plugin together with war plugin, the war task is disabled by default and "replaced" by the SpringBoot bootWar task.
So if you want to configure the war artefact, you will need to configure the bootWar task instead of base war task :
bootWar {
baseName = 'service'
archiveName 'service.war'
}
Additional notes:
in Gradle 5.x , archiveName has been deprecated, and you should use archiveFileName instead
if you set the archiveName property you don't need to set baseName property
M. Ricciuti answer is correct, with the caveat that even though archiveName has been deprecated in Gradle 5.x, Spring Boot is still using it in 2.1.6.RELEASE. For example, if the bootWar task was configured with the new archiveFileName property like this:
bootWar {
archiveFileName 'service.war'
}
you will get this error:
Could not find method archiveFileName() for arguments [service.war] on task ':bootWar' of type org.springframework.boot.gradle.tasks.bundling.BootWar.
Use archiveName for now. See Spring BootWar class Java doc for details. They may add archiveFileName in the future.
bootWar {
archiveFileName.set 'service.war'
}
codemule - answer is correct, with a different error message when archiveFileName was used in bootWar task. I am using Gradle 6.5.1.
With the below bootWar task
bootWar {
archiveFileName 'bst.war'
}
I got the error message
A problem occurred evaluating root project 'example'.
> No signature of method: build_1z1dezuc71i4g9wsz51um0q6o.bootWar() is applicable for argument types: (build_1z1dezuc71i4g9wsz51um0q6o$_run_closure1) values: [build_1z1dezuc71i4g9wsz51um0q6o$_run_closure1#7abe6e]
It disappeared when bootWar task was configured with archiveName
bootWar {
archiveName 'example.war'
}
I have created liquibase changesets, in the Spring boot application. If I execute bootRun task then changesets get executed however, if I run bootRepackage changesets are not executed.
What additional configuration I need to do in order to run changesets with bootRepackage.
I suggest that you add the task tree plugin to your script
plugins {
id "com.dorongold.task-tree" version "1.3"
}
And then run
gradle bootRun bootRepackage taskTree
You should then see which tasks are run by the bootRun task which are not run by the bootRepackage task.
You'd then likely add a task dependency in build.gradle
bootRepackage {
dependsOn someLiquibaseTask
}
Trying to create a small custom gradle task for Spring Boot that originally looks like this:
gradle bootRun --debug-jvm
The task should look like this: gradle debugRun
I tried this but it does not work:
task debugRun(dependsOn: 'bootRun') << {
applicationDefaultJvmArgs = ['--debug-jvm']
}
How can I pass this debug-flag to the bootRun task?
It isn't sufficient for your debug run task to depend on the bootRun task. It needs to modify the existing bootRun task to enable debugging. You can do that by checking for the debugRun task in Gradle's task graph. If it's there, you set the bootRun task's debug property to true:
task debugRun(dependsOn:bootRun) {
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(debugRun)) {
bootRun {
debug = true
}
}
}
}
I created a Gradle project in NetBeans (8.0.2), which uses database connections from my tnsnames.ora. So I added the line
applicationDefaultJvmArgs = ["-Doracle.net.tns_admin=${System.env.TNS_ADMIN}"]
to my build.gradle. (I use the java and application plugins)
This works when I run the project, but not for debugging. It looks like this setting is simply not used in debug mode.
How can I get this to run? Either in the build.gradle or in the NetBeans settings would be fine.
Here's how I got around this problem.
task(debug, dependsOn: 'classes', type: JavaExec)
{
main = mainClass
classpath = sourceSets.main.runtimeClasspath
debug true
jvmArgs = ["-DmyProperty=myValue"]
}