Skip to content

Instantly share code, notes, and snippets.

@sdb
Created March 7, 2011 23:38
Show Gist options
  • Save sdb/859524 to your computer and use it in GitHub Desktop.
Save sdb/859524 to your computer and use it in GitHub Desktop.
create an SBT plug-in for running webapps in Glassfish
package sbt {
import org.glassfish.embeddable.{GlassFish, GlassFishRuntime, GlassFishProperties, Deployer}
trait GlassFishPlugin extends BasicWebScalaProject {
def glassFishPort = GlassFishRunner.DefaultPort
private lazy val glassFish = new GlassFishRunner(glassFishPort, temporaryWarPath)
lazy val glassfishRun = glassfishRunAction
def glassfishRunAction = task {
glassFish()
} dependsOn(prepareWebapp) describedAs("Starts the embedded GlassFish server and serves this project as a web application.")
lazy val glassfishStop = glassfishStopAction
def glassfishStopAction = task {
glassFish.stop; None
} describedAs("Stops the embedded GlassFish server that was started with glassfish-run.")
lazy val glassfishRestart= glassfishRestartAction
def glassfishRestartAction = task {
glassFish.stop; glassFish()
} describedAs("Restarts the embedded GlassFish server that was started with glassfish-run.")
}
object GlassFishRunner {
val DefaultPort = 8080
lazy val runtime = GlassFishRuntime.bootstrap
}
class GlassFishRunner(val port: Int, val path: Path) extends ExitHook {
ExitHooks.register(this)
private var glassFish: Option[GlassFish] = None
private var deployKey: String = _
private lazy val glassFishProps = new GlassFishProperties {
setPort("http-listener", port)
}
def name = "glassfish-shutdown"
def runBeforeExiting() { stop }
def apply(): Option[String] = {
if (glassFish.isDefined)
Some("A GlassFish instance is already running")
else
try {
val gf = GlassFishRunner.runtime.newGlassFish(glassFishProps)
gf.start
val d = gf.getService(classOf[Deployer])
deployKey = d.deploy(path.asFile)
glassFish = Some(gf)
None
} catch {
case e => Some("Error running GlassFish: %s".format(e.getMessage))
}
}
def stop() {
glassFish.foreach { gf =>
val d = gf.getService(classOf[Deployer])
d.undeploy(deployKey)
gf.stop
gf.dispose
}
glassFish = None
}
}
}
import sbt._
class GlassFishProject(info: ProjectInfo) extends PluginProject(info) {
val glassFishEmbedded = "org.glassfish.extras" % "glassfish-embedded-all" % "3.1-b33" % "provided"
val javaNetRepo = "java.net GlassFish Maven repository" at "http://download.java.net/maven/glassfish/"
}
@jcranky
Copy link

jcranky commented Jul 16, 2012

Is this plugin available from any repo, or should I use it by copying the source to my project?

@sdb
Copy link
Author

sdb commented Jul 26, 2012

@jcranky yeah, you should add the source code to your project. i never created a plugin for this.
note that this is for sbt 0.7, not for the latest versions.

@jcranky
Copy link

jcranky commented Jul 26, 2012

thanks!
it certainly serves as inspiration =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment