Access SBT settings in current area

I had a problem trying to understand the concept of a sphere in sbt. I want the task to be performed in a certain area and be able to access cloud settings, i.e.

build.sbt

name := "Superapp" name in Test := "Testapp" val printScopedKey = TaskKey[Unit]("psk", "Print Scoped Key") printScopedKey := println("***** [APP NAME] " + name.value) 

I expect the following:

 > test:psk > ***** [APP NAME] Testapp 

Instead of the actual one:

 > ***** [APP NAME] Superapp 

How can i do this in sbt? Is it possible?

+8
sbt
source share
2 answers

The OP wrote: "I would expect the following:"

 > test:psk > ***** [APP NAME] Testapp 

Without actually defining the psk task in the Test configuration, sbt will search for the psk task first in the Global configuration, and then in the order of the projects configurations , which by default is Seq(Compile, Runtime, Test, Provided, Optional) .

So, the following (and @Jacek Laskowski's answer) describes how you can deal with defining tasks in many areas without duplicating code. Customization can be limited to three axes (project, configuration, and task). Part of the project does not enter the game, so we will discuss the configuration and task here.

He recommended that task-specific settings be tied to the task in order to encourage reuse of keys. For example:

 test in assembly := {} 

The Test key above uses the assembly area to manage the tests that run before creating a thick JAR. You can define a "task-generator" method that will take the key and create a settings graph around it:

 def assemblyTask(key: TaskKey[File]): Initialize[Task[File]] = Def.task { val t = (test in key).value val s = (streams in key).value Assembly((outputPath in key).value, (assemblyOption in key).value, (packageOptions in key).value, (assembledMappings in key).value, s.cacheDirectory, s.log) } 

I use this to define assembly , packageScala and packageDependency .

 lazy val baseAssemblySettings: Seq[sbt.Def.Setting[_]] = Seq( assembly := Assembly.assemblyTask(assembly).value, packageScala := Assembly.assemblyTask(packageScala).value, .... ) 

So far, baseAssemblySettings neutral.

If I wanted to use it in configurations like Compile and Test , I would call inConfig(conf)(settings) as follows:

 lazy val assemblySettings: Seq[sbt.Def.Setting[_]] = inConfig(Compile)(baseAssemblySettings) ++ inConfig(Test)(baseAssemblySettings) 

You now have several task schedules in several configurations.

+8
source share

Thanks for the question! At first it seemed to me that I knew the answer, and then I realized that it was not so simple. I had to look for a solution.

I am using sbt 0.13.2-RC1 .

 > about [info] This is sbt 0.13.2-RC1 [info] The current project is {file:/C:/dev/sandbox/0.13.2/}root-0-13-2 0.1-SNAPSHOT [info] The current project is built against Scala 2.11.0-RC3 [info] Available Plugins: org.sbtidea.SbtIdeaPlugin, de.johoop.jacoco4sbt.JacocoPlugin, com.timushev.sbt.updates.UpdatesPlugin [info] sbt, sbt plugins, and build definitions are using Scala 2.10.3 

I found the solution in Mark Harrah's answer to a similar question on the sbt mailing list , which boils down to the following changes to build.sbt :

 scalaVersion := "2.11.0-RC3" name := "Superapp" name in Test := "Testapp" name in Runtime := "Runtimeapp" lazy val psk = taskKey[Unit]("Print Scoped Key") val pskSetting = psk := println("***** [APP NAME] " + name.value) // https://groups.google.com/d/msg/simple-build-tool/A87FFV4Sw4k/KPtygikQvogJ val myPsks = Seq(Compile, Test, Runtime) flatMap { conf => inConfig(conf)( Seq(pskSetting) ) } myPsks 

When you load the assembly file, sbt will automatically recognize that when psk executed, its dependency is name in Compile , and test:psk depends on name in Test . Pretty smart.

 > psk ***** [APP NAME] Superapp [success] Total time: 0 s, completed 2014-03-26 21:27:37 > test:psk ***** [APP NAME] Testapp [success] Total time: 0 s, completed 2014-03-26 21:27:41 > runtime:psk ***** [APP NAME] Runtimeapp [success] Total time: 0 s, completed 2014-03-26 21:27:44 

Use inspect to dig deeper. It is always useful to know how this works under the hood (which is not so difficult to understand once you start using the right tools, such as inspect ).

+5
source share

All Articles