简体   繁体   中英

How to stop Tomcat server launched from an Eclipse Gradle Plugin?

I'm running through a tutorial from http://spring.io about RESTful webservices.
I wanted to be able to launch my web project from Eclipse as a Gradle build ( Run As => Gradle Build... ) and then stop it when I'm done testing it.
I know how to start it, but I just can't get it to stop without quitting Eclipse (Spring Tool Suite).

Any Suggestions?

To borrow from the Gradle Tomcat plugin documentation, just do this:

ext {
    tomcatStopPort = 8081
    tomcatStopKey = 'stopKey'
}

task doTomcatRun(type: org.gradle.api.plugins.tomcat.TomcatRun) {
    stopPort = tomcatStopPort
    stopKey = tomcatStopKey
    daemon = true
}

task doTomcatStop(type: org.gradle.api.plugins.tomcat.TomcatStop) {
    stopPort = tomcatStopPort
    stopKey = tomcatStopKey
}

task someTask(type: SomeGradleTaskType) {
    //do stuff
    dependsOn doTomcatRun
    finalizedBy doTomcatStop
}

In this example, someTask is a Gradle task that you execute where Tomcat is started before the task runs and Tomcat is stopped after the task completes.

If you prefer something more manual, then simply configure and then run the tomcatStop task via Eclipse:

tomcatStop {
        stopPort = 8090
        stopKey = 'foo'
}

May I humbly suggest you using gradle "jetty" plugin instead. Consider:

a) it supports jettyRun, jettyRunWar and jettyStop tasks.

b) it uses jetty 6 and, therefore, is compatible with servlet-api 2.5 (the equivalent version of tomcat is 6).

If you are not satisfied with too old jetty/servlet-api, feel free trying "gretty" plugin (which I wrote) - it supports jetty 7/8/9 and servlet-api 2.5/3.0.1/3.1.0. Gretty plugin is available in maven central and on github: https://github.com/akhikhl/gretty . The documentation is also provided on github.

In your build.gradle

[tomcatRun, tomcatRunWar, tomcatStop]*.stopPort = 8081
[tomcatRun, tomcatRunWar, tomcatStop]*.stopKey = 'stopKey'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM