How to build a WAR file with gradle?

As rightly said by @M.Ricciuti, the spring boot gradle plugin will disable the jar/war tasks and would only work with bootJar/bootWar tasks. But if you still want your project to be packaged with jar/war tasks just add the below to your build.gradle file

war {
    enabled=true
}

This would enable the gradle war command to generate the war for your project.


I guess that you have applied the spring boot gradle plugin to your project, in addition to the war plugin ? then this behaviour is normal, since the Spring Boot plugin will disable jar and war tasks and replace these with bootWar and bootJar tasks .

With both spring boot and war plugin applied:

./gradlew war
15:35:09: Executing task 'war'...

> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :war SKIPPED

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 up-to-date
15:35:10: Task execution finished 'war'.

Note the SKIPPED message

$ ./gradlew bootWar
15:36:35: Executing task 'bootWar'...

> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :bootWar

BUILD SUCCESSFUL in 1s
3 actionable tasks: 1 executed, 2 up-to-date
15:36:37: Task execution finished 'bootWar'.

Then you will get the expected war file under build/libs.

You can still re-enable the standard jar/war tasks as explained here : https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-wars-deployable (if you need to produce normal archives and not executable archives)

Regarding the Tomcat issue: install Tomcat 8.5.


Please read: https://docs.gradle.org/current/userguide/war_plugin.html

If using Gradle with IntelliJ, goto build.gradle (or build.gradle.kts for Kotlin) and add

id 'war'

(or just

war

for Kotlin ) under Plugins

Reload Gradle Project and then use gradlew bootWar on the Intellij Terminal.

Add --info or --stackTrace for debugging

Tags:

Gradle