How to build different versions of the same application in Gradle

(Robert Stroud) #1


I am relatively new to Gradle and would appreciate some advice on the best way to organise my Gradle build file to allow different versions of my application to be built.

Specifically, I am using Gradle to build a Grails 3 application, which results in a Spring Boot WAR file. This is all working fine, but I would like to add two features to the build file:

  1. I would like to build two different versions of the WAR file - with and without an embedded Tomcat server. The difference is whether the relevant dependency is labelled as compile or provided.

  2. My application uses a 3rd party library, which has a dependency on another library. It turns out that this second library is only needed on Windows and not on Mac OS X, so I would like to be able to build two versions of the application, with and without this extra library.

What is the best / most idiomatic way to add these features to my build file? I’d like the solution to be declarative if possible.



(Carlo Luib-Finetti) #2

should result in a different WAR.

Windows or MAC Os X: you might import and the handle it with an external variable like here:
ext { isWindows = Os.isFamily(Os.FAMILY_WINDOWS) }

if (isWindows) {// do something}

Another possibility executing different build flows is by using a value for an ext variable on the command line:
You define ‘customer’ in a ext-Block and use it in an if-statement:
if (customer=="customer1") {}

Hope this helps.

(Robert Stroud) #3

Hello Carlo,

Thank you for your answer to my question. I should be able to use isWindows in the way that you suggest, but I don’t know how to use one build file to generate two WAR files.

I essentially need to run the assemble task twice, with two different outputs and an extra dependency for one version. Is that possible? If so, what does the syntax look like?



(Carlo Luib-Finetti) #4

As always: there are a lot of different possible solution in Gradle. Searching the web and in this forum brings you a lot of insights and code snippets. And of course, study the examples that come with Gradle.

To solve your problem, you could do something like this (the following is a bit noisy) :

ext {

war {
      // do common things here
      // then
      if (warForX) {
      if (warFoxY) {
task assembleX() {

task assembleY() {

assemble.dependsOn assembleX, assembleY