Leo Orientis
Leo Orientis

Reputation: 1061

How to define a custom sbt setting in project/{...}.scala?

Using sbt 1.0.1 and scala 2.12.3...

My directory structure:

project/build.properties
project/PackageDist.scala
src/main/scala/{$packageDirs}/MyClass.scala
build.sbt

In project/PackageDist.scala , I've got:

import sbt._
import Keys._
import io.RichFile._

import java.io.File

object PackageDist {
  lazy val distArtifactRoot = settingKey[File]("The directory to which all distribution artifacts will be written")
  lazy val distDirectoryName = settingKey[String]("The name of the root diectory for the distribution")

  def distArtifactRootFile : Setting[File] = 
    distArtifactRoot := target.value / "dist"
  def distDirectory : Setting[String] =
    distDirectoryName := s"${normalizedName.value}-${version.value}"
}

In build.sbt , I've got:

lazy val commonSettings = Seq(
  organization := "myTestOrg",
  scalaVersion := "2.12.3",
  version := "0.1.0-SNAPSHOT"
)

lazy val root = ( project in file(".") )
  .settings(
    name := "package-test",
    commonSettings
  )

When I run sbt , the distArtifactRoot and distDirectoryName settings are not available when I run > settings -V.

What am I doing wrong?

Upvotes: 0

Views: 320

Answers (1)

pedromss
pedromss

Reputation: 2453

You need to make your settings visible to other sbt files:

object PackageDist {
  lazy val distArtifactRoot = settingKey[File](
    "The directory to which all distribution artifacts will be written")
  lazy val distDirectoryName =
    settingKey[String]("The name of the root diectory for the distribution")

  val settings = Seq(
    distArtifactRoot := target.value / "dist",
    distDirectoryName := s"${normalizedName.value}-${version.value}"
  )
}

Then you use them in any project like

lazy val root = ( project in file(".") )
  .settings(PackageDist.settings: _*) // <-- Add the settings to this project
  .settings(
    name := "package-test",
  )

Reloading sbt and typing dist + make the autocomplete suggest the 2 settings you added.

Upvotes: 1

Related Questions