Skip to content

Instantly share code, notes, and snippets.

@remeniuk
Created November 18, 2011 09:52
Show Gist options
  • Save remeniuk/1376036 to your computer and use it in GitHub Desktop.
Save remeniuk/1376036 to your computer and use it in GitHub Desktop.
Deploying to Glassfish remotely through SBT
libraryDependencies ++= Seq(
"com.sun.jersey" % "jersey-client" % "1.8",
"com.sun.jersey" % "jersey-core" % "1.8",
"com.sun.jersey.contribs" % "jersey-multipart" % "1.8"
)
> show glassfish-context-path
[info] nbaa
> show glassfish-mgmt-url
[info] http://localhost:4848/management/domain
> glassfish-deploy
[info] GLASSFISH: deploying [D:\PROJECTS\NBAAv2_newschema\nbaa-web-ui\target\scala-2.9.1\nbaa-web-ui_2.9.1-0.1-SNAPSHOT.war]
to the server...
[info] GLASSFISH: [nbaa] deployment finished: POST http://localhost:4848/management/domain/applications/application returned
a response status of 200 OK
[success] Total time: 56 s, completed Nov 18, 2011 8:35:47 PM
> glassfish-apps
[info] ==================================
[info] GLASSFISH: deployed applications:
[info] http://localhost:4848/management/domain/applications/application/nbaa-web-ui_2.9.1-0.1-SNAPSHOT
[info] ==================================
[success] Total time: 0 s, completed Nov 18, 2011 8:52:09 PM
trait GlassfishDeployer {
import com.sun.jersey.api.client.{Client, ClientResponse}
import com.sun.jersey.multipart.FormDataMultiPart
import com.sun.jersey.multipart.file.FileDataBodyPart
import javax.ws.rs.core.MediaType
import java.io.File
import scala.xml._
private val client = Client.create()
private val responseType = "application/xml"
private val glassfishMgmtUrl = SettingKey[String]("glassfish-mgmt-url", "URL to GlassFish management service.")
private val glassfishContextPath = SettingKey[String]("glassfish-context-path", "Application context path.")
private val glassfishApps = TaskKey[Unit]("glassfish-apps", "List of deployed GlassFish applications.")
private val glassfishDeploy = TaskKey[Unit]("glassfish-deploy", "Deploys web application to Glassfish.")
private val glassfishAppsTask = glassfishApps <<= (glassfishMgmtUrl, streams) map {
(url, s) =>
val log = s.log
val xml = XML.loadString(client.resource(url + "/applications/application")
.accept(responseType)
.get(classOf[String]))
val deployedApplications = for (entry <- ((xml \\ "entry" filter {
node => (node \ "@key").text == "childResources"
}) \ "map" \ "entry")) yield entry.attributes("value").text
if (deployedApplications.isEmpty) log.info("GLASSFISH: no applications deployed...")
else {
log.info("==================================")
log.info("GLASSFISH: deployed applications: ")
deployedApplications.foreach(app => log.info(app))
log.info("==================================")
}
}
private val glassfishDeployTask = glassfishDeploy <<= (packageWar in Compile, glassfishMgmtUrl, glassfishContextPath, streams) map {
(war, url, contextPath, s) =>
val log = s.log
val form = new FormDataMultiPart {
getBodyParts.add(new FileDataBodyPart("id", war))
field("contextroot", contextPath, MediaType.TEXT_PLAIN_TYPE)
field("force", "true", MediaType.TEXT_PLAIN_TYPE)
}
log.info("GLASSFISH: deploying [%s] to the server..." format (war.getAbsolutePath))
val result = client.resource(url + "/applications/application")
.`type`(MediaType.MULTIPART_FORM_DATA)
.accept(responseType)
.post(classOf[ClientResponse], form)
log.info("GLASSFISH: [%s] deployment finished: %s" format (contextPath, result))
}
val glassfishSettings = Seq(
glassfishMgmtUrl := "http://localhost:4848/management/domain",
glassfishContextPath := "nbaa",
glassfishAppsTask,
glassfishDeployTask
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment