开发者

gradle: copy war to tomcat directory

开发者 https://www.devze.com 2023-01-25 20:51 出处:网络
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:

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"
     开发者_C百科 include '*.war'
    }
  }

Any ideas on what to do next?


The WAR task is aware of the artifacts it generates.

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


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


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 :-)


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


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.


Please, make sure the war archive is getting bundled before deploylocal is executed. Maybe, you could define a dependency:

task deploylocal(dependsOn: build) << {

NB There is a convention property in java plugin named libsDir. It allows you to reference build/libs directory in better way:

 task deploylocal(dependsOn: build) << {
    println "Copy from ${libsDir.getPath()} into ${tomcatHome}/webapps"
    copy{
      from libsDir
      into "${tomcatHome}/webapps"
      include '*.war'
    }
  }


Actually later a replaced this functionality with cargo plugin for gradle and runn the deploy by

gradle cargoRunLocal


If you already have the war plugin you can also just add

war.doLast {
    copy {
        from war.archiveFile
        into "${tomcatHome}/webapps"
    }
}

Then you will always automatically deploy each time you build the war file.


First, attempting to debug. Check the order of task execution; see if it it being run after the war file is produced. Also whether or not it says UP-TO-DATE.

The problem could be the lack of any dependencies between the this task and the output you want to copy.

Try replacing this general task with an instance of a Copy task, and make it dependent on the war file you're trying to copy.

Something like this:

task deployLocal(type: Copy) { 
    dependsOn configurations.archives.buildArtifacts
    from configurations.archives.allArtifacts*.file 
    into("$tomcatHome/webapps") 
}


Instead of copy the war in another local path, you can directly generate the war in the desired directory using destinationDirectory property of the war plugin.

war {
   destinationDirectory = new File("${tomcatHome}/webapps")
}
0

精彩评论

暂无评论...
验证码 换一张
取 消