Is there any easy way to run jetty 8 from gradle (

2019-07-19 04:39发布

问题:

Unlucky I need jetty 8 to work properly with spray/akka (it's scala project).
With older version used by jettyRun I'm getting error like:
java.lang.NoClassDefFoundError: org/eclipse/jetty/continuation/ContinuationListener
Is it possible to create some simple task to do the job which jettyRun is doing but with jetty 8?

In worst case I can use embedded version of jetty with war which I'm building, but I would be happy to see some simpler solution if there is any...

回答1:

Pitor, why didn't you end up adding your excellent answer from here?

I've adapted it below, to use Jetty version 9, to depend on the war task, and to use the same task name as the jetty plugin (i.e. jettyRun).

configurations {
    jetty
}

dependencies {
    jetty "org.eclipse.jetty:jetty-runner:9.2.11.v20150529"
}

task jettyRun(type: JavaExec, dependsOn: war) {
    main = "org.eclipse.jetty.runner.Runner"
    args = [war.archivePath]
    classpath configurations.jetty
}

Usage:

gradle jettyRun


回答2:

Since I wasn't able to find good solution at the gradle build level I decided to use embedded jetty. Here is scala class:

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext
import org.eclipse.jetty.server.bio.SocketConnector

object JettyServer {

  def main(args: Array[String]) {
    val server = new Server
    val context = new WebAppContext
    val connector = new SocketConnector

    connector.setMaxIdleTime(1000 * 60 * 60)
    connector.setPort(8080)

    context.setServer(server)
    context.setWar(args(0))

    server.setConnectors(Array(connector))
    server.setHandler(context)

    try {
      server.start();
      server.join();
    } catch {
      case e: Exception => e.printStackTrace();
    }
  }
}

And then in build.gradle:

apply plugin: "application"

mainClassName = "com.mycompany.myproject"
run.args = [war.archivePath]
task jettyRun(dependsOn: run)

And everything works :)



回答3:

I think it's a little late for an answer :) But I'll post it for others who would google around for the same thing.

I stumbled upon the same issue while trying to run a scalatra app with gradle. I found this plugin and it just works - https://github.com/martins1930/jettyMulti