gradle: copy war to tomcat directory

前端 未结 9 1356
情话喂你
情话喂你 2020-12-28 14:01

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:



        
9条回答
  •  [愿得一人]
    2020-12-28 14:44

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

提交回复
热议问题