1

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. ))

Schleichardt
  • 7,502
  • 1
  • 27
  • 37
KajMagnus
  • 11,308
  • 15
  • 79
  • 127
  • Can you try 2. with a folder that is not in target but in the baseDirectory? – Schleichardt Dec 13 '12 at 12:12
  • @Schleichardt Done. Compilation works, but `stage` and `dist` don't work (because `java.lang.NoClassDefFoundError`, when the generated classes aren't found). – KajMagnus Dec 14 '12 at 22:17
  • And, in SBT, the `classpath` command doesn't list *the-folder-that-is-not-in-target-but-in-the-baseDirectory*, namely `compiledjs-classes` in my case. However, `show full-classpath` *does* list that folder, `compiledjs-classes`. So what I've done seems to be to tell SBT to include the folder when compiling only, but not when generating a dist? – KajMagnus Dec 14 '12 at 22:20
  • In SBT, `clean` won't delete the `compiledjs-classes` folder. – KajMagnus Dec 14 '12 at 22:22
  • for the clean problem, there exists a solution: http://stackoverflow.com/questions/10471596/add-additional-directory-to-clean-task-in-sbt-build – Schleichardt Dec 14 '12 at 23:02

1 Answers1

1

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
  )
Schleichardt
  • 7,502
  • 1
  • 27
  • 37
  • Thanks for this detailed example :-) I'm not sure if it's "better" to place the classes in a JAR and include the JAR in the build, rather than copying the classes directly to the target/scala-2.10/classes/ folder? I think that in my case the latter approach is actually a little bit simpler, since I'm calling a Makefile script, and I kind of just added a `cp …` line. – KajMagnus Dec 18 '12 at 20:06
  • One thing I don't understand, with the example, is: How does Play know to include the generated JAR, when running stage/dist? `rhinoClassesToJar` creates the JAR, since `playStage.dependsOn(rhinoClassesToJar)` — but then what? The JAR gets created but don't one have to tell SBT to include it in the generated file bundles somehow? – KajMagnus Dec 18 '12 at 20:08
  • SBT is sometimes hardly readable. What really happens: I overrite `play dist`. First a JAR with the rhino classes is created into the lib folder (unmanagedBase), then runs the real dist command and sees the rhino.jar and includes it, after that it delete the JAR. – Schleichardt Dec 18 '12 at 21:14