Skip to content

Commit

Permalink
Merge pull request #4522 from jroper/externalized-exclude
Browse files Browse the repository at this point in the history
Exclude externalized resources from application jar in dist
  • Loading branch information
marcospereira committed May 22, 2015
2 parents 3cda2f7 + 8137e10 commit 848329b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
Expand Up @@ -97,6 +97,8 @@ object PlayImport {
val playInteractionMode = SettingKey[PlayInteractionMode]("playInteractionMode", "Hook to configure how Play blocks when running")

val externalizeResources = SettingKey[Boolean]("playExternalizeResources", "Whether resources should be externalized into the conf directory when Play is packaged as a distribution.")
val playExternalizedResources = TaskKey[Seq[(File, String)]]("playExternalizedResources", "The resources to externalize")
val playJarSansExternalized = TaskKey[File]("playJarSansExternalized", "Creates a jar file that has all the externalized resources excluded")

val playOmnidoc = SettingKey[Boolean]("playOmnidoc", "Determines whether to use the aggregated Play documentation")
val playDocsName = SettingKey[String]("playDocsName", "Artifact name of the Play documentation")
Expand Down
Expand Up @@ -183,8 +183,7 @@ object PlaySettings {
// Support for externalising resources
mappings in Universal ++= {
if (externalizeResources.value) {
val rdirs = (unmanagedResourceDirectories in Compile).value
val resourceMappings = ((unmanagedResources in Compile).value --- rdirs) pair (relativeTo(rdirs) | flat)
val resourceMappings = (playExternalizedResources in Compile).value
resourceMappings.map {
case (resource, path) => resource -> ("conf/" + path)
}
Expand All @@ -195,6 +194,27 @@ object PlaySettings {
"../conf" +: scriptClasspath.value
} else scriptClasspath.value
},
// taskDyn ensures we only build the sans externalised jar if we need to
scriptClasspathOrdering <<= Def.taskDyn {
val oldValue = scriptClasspathOrdering.value
if (externalizeResources.value) {
Def.task {
// Filter out the regular jar
val jar = (packageBin in Runtime).value
val jarSansExternalized = (playJarSansExternalized in Runtime).value
oldValue.map {
case (packageBinJar, _) if jar == packageBinJar =>
val id = projectID.value
val art = (artifact in Compile in playJarSansExternalized).value
val jarName = JavaAppPackaging.makeJarName(id.organization, id.name, id.revision, art.name, art.classifier)
jarSansExternalized -> ("lib/" + jarName)
case other => other
}
}
} else {
Def.task(oldValue)
}
},

mappings in Universal ++= {
val docDirectory = (doc in Compile).value
Expand All @@ -220,6 +240,32 @@ object PlaySettings {
generateSecret <<= ApplicationSecretGenerator.generateSecretTask,
updateSecret <<= ApplicationSecretGenerator.updateSecretTask

)
) ++ inConfig(Compile)(externalizedSettings)

/**
* Settings for creating a jar that excludes externalized resources
*/
private def externalizedSettings: Seq[Setting[_]] =
Defaults.packageTaskSettings(playJarSansExternalized, mappings in playJarSansExternalized) ++ Seq(
playExternalizedResources := {
val rdirs = unmanagedResourceDirectories.value
(unmanagedResources.value --- rdirs) pair (relativeTo(rdirs) | flat)
},
mappings in playJarSansExternalized := {
// packageBin mappings have all the copied resources from the classes directory
// so we need to get the copied resources, and map the source files to the destination files,
// so we can then exclude the destination files
val packageBinMappings = (mappings in packageBin).value
val externalized = playExternalizedResources.value.map(_._1).toSet
val copied = copyResources.value
val toExclude = copied.collect {
case (source, dest) if externalized(source) => dest
}.toSet
packageBinMappings.filterNot {
case (file, _) => toExclude(file)
}
},
artifactClassifier in playJarSansExternalized := Option("sans-externalized")
)

}
Expand Up @@ -6,6 +6,7 @@ package controllers
import play.api._
import play.api.mvc._
import play.api.Play.current
import scala.collection.JavaConverters._

object Application extends Controller {

Expand All @@ -16,4 +17,9 @@ object Application extends Controller {
def config = Action {
Ok(Play.configuration.underlying.getString("some.config"))
}

def count = Action {
val num = Play.classloader.getResources("application.conf").asScala.toSeq.size
Ok(num.toString)
}
}
Expand Up @@ -58,3 +58,14 @@ InputKey[Unit]("checkConfig") := {
sys.error(s"Expected config $expected but got $config")
}
}

InputKey[Unit]("countApplicationConf") := {
val expected = Def.spaceDelimited().parsed.head
import java.net.URL
val count = retry() {
IO.readLinesURL(new URL("http://localhost:9000/countApplicationConf")).mkString("\n")
}
if (expected != count) {
sys.error(s"Expected application.conf to be $expected times on classpath, but it was there $count times")
}
}
Expand Up @@ -6,6 +6,7 @@
GET / controllers.Application.index

GET /config controllers.Application.config
GET /countApplicationConf controllers.Application.count

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
Expand Up @@ -3,6 +3,8 @@
$ exists target/universal/stage/README
$ exists target/universal/stage/SomeFile.txt
$ exists target/universal/stage/SomeFolder/SomeOtherFile.txt
$ exists target/universal/stage/lib/dist-sample.dist-sample-1.0-SNAPSHOT-sans-externalized.jar
-$ exists target/universal/stage/lib/dist-sample.dist-sample-1.0-SNAPSHOT.jar

> checkStartScript
$ exists target/universal/stage/conf/application.conf
Expand All @@ -12,6 +14,7 @@ $ exists target/universal/stage/share/doc/api
# Run it to make sure everything works
> testProd --no-exit-sbt
> checkConfig foo
> countApplicationConf 1
> stopProd --no-exit-sbt

# Change the configuration in the conf directory, make sure it takes effect
Expand All @@ -24,4 +27,10 @@ $ copy-file conf/alternate.conf target/universal/stage/conf/application.conf
> set PlayKeys.externalizeResources := false
> stage
-$ exists target/dist/conf/application.conf
-$ exists target/universal/stage/lib/dist-sample.dist-sample-1.0-SNAPSHOT-sans-externalized.jar
$ exists target/universal/stage/lib/dist-sample.dist-sample-1.0-SNAPSHOT.jar
> checkStartScript no-conf
> testProd --no-exit-sbt
> checkConfig foo
> countApplicationConf 1
> stopProd --no-exit-sbt

0 comments on commit 848329b

Please sign in to comment.