Skip to content

Commit

Permalink
War packaging support
Browse files Browse the repository at this point in the history
  • Loading branch information
aolshevskiy committed Oct 2, 2011
1 parent 66eab35 commit 91b1575
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/github/siasia/Container.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ case class Container(name: String) {
def pairToTask(conf: Configuration)(p: (String, ProjectReference)): Initialize[Task[(String, Deployment)]] = (deployment in (p._2, conf)) map { (d) => (p._1, d) }
type SettingSeq = Seq[Setting[_]]
def deploy(map: (String, ProjectReference)*): SettingSeq =
deploy(Runtime)(map :_*)
deploy(Defaults.conf)(map :_*)
def deploy(conf: Configuration)(map: (String, ProjectReference)*): SettingSeq =
settings ++ inConfig(Configuration)(Seq(
apps <<= map.map(pairToTask(conf)).join
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/com/github/siasia/PluginKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ case class Deployment(
scanDirectories: Seq[File],
scanInterval: Int
)

object Defaults {
val conf = Runtime
}

object PluginKeys {
lazy val port = SettingKey[Int]("port")
Expand All @@ -19,5 +23,6 @@ object PluginKeys {
lazy val webappResources = SettingKey[Seq[File]]("webapp-resources")
lazy val scanDirectories = SettingKey[Seq[File]]("scan-directories")
lazy val scanInterval = SettingKey[Int]("scan-interval")
lazy val packageWar = TaskKey[File]("package-war")
lazy val deployment = TaskKey[Deployment]("deployment")
}
65 changes: 65 additions & 0 deletions src/main/scala/com/github/siasia/WarPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.siasia

import sbt._
import Project.Initialize
import Keys._
import PluginKeys._
import _root_.sbt.Defaults.packageTasks

object WarPlugin extends Plugin {
private def copyFlat(sources: Iterable[File], destinationDirectory: File): Set[File] = {
val map = sources.map(source => (source.asFile, destinationDirectory / source.getName))
IO.copy(map)
}

def packageWarTask: Initialize[Task[Seq[(File, String)]]] =
(webappResources, target, fullClasspath, excludeFilter, streams) map {
(webappResources, target, fullClasspath, filter, s) =>
val classpath = fullClasspath.map(_.data)
val warPath = target / "webapp"
val log = s.log.asInstanceOf[AbstractLogger]
import _root_.sbt.classpath.ClasspathUtilities
val webInfPath = warPath / "WEB-INF"
val webLibDirectory = webInfPath / "lib"
val classesTargetDirectory = webInfPath / "classes"

val (libs, directories) = classpath.toList.partition(ClasspathUtilities.isArchive)
val wcToCopy = for {
dir <- webappResources
file <- dir.descendentsExcept("*", filter).get
val target = Path.rebase(dir, warPath)(file).get
} yield (file, target)
val classesAndResources = for {
dir <- directories
file <- dir.descendentsExcept("*", filter).get
val target = Path.rebase(dir, classesTargetDirectory)(file).get
} yield (file, target)
if(log.atLevel(Level.Debug))
directories.foreach(d => log.debug(" Copying the contents of directory " + d + " to " + classesTargetDirectory))

val copiedWebapp = IO.copy(wcToCopy)
val copiedClasses = IO.copy(classesAndResources)
val copiedLibs = copyFlat(libs, webLibDirectory)
val toRemove = scala.collection.mutable.HashSet((warPath ** "*").get.toSeq : _*)
toRemove --= copiedWebapp
toRemove --= copiedClasses
toRemove --= copiedLibs
val (dirs, files) = toRemove.toList.partition(_.isDirectory)
if(log.atLevel(Level.Debug))
files.foreach(r => log.debug("Pruning file " + r))
IO.delete(files)
IO.deleteIfEmpty(dirs.toSet)
(warPath).descendentsExcept("*", filter) x (relativeTo(warPath)|flat)
}
def warSettings0 = Seq(
webappResources <<= sourceDirectory(sd => Seq(sd / "webapp")),
configuration in packageWar := Defaults.conf,
artifact in packageWar <<= name(n => Artifact(n, "war", "war")),
publishArtifact in (Compile, packageBin) := false,
publishArtifact in packageWar := true
) ++
packageTasks(packageWar, packageWarTask)
def warSettings = inConfig(Defaults.conf)(warSettings0) ++
addArtifact(artifact in (Defaults.conf, packageWar), packageWar in Defaults.conf)

}
4 changes: 2 additions & 2 deletions src/main/scala/com/github/siasia/WebappPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import sbt._
import Keys._
import PluginKeys._
import classpath.ClasspathUtilities._
import WarPlugin.warSettings

object WebappPlugin extends Plugin {
def webappSettings0 = Seq(
webappResources <<= sourceDirectory(sd => Seq(sd / "webapp")),
scanDirectories <<= classDirectory(Seq(_)),
scanInterval := 3,
deployment <<= (webappResources, fullClasspath, scanDirectories, scanInterval) map {
(rs, cp, sd, si) =>
Deployment(rs, cp.map(_.data), sd, si)
}
)
def webappSettings = inConfig(Runtime)(webappSettings0)
def webappSettings = warSettings ++ inConfig(Defaults.conf)(webappSettings0)
}

0 comments on commit 91b1575

Please sign in to comment.