-->

Publish asset files of play framework plugin

2019-04-15 14:28发布

问题:

using publishTo in build.sbt file, it can be automation the publishing progress. But asset files( in public folder) does not packaged. how to package and publish asset files?

回答1:

For locally publishing assets files , add this snippet in build.sbt file

import play.PlayImport.PlayKeys._

packagedArtifacts in publishLocal := {
  val artifacts: Map[sbt.Artifact, java.io.File] = (packagedArtifacts in publishLocal).value
  val assets: java.io.File = (playPackageAssets in Compile).value
  artifacts + (Artifact(moduleName.value, "jar", "jar", "assets") -> assets)
}

Important notes in last function line:

first jar specify package type folder (As webJars added as java dependency they should put in jars folder).

second jar specify extention of packaged file.

assets is classifier (more information about sbt classifier)

now you can use packaged assets as webjar in another project using this command in buil.sbt file:

libraryDependencies ++=   "organization" % "pluginName_scalaVersion" % "version" classifier "assets"


回答2:

If you want to add some directory (e. g. "public") into a jar, just add this lines in build.sbt

import NativePackagerHelper._
...
mappings in (Compile, packageBin) ++= directory("public")

From here and here.



回答3:

The question title mentions "plugin" but I'm not sure from the rest of the question whether your intention is to deploy a Play web application to production, push a Play web application to a Maven repository (as in the link) or push a Play plugin to a Maven repo.

Assuming the first case, my own process to deploy a Play web app to production does this with bash scripts:

  1. Run play dist

    (This includes the public assets)

  2. Optional: repackage the archive

    (You can skip this but I found I can reduce the size by about 30%)

  3. Copy the archive to the production host, e.g. with scp.

  4. Unpack the archive to where it is going to run
  5. Unpack from within the archive the public assets and ensure nginx or other front-facing HTTP server serves those assets directly.

This related question may also be useful.