gradle: copy war to tomcat directory

44,627

Solution 1

Alternatively, you might be able to leverage the gradle-tomcat-plugin

Solution 2

The WAR task is aware of the artifacts it generates.

task deployToTomcat(type: Copy) {
    from war.archivePath
    into "${tomcatHome}/webapps"
}

Solution 3

I accomplished this with:

task deploy (dependsOn: war){
    copy {
        from "build/libs"
        into "C:/dev/jetty-distribution-9.1.4.v20140401/webapps"
        include "*.war"
    }
}

running it like this:

gradle deploy

Solution 4

You could of-course use the tomcat plugin. My setup prevents me from using/modify the out of the box war & tomcat option.

I personally like the following flavor (copied from my build.gradle).

tomcat_home='tomcat_location'
tomcat_bin=tomcat_home + '/bin'
tomcat_start=tomcat_bin + '/startup.sh'
tomcat_stop=tomcat_bin + '/shutdown.sh'
tomcat_webapps = tomcat_home + '/webapps'
task tom << {
    if (project.hasProperty('start')) {
        startTom()
    } else if (project.hasProperty('stop')) {
        stopTom()
    } else if (project.hasProperty('deployNstart')) {
        stopTom()
        webappsCopy()
        startTom()
    } else {
        throw new RuntimeException('unrecognized option')
    }
}
def stopTom() {
    executeCmd(tomcat_stop)
}
def startTom() {
    executeCmd(tomcat_start)
}
def executeCmd(command) {
    proc = command.execute()
    proc.waitFor()
}
def webappsCopy() {
    copy {
        from 'war file location' // could be exploded or war itself
        into tomcat_webapps
    }
}

-- you call the various options you include in the 'tom' task from the command line --

$ gradle tom -Pstart
$ gradle tom -Pstop
$ gradle tom -PdeployNstart

this could potentially grow further, as I add more commands/options related to Tomcat. Few pointers:

  1. move the location etc. to gradle.properties so that it could work in different environments.
  2. poll your tomcat server port to fine tune options and msgs.
  3. move to plugin/task code that could be reused.

this limited version works for me right now :-)

Solution 5

You could give the Gradle Cargo plugin a shot. It lets you deploy a WAR file to a local as well as a remote Tomcat.

Share:
44,627
Dave
Author by

Dave

SOreadytohelp

Updated on July 25, 2020

Comments

  • Dave
    Dave almost 3 years

    I'm trying to write a Gradle task which copies generated war files to my local tomcat instance:

    This isn't working and I'm not sure how to debug it:

     task deploylocal() << {
        println "Copy from ${buildDir}\\libs into ${tomcatHome}/webapps"
        copy{
          from "${buildDir}\\libs"
          into "${tomcatHome}/webapps"
          include '*.war'
        }
      }
    

    Any ideas on what to do next?

  • Dave
    Dave over 12 years
    Thanks, I might use that instead!
  • Rup
    Rup about 11 years
    Ken comments that you would not have declare the dependsOn: 'war': the fact that you are copying from war.archivePath is an implicit dependency.
  • Benjamin Muschko
    Benjamin Muschko almost 10 years
    I'd recommend using the Copy task type instead of the method call copy. The method call doesn't provide you with UP-TO-DATE checks.
  • lapo
    lapo over 4 years
    This might copy multiple versions all at once (unless you clean often), it's better to copy only war.archivePath as suggested in other answers to copy latest version only.
  • Adam Johns
    Adam Johns over 4 years
    I had my war.dependsOn test and I had to add deployToTomcat.dependsOn war to make sure my tests ran.