2rs2ts
2rs2ts

Reputation: 11066

How to define a custom build setting?

I want my users to be able to define a value in their project/ definition that will be used as a URL for fetching a remote configuration file, which an sbt plugin will in turn use. I can't figure out how to define such a value though. When I try to add it to build.sbt I get this error:

/Users/2rs2ts/src/my-cool-plugin/build.sbt:74: error: not found: value myConfigUrl
myConfigUrl := "http://mycoolwebsite.com/config.xml"
^
[error] Type error in expression

Probably because it's not part of Keys. But I don't know how I'm supposed to add something like this. Even after that point, I don't know how to access the setting in my plugin's Scala source.

Upvotes: 0

Views: 124

Answers (1)

Jacek Laskowski
Jacek Laskowski

Reputation: 74749

Use settingKey macro to define the myConfigUrl key.

A sample build.sbt could be as follows:

lazy val myConfigUrl = settingKey[String]("URL for fetching a remote configuration file")

myConfigUrl := "http://mycoolwebsite.com/config.xml"

A sample session:

➜  my-cool-plugin  xsbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to my-cool-plugin (in build file:/Users/jacek/sandbox/my-cool-plugin/)
> show myConfigUrl
[info] http://mycoolwebsite.com/config.xml

Given the comment where the OP asked:

How can I refer to this in my project code now? I want to be able to access the value I assigned to myConfigUrl in one of my .scala files, not related to the build process.

the key should be defined in build.scala build object as no keys in *.sbt files are visible in project/*.scala files.

Here is a sample project/build.scala build definition with the key:

import sbt._
import Keys._

object build extends Build {
  lazy val myConfigUrl = settingKey[String]("URL for fetching a remote configuration file")

  lazy val mySettings = inConfig(Compile) { Seq(
    myConfigUrl := "http://mycoolwebsite.com/config.xml"
  )}

}

With the Scala build, change build.sbt to be as follows:

mySettings

You could do it since every build file is automatically imported in *.sbt files and hence accessing vals becomes simple like that. To have the settings - the single myConfigUrl key - available in the project you need to add the Seq[Setting] val.

Do reload and the key should be available as if it was before:

> show myConfigUrl
[info] http://mycoolwebsite.com/config.xml

Given the comment:

I'm particularly interested in a way to let end users of my-cool-plugin define their own myConfigUrl which will be used instead of the default one in my-cool-plugin's build.sbt

it's clear on the intent of the key. This is a key of the plugin so just add sbtPlugin := true to the build, publishLocal the project and use addSbtPlugin to declare a plugin dependency on the plugin in another build.

You may want to read about the new feature of sbt 0.13.5 - auto plugins - that further ease setting up the plugin of yours:

As of sbt 0.13.5, there is a new auto plugins feature that enables plugins to automatically, and safely, ensure their settings and dependencies are on a project.

Upvotes: 1

Related Questions