Skip to content

Commit

Permalink
Create a webapp components plugin
Browse files Browse the repository at this point in the history
To save on unnecessary IO, we want to be able to identify all of the
components of a webapp without unnecessarily copying files around.

A webapp consists of three sets of files:

* resources (HTML/CSS/JS files, images, deployment descriptor, etc.)
* .class files to be loaded by the container classloader
* .jar files to be loaded by the container classloader

In most cases, resources are static and kept in the `src/main/webapp`
directory.  Occasionally they might be generated (e.g. via a minifier, a
JS compiler, etc.).

The webapp components plugin allows the user to specify/override the
locations of these files.

The webapp components plugin will be used by a downstream container
runner to launch a container without needing to copy static files
around, prepare an exploded .war file, or package a .war file.
  • Loading branch information
earldouglas committed Sep 22, 2024
1 parent 7733d3b commit a40ffb7
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 98 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ semanticdbEnabled := true
semanticdbVersion := scalafixSemanticdb.revision
scalacOptions += "-Ywarn-unused-import"

// Testing
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % "test"

// Publish to Sonatype, https://www.scala-sbt.org/release/docs/Using-Sonatype.html
credentials := List(
Credentials(Path.userHome / ".sbt" / "sonatype_credentials")
Expand Down
71 changes: 71 additions & 0 deletions src/main/scala/com/earldouglas/sbt/war/WebappComponents.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.earldouglas.sbt.war

import sbt._

/** Identifies the files that compose the webapp:
*
* - Resources
* - HTML/JS/CSS files, images, etc.
* - Optional WEB-INF/web.xml deployment descriptor
* - Anything else that isn't a .class file or .jar file
* - .class files
* - .jar files
*/
object WebappComponents {

/** Given a resources directory, e.g. src/main/webapp, traverse to
* find all the files it contains.
*
* @return
* a mapping from source to destination of webapp resources
*/
def getWebappResources(resourcesDir: File): Map[File, String] = {
(resourcesDir ** "*").get
.filter(_.isFile())
.flatMap(src =>
IO
.relativize(resourcesDir, src)
.map(dst => src -> dst)
)
.toMap
}

/** Given a classpath (potentially with both .jar files and classes
* directories), traverse to find all the .class files.
*
* @return
* a mapping from source to destination of .class files
*/
def getWebappClasses(classpath: Seq[File]): Map[File, String] = {

val classpathDirs: Seq[File] =
classpath
.filter(_.isDirectory())

val classesMappings: Seq[(File, File)] =
for {
classpathDir <- classpathDirs
classFile <- (classpathDir ** "*").get
if classFile.isFile()
relativeFile <- IO.relativizeFile(classpathDir, classFile)
} yield (classFile, relativeFile)

classesMappings
.map({ case (src, dst) => src -> dst.getPath() })
.toMap
}

/** Given a classpath (potentially with both .jar files and classes
* directories), traverse to find all the .jar files.
*
* @return
* a mapping from source to destination of .jar files
*/
def getWebappLib(classpath: Seq[File]): Map[File, String] = {
classpath
.filter(f => f.isFile())
.filter(f => f.getName().endsWith(".jar"))
.map(src => src -> src.getName())
.toMap
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.earldouglas.sbt.war

import sbt.Def.Initialize
import sbt.Def.taskKey
import sbt.Keys._
import sbt._

/** Identifies the files that compose the webapp (resources, .class
* files, and .jar files). This is used by user-facing plugins
* (WarPlugin and WebappRunnerPlugin).
*/
object WebappComponentsPlugin extends AutoPlugin {

object autoImport {

lazy val webappResources =
taskKey[Map[File, String]]("webapp resources")

lazy val webappClasses =
taskKey[Map[File, String]]("webapp classes")

lazy val webappLib =
taskKey[Map[File, String]]("webapp lib")
}

import autoImport._

override def requires = plugins.JvmPlugin

val webappResourcesSetting: Initialize[Map[File, String]] =
Def.setting {
val resourcesDir: File =
(Compile / sourceDirectory).value / "webapp"
WebappComponents.getWebappResources(resourcesDir)
}

val webappClassesTask: Initialize[Task[Map[File, String]]] =
Def.task {
val classpath: Seq[File] =
(Runtime / fullClasspath).value
.map(_.data)
WebappComponents.getWebappClasses(classpath)
}

val webappLibTask: Initialize[Task[Map[File, String]]] =
Def.task {
val classpath: Seq[File] =
(Runtime / fullClasspath).value
.map(_.data)
WebappComponents.getWebappLib(classpath)
}

override def projectSettings: Seq[Setting[_]] =
Seq(
webappResources := webappResourcesSetting.value,
webappClasses := webappClassesTask.value,
webappLib := webappLibTask.value
)
}
117 changes: 59 additions & 58 deletions src/main/scala/com/earldouglas/xwp/WebappPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.earldouglas.xwp

import com.earldouglas.sbt.war.WebappComponents
import com.earldouglas.sbt.war.WebappComponentsPlugin
import sbt.Def.settingKey
import sbt.Def.taskKey
import sbt.FilesInfo.exists
Expand Down Expand Up @@ -27,7 +29,7 @@ object WebappPlugin extends AutoPlugin {

import autoImport._

override def requires = plugins.JvmPlugin
override def requires = WebappComponentsPlugin

override def projectSettings: Seq[Setting[_]] =
Seq(
Expand Down Expand Up @@ -94,22 +96,33 @@ object WebappPlugin extends AutoPlugin {
) =
Def.task {

val webappSrcDir = (webappPrepare / sourceDirectory).value
val webappResourcesDir: File =
(webappPrepare / sourceDirectory).value

val webappTargetDir: File =
webappTarget.value

val resourceFiles: Set[File] =
WebappComponents
.getWebappResources(webappResourcesDir)
.filterNot(x => x._1.isDirectory())
.map(_._1)
.toSet

cacheify(
cacheName,
{ in =>
for {
f <- Some(in)
if !f.isDirectory
r <- IO.relativizeFile(webappSrcDir, f)
} yield IO.resolve(webappTarget.value, r)
r <- IO.relativizeFile(webappResourcesDir, f)
t = IO.resolve(webappTargetDir, r)
} yield t
},
(webappSrcDir ** "*").get.toSet,
resourceFiles,
streams.value
)

webappTarget.value
webappTargetDir
}

private def webappPrepareQuickTask =
Expand All @@ -136,74 +149,62 @@ object WebappPlugin extends AutoPlugin {
val webappTarget =
_webappPrepare(webappPrepare / target, "webapp").value

val m = (Compile / packageBin / mappings).value
val p = (Compile / packageBin / packagedArtifact).value._2

val webInfDir = webappTarget / "WEB-INF"
val webappLibDir = webInfDir / "lib"

if (webappWebInfClasses.value) {
// copy this project's classes directly to WEB-INF/classes
val classpath: Seq[File] =
(Runtime / fullClasspath).value
.map(_.data)

val webappClasses: Map[File, String] =
WebappComponents.getWebappClasses(classpath)

// copy this project's classes directly to WEB-INF/classes
def classesAsClasses(): Set[File] = {

cacheify(
"classes",
{ in =>
m find { case (src, dest) =>
src == in
} map { case (src, dest) =>
webInfDir / "classes" / dest
}
webappClasses
.find { case (src, dest) => src == in }
.map { case (src, dest) => webInfDir / "classes" / dest }
},
(m filter { case (src, dest) =>
!src.isDirectory
} map { case (src, dest) =>
src
}).toSet,
taskStreams
)
} else {
// copy this project's classes as a .jar file in WEB-INF/lib
cacheify(
"lib-art",
{ in => Some(webappLibDir / in.getName) },
Set(p),
webappClasses
.filter { case (src, dest) => !src.isDirectory }
.map { case (src, dest) => src }
.toSet,
taskStreams
)
}

val classpath = (Runtime / fullClasspath).value

// create .jar files for depended-on projects in WEB-INF/lib
for {
cpItem <- classpath.toList
dir = cpItem.data
if dir.isDirectory
artEntry <- cpItem.metadata.entries find { e =>
e.key.label == "artifact"
}
cpArt = artEntry.value.asInstanceOf[Artifact]
artifact = (Compile / packageBin / packagedArtifact).value._1
if cpArt != artifact
files = (dir ** "*").get flatMap { file =>
if (!file.isDirectory)
IO.relativize(dir, file) map { p => (file, p) }
else
None
}
jarFile = cpArt.name + ".jar"
_ = Compat.jar(
sources = files,
outputJar = webappLibDir / jarFile,
// copy this project's classes as a .jar file in WEB-INF/lib
def classesAsJar(): Set[File] = {

val jarFilename: String =
(Compile / packageBin / packagedArtifact).value._2.getName()

val outputJar = webappLibDir / jarFilename

Compat.jar(
sources = webappClasses,
outputJar = outputJar,
manifest = new Manifest
)
} yield ()

Set(outputJar)
}

if (webappWebInfClasses.value) {
classesAsClasses()
} else {
classesAsJar()
}

// copy this project's library dependency .jar files to WEB-INF/lib
cacheify(
"lib-deps",
{ in => Some(webappTarget / "WEB-INF" / "lib" / in.getName) },
classpath.map(_.data).toSet filter { in =>
!in.isDirectory && in.getName.endsWith(".jar")
},
{ in => Some(webappTarget / "WEB-INF" / "lib" / in.getName()) },
WebappComponents.getWebappLib(classpath).keySet,
taskStreams
)

Expand Down

This file was deleted.

11 changes: 2 additions & 9 deletions src/sbt-test/container/multi-module-multi-webapp/test
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@

> jetty:stop

$ exists mathsweb/target/webapp/WEB-INF/lib/maths.jar
$ absent mathsweb/target/webapp/WEB-INF/lib/remote.jar
$ absent mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war

$ exists remoteweb/target/webapp/WEB-INF/lib/remote.jar
$ absent remoteweb/target/webapp/WEB-INF/lib/maths.jar
$ absent remoteweb/target/scala-2.12/remoteweb_2.12-0.1.0-SNAPSHOT.war

> package

$ exists mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/maths.jar
-> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/remote.jar
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/mathsweb_2.12-0.1.0-SNAPSHOT.jar

$ exists remoteweb/target/scala-2.12/remoteweb_2.12-0.1.0-SNAPSHOT.war
> findInZip remoteweb/target/scala-2.12/remoteweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/remote.jar
-> findInZip remoteweb/target/scala-2.12/remoteweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/maths_2.12-0.1.0-SNAPSHOT.jar
> findInZip remoteweb/target/scala-2.12/remoteweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/remoteweb_2.12-0.1.0-SNAPSHOT.jar
7 changes: 1 addition & 6 deletions src/sbt-test/container/multi-module-single-webapp/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
> get http://localhost:8080/index.html 200
> get http://localhost:8080/test 200
> jetty:stop
$ exists mathsweb/target/webapp/WEB-INF/lib/numbers.jar
$ exists mathsweb/target/webapp/WEB-INF/lib/maths.jar
$ exists mathsweb/target/webapp/WEB-INF/lib/typeclasses.jar
$ exists mathsweb/target/webapp/WEB-INF/lib/mathsweb_2.12-0.1.0-SNAPSHOT.jar
$ absent target/scala-2.12/root_2.12-0.1.0-SNAPSHOT.war
$ absent numbers/target/scala-2.12/numbers_2.12-0.1.0-SNAPSHOT.war
$ absent typeclasses/target/scala-2.12/typeclasses_2.12-0.1.0-SNAPSHOT.war
Expand All @@ -18,6 +16,3 @@ $ absent typeclasses/target/scala-2.12/typeclasses_2.12-0.1.0-SNAPSHOT.war
$ absent maths/target/scala-2.12/maths_2.12-0.1.0-SNAPSHOT.war
$ exists mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/mathsweb_2.12-0.1.0-SNAPSHOT.jar
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/numbers.jar
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/maths.jar
> findInZip mathsweb/target/scala-2.12/mathsweb_2.12-0.1.0-SNAPSHOT.war WEB-INF/lib/typeclasses.jar
4 changes: 4 additions & 0 deletions src/sbt-test/plugins/webapp-components/.scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rules = [
OrganizeImports
RemoveUnused
]
2 changes: 2 additions & 0 deletions src/sbt-test/plugins/webapp-components/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version=3.7.13
runner.dialect=scala3
Loading

0 comments on commit a40ffb7

Please sign in to comment.