Pergunta

Where should I place my compiled (via Rhino) Javascript classes, for them to be included on the classpath during compilation, and included in the generated dist bundle?

Play Framework 2.1-RC1 + SBT ignores them, sometimes during compilation, and sometimes when generating the dist bundles.

1. Placing generated files in classes/ or classes_managed/

If I place the generated .class files here or here:
target/scala-2.10/classes/ target/scala-2.10/classes_managed/

Then compile and run works fine. But some weird compilation step (what?!) during stage and dist fails: it won't find the compiled classes.

2. Placing classes in dedicated directory

If I place the generated .class files here: target/scala-2.10/compiledjs-classes/

And add a classpath entry to SBT's config:

object ApplicationBuild extends Build {
  ...
  def mainSettings = List(
    ...,
    unmanagedClasspath in Compile <+= (baseDirectory) map { bd =>
      Attributed.blank(bd / "target/scala-2.10/compiledjs-classes")
    },
    ...)

Then compile, run, stage and dist works fine. However! When I unzip and start the generated .zip file (generated via dist), then the application fails at runtime, because the compiled Javascript classes aren't included in the .zip.

3. Placing classes in lib/ folder

Then compile won't find the class files. (I placed e.g. class compiledjs.HtmlSanitizerJs in lib/compiledjs/HtmlSanitizerJs.class.)


What am I supposed to do? I mean, what works and is best practices?

Anyhow, right now I'm copying the generated classes to both
target/scala-2.10/compiledjs-classes/ (so compilation works) and
target/scala-2.10/classes/ (so they're included in the generated .zip)
This works, but feels very wrong.

(( Oddly enough, everything worked okay with an older version of Play Framework (older than RC-1) and when I was using PlayProject rather than play.Project. I then kept the generated classes in target/scala-2.10/classes/, only. ))

Foi útil?

Solução

I couldn't find a way to put classfiles to the jars on the stage task. As a workaround I generate a JAR file on stage and on play run I set the classpath to the "rhino" folder in the base folder.

I created a gist for you, so you can check it out and try: https://gist.github.com/4321216 rhino/so/Example.class represents a Rhino class but is only a Java class that returns a String.

Add in your Build.scala:

  val rhinoJarName = "rhino.jar"
  val folderForRhinoJar = unmanagedBase
  val rhinoClasspath = (baseDirectory) map { base => Attributed.blank(base / "rhino")}
  val rhinoClassesToJar = TaskKey[Unit]("rhino-classes-to-jar")
  val rhinoClassesToJarInitializer = (base: File, folderForJar: File) => {
    val rhinoFolder = new File(base, "rhino")
    val isClassFile = (file: File) => file.getName.endsWith("class")
    val classFiles =  rhinoFolder.***.filter(isClassFile).get
    val sources = classFiles map { file =>
      file -> file.getCanonicalPath.drop(rhinoFolder.getCanonicalPath.length + 1)
    }
    IO.jar(sources, folderForJar / rhinoJarName, new java.util.jar.Manifest())
  }

  val main = play.Project(appName, appVersion, appDependencies).settings(
    unmanagedClasspath in Compile <+= rhinoClasspath,
    unmanagedClasspath in Runtime <+= rhinoClasspath,
    rhinoClassesToJar <<= (baseDirectory, folderForRhinoJar) map rhinoClassesToJarInitializer,
    playStage <<= playStage.dependsOn(rhinoClassesToJar),
    playStage <<= (playStage, folderForRhinoJar) map {(unused, folder) =>
      IO.delete(folder / rhinoJarName)
    },
    cleanFiles <+= folderForRhinoJar { _ / rhinoJarName } //make sure jar will be deleted if play stage does not finish and cleanup jar
  )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top