Skip to main content

Gradle : How to make a custom War file

Points to Remember

  1. War task extends Jar
  2. You can create war files with any configuration defined in configurations { } closure
  3. You can also add files to an existing war file.
  4. You can select the files that needs to be included or excluded while creating a war file

How to create a War file in Gradle

To create a war file you have to create a task of type War as shown below

// include java plugin 
apply plugin : 'java'

task createWar(type : War){
    destinationDir = file("$buildDir")
    baseName = "my-war"
    version = "1.1"
    caseSensitive = true
    classifier = "SNAPSHOT"
    from "src"
}

Run the above task with command gradle -q createWar, this will create a war file named my-war-1.1-SNAPSHOT.war in the build folder.

See Full Documentation of War Task

How to create a War file and exclude some files

Now if we want to exclude some files from a war file we can use the exclude method which takes Set<String>, and excludes them from the war file. Lets see an example below

task createWar(type : War){
    destinationDir = file("$buildDir")
    baseName = "my-war"
    version = "1.1"
    caseSensitive = true
    classifier = "SNAPSHOT"
    from("src/main"){
        excludes = [
            "resources/test.txt"
        ]
    }
}

The above task will create a war file and will exclude the file test.txt from directory src/main/resources/ .

Note
  1. You can add multiple files in the exclude array to exclude multiple files
  2. You can add a directory to exclude it from war, e.g excludes = [ 'resources/**' ], this will exclude all files and folders in resources folder.

How to create a War file and include files from some other directory

You may also get a scenario when you have to include some files from some other directory to the war file. To do that you can use the includes method and define an array of files or folders to be included.

task createWar(type : War){
    destinationDir = file("$buildDir")
    baseName = "my-war"
    version = "1.1"
    caseSensitive = true
    classifier = "SNAPSHOT"
    from("src/main"){
        excludes = [
            "resources/**"
        ]
    }
    from("src/demo"){
        includes = [
             "a.txt",
             "b.txt"
        ]
    }
}

The above task will include the files from src/main folder and src/demo folder and make a war file.

Copy files to an War file at custom location

You may need to copy some files to the war file in some custom directory. Lets say, you want to copy configuration files from directory src/conf to folder /conf in the war file. Then you can write the task as shown below

task createWar(type : War){
    destinationDir = file("$buildDir")
    baseName = "my-war"
    version = "1.1"
    caseSensitive = true
    classifier = "SNAPSHOT"
    from("src/main"){
        excludes = [
            "resources/**"
        ]
    }
    from("src/conf"){
        into("/conf")
    }
}

This task will copy the files from /src/conf to the /conf folder in the war file.

Comments