-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7733d3b
commit a40ffb7
Showing
36 changed files
with
711 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/scala/com/earldouglas/sbt/war/WebappComponents.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/scala/com/earldouglas/sbt/war/WebappComponentsPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
src/sbt-test/container/multi-module-multi-webapp/remoteweb/src/main/scala/remote.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
rules = [ | ||
OrganizeImports | ||
RemoveUnused | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version=3.7.13 | ||
runner.dialect=scala3 |
Oops, something went wrong.