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

4

9

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?

Adrianaadriane answered 25/11, 2011 at 8:19 Comment(1)
possible duplicate of Is there any easy way to run jetty 8 from gradle (like with jettyRun)?Michel
A
15

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
}
Adrianaadriane answered 10/12, 2011 at 16:58 Comment(2)
How can I create a task that uses Jetty 8, and mimics jettyRunWar which packages the war and deploys it to the server?Inimical
How do u specify the context path? I tried --path contextPath. It doesn't work. I can see this in the logs: o.e.j.w.WebAppContext{/,null}Calico
S
3

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")
}
Subzero answered 2/8, 2013 at 16:22 Comment(1)
I don't see any IP address is given here. My main goal is to connect my web service via other devices in the same local area.Climb
J
3

There is a jetty-eclipse-plugin that allows you to run newer versions of jetty https://github.com/Khoulaiz/gradle-jetty-eclipse-plugin

Jeu answered 25/10, 2013 at 18:27 Comment(0)
F
1

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 {
      ...
    }
  }
}
Fabron answered 9/12, 2011 at 13:44 Comment(2)
Yea, I know its possible to do it this way, but I don't like this line: "jettyRoot = '/path/to/your/jetty/root'". Is it possible to add jetty as runtime dependency and somehow get the path where gradle saved it (after fetching)?Adrianaadriane
You could add jetty 7 as a dependency: configurations { jetty7 } dependencies { jetty7 "org.mortbay.jetty:jetty:+" } ext.jettyRoot = configurations.jetty7Fast

© 2022 - 2024 — McMap. All rights reserved.