简体   繁体   中英

How to run jetty 7+ with specified war with groovy/gradle?

I want to run Jetty 7+ with gradle build, but unlucky looks like there is no way to do this with jettyRun. So probably simplest idea to achieve what I want would be to use custom target:

task runJetty << {
  def server = new Server()
  // more code here
  server.start()
  server.join()   
}

Unlucky I just started with gradle and I don't know groovy either, so it's hard for me to create proper target. I was looking over the internet but I wasn't able to find any solution. Can anyone hit me with some sample groovy code which can run existing jar with jetty?

Ok, I found out how to run it using jetty directly from repository:

jettyVersion = "8.1.0.RC0"

configurations {
    jetty8
}

dependencies {
    jetty8 "org.mortbay.jetty:jetty-runner:$jettyVersion"
}

task runJetty8(type: JavaExec) {
    main = "org.mortbay.jetty.runner.Runner"
    args = [war.archivePath]
    classpath configurations.jetty8
}

Here's a working version, using the jetty ant tasks. This finally enabled me the proper control with deamon=true.

configurations { jetty }
dependencies { jetty 'org.eclipse.jetty:jetty-ant:9.0.4.v20130625' }
task jetty(dependsOn: build) << {
    ant.taskdef(name: 'jettyRun', classname: 'org.eclipse.jetty.ant.JettyRunTask', classpath: configurations.jetty.asPath, loaderref: "jetty.loader")
    ant.typedef(name: "connector", classname: "org.eclipse.jetty.ant.types.Connector", classpath: configurations.jetty.asPath, loaderref: "jetty.loader")
    ant.jettyRun(daemon:true, stopPort: 8999, stopKey: "STOP") {
        webApp(war: THE_WAR_PRODUCING_TASK.archivePath, contextPath: '/context')
        connectors { connector(port: 9000) }
        systemProperties {
            systemProperty(name: 'environment.type', value: 'development')
        }
    }
}
task jettyStop << {
    ant.taskdef(name: 'jettyStop', classname: 'org.eclipse.jetty.ant.JettyStopTask', classpath: configurations.jetty.asPath)
    ant.jettyStop(stopPort: 8999, stopKey: "STOP")
}

有一个jetty-eclipse-plugin允许你运行更新版本的jetty https://github.com/Khoulaiz/gradle-jetty-eclipse-plugin

jetty plugin supports jetty 6.1.25 at present

You can use something like this:

jettyRoot = '/path/to/your/jetty/root'
task runJetty7 << {
  description = "Runs jetty 7"
  ant.java(dir: jettyRoot, jar: jettyRoot + '/start.jar', failOnError: 'true', fork: 'true') {
    classpath {
      ...
    }
  }
}

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