Skip to main content

Gradle : How to use Gradle Cargo Plugin to Deploy war on Tomcat

This post is based on the Gradle Cargo Plugin by bmuschko

Include the Plugin

You need to include the plugin in your build.gradle.

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3'
    }
}

apply plugin: 'com.bmuschko.cargo'

buildscript block should be the first thing in the build.gradle file.

Include the Gradle Cargo plugin dependencies

Add the following in your gradledependencies block

dependencies {
    def cargoVersion = '1.4.5'
    cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion",
            "org.codehaus.cargo:cargo-ant:$cargoVersion"
}

Define the Tomcat Container

To deploy The war files to Tomcat you need to create a dsl by name cargo and define the mandatory containerId.

Note

ContainerId will define the version of tomcat you want to use. If your want to deploy to tomcat6 your containerId will tomcat6x.

See the complete list of possible contianerId's supported


Gradle task to deploy war file on Tomcat

Below is a sample gradle task that will deploy war file from given location to Tomcat.

task deployAppA(type: com.bmuschko.gradle.cargo.tasks.remote.CargoRedeployRemote) {
    description = "Deploys WAR to Tomcat ."  // description of task 
    hostname = '127.0.0.1' // ip of the tomcat 
    port = 8080 // port at which tomcat is running
    username = "tomcat" // username of tomcat
    password = "tomcat"  // password of the tomcat
    deployables = [
            new com.bmuschko.gradle.cargo.convention.Deployable(file: file('/path/to/war/appA.war'), context: 'appA')]
}

The above will deploy your war file appA.war on Tomcat running on your local machine.

Note

Your tomcat should have manager-script role to allow the plugin to deploy war on tomcat.


Adding Manage-Script role to Tomcat

To add the role to Tomcat, go to path-to-tomcat/conf folder and replace the contents of file with the following.

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager-script"/>
  <user username="tomcat" password="tomcat" roles="tomcat,manager-script"/>

</tomcat-users>

The above will set a user tomcat with password tomcat and roles tomcat and manager-script.

Now restart your tomcat server and run the gradle task with the command

gradle -q deployAppA

This should deploy your war file to tomcat.

Comments