Search code examples
scalasbtplayframework-2.2

scala-sbt change javacOptions in task


I'm trying to create a simple ant task, which depends on compile. The problem I'm having is that I can't seem to set the javacOptions from within my task (only in the global scope).

val metamodelSettings = TaskKey[Unit]("metamodelSettings")
val metamodel = TaskKey[Unit]("metamodel")

metamodelSettings := {
  print("Metamodel generation started")
  javacOptions := Seq(
    "-verbose",
    "-g",
    "-processor", "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor",
    "-s", "app",
    "-proc:only"
  )
}

metamodel := {
    print("Metamodel generation complete")
}

metamodel <<= metamodel.dependsOn(metamodelSettings, compile in Compile)

How can I change the javacOptions just for my metamodel task?

Edit:

[info] This is sbt 0.13.0

$ inspect metamodelSettings:javacOptions
[info] Task: scala.collection.Seq[java.lang.String]
[info] Description:
[info]  Options for the Java compiler.
[info] Provided by:
[info]  */*:javacOptions
[info] Defined at:
[info]  (sbt.Defaults) Defaults.scala:209
[info] Delegates:
[info]  metamodelSettings:javacOptions
[info]  *:javacOptions
[info]  {.}/metamodelSettings:javacOptions
[info]  {.}/*:javacOptions
[info]  */metamodelSettings:javacOptions
[info]  */*:javacOptions
[info] Related:
[info]  *:metamodelSettings::javacOptions
[info]  compile:javacOptions
[info]  *:metamodel::javacOptions
[info]  */*:javacOptions
[info]  compile:doc::javacOptions

show metamodelSettings::javacOptions is showing the expected values, but still not using them.

[test] $ show metamodelSettings::javacOptions
[info] List(-verbose, -g, -processor, org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor, -s, app, -proc:only)
[success] Total time: 0 s, completed 04-Feb-2014 11:29:56

Solution

  • As I was unable to override the settings, I resorted to invoking the compiler directly. This seems like a nasty solution to me, but maybe somebody will find it useful.

    val metamodel = TaskKey[Unit]("metamodel")
    
    javacOptions := Seq("-proc:none")
    
    cleanFiles <++= baseDirectory (_ ** "*_.java" get)
    
    metamodel := {
      managedClasspath in Compile <<= (classpathTypes, update) map {
        (ct, report) =>
          Classpaths.managedJars(Compile, Set("jar", "zip"), report)
      }
      val javac = (Keys.compileInputs in Keys.compile in Compile).value.compilers.javac
      val src = (file("app") / "models") ** "*.java"
      val cp = (managedClasspath in Compile).value.map(x => x.data)
      val out = file("app/models")
      val args = Seq(
        "-processor", "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor",
        "-s", "app",
        "-proc:only"
      )
      javac(sources=src.get, classpath=cp, outputDirectory=out, options=args)(ConsoleLogger())
    }