Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sbt Task classpath

I'm working on a sbt Task and I would like to have access to some of the application classes and dependencies. (Specifically, I'd like to generate the Database DDL using scalaquery)

Is there any way to add those dependencies to the task or maybe I need to create a plugin for this?

object ApplicationBuild extends Build {

  val appName = "test"
  val appVersion = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    "org.scalaquery" % "scalaquery_2.9.0-1" % "0.9.5")

  val ddl = TaskKey[Unit]("ddl", "Generates the ddl in the evolutions folder")

  val ddlTask = ddl <<= (baseDirectory, fullClasspath in Runtime) map { (bs, cp) =>
    val f = bs / "conf/evolutions/default" 

    // Figures out the last sql number used
    def nextFileNumber = { ... }

    //writes to file
    def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) { ...}

    def createDdl = {
      import org.scalaquery.session._
      import org.scalaquery.ql._
      import org.scalaquery.ql.TypeMapper._

      import org.scalaquery.ql.extended.H2Driver.Implicit._
      import org.scalaquery.ql.extended.{ ExtendedTable => Table }
      import models._
      printToFile(new java.io.File(nextFileNumber, f))(p => {
          models.Table.ddl.createStatements.foreach(p.println)
      });
    }
    createDdl
    None
  }

  val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
    ddlTask)

}

The error I get is

[test] $ reload
[info] Loading global plugins from /home/asal/.sbt/plugins
[info] Loading project definition from /home/asal/myapps/test/project
[error] /home/asal/myapps/test/project/Build.scala:36: object scalaquery is not a member of package org
[error]       import org.scalaquery.session._
[error]                  ^
[error] one error found

Thanks in advance

like image 936
mericano1 Avatar asked Mar 23 '12 10:03

mericano1


1 Answers

You have to add ScalaQuery and everything else your build depends on as a build dependency. That means that basically, you have to add it "as an sbt plugin".

This is described in some detail in the Using Plugins section of the sbt wiki. It all boils down to a very simple thing, though - just add a line defining your dependency under project/plugins.sbt like this:

libraryDependencies += "org.scalaquery" % "scalaquery_2.9.0-1" % "0.9.5"

Now, the problem with using application classes in the build is that you can't really add build products as build dependencies. - So, you would probably have to create a separate project that builds your DDL module, and add that as dependency to the build of this project.

like image 152
Joachim Hofer Avatar answered Oct 08 '22 10:10

Joachim Hofer