Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sbt: How to define task for all projects?

Tags:

scala

sbt

I would like to be able to define a task for all projects in my sbt.build:

lazy val project1 = project.in(`.` /  "project1)
   ...
lazy val project2 = 
   ...

lazy val upload = taskKey[Unit]("upload a config file from project to server)
upload := { 
   val file = baseDirectory.value / "config.json"
   ...
}

The problem is this definition works only when I call sbt upload, but I would like to be able to call it for each subproject: sbt project1/upload and sbt project2/upload.

Is there a way to do it, without using inputKey?

like image 386
Ford O. Avatar asked Jul 17 '18 08:07

Ford O.


People also ask

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.

Can sbt execute tasks in parallel?

By default, sbt executes tasks in parallel (subject to the ordering constraints already described) in an effort to utilize all available processors. Also by default, each test class is mapped to its own task to enable executing tests in parallel.

How do I run a sbt task?

Run “sbt hello” from command line to invoke the task. Run “sbt tasks” to see this task listed.


1 Answers

See Organizing the build:

For more advanced users, another way of organizing your build is to define one-off auto plugins in project/*.scala. By defining triggered plugins, auto plugins can be used as a convenient way to inject custom tasks and commands across all subprojects.

project/UploadPlugin.scala

package something

import sbt._
import Keys._

object UploadPlugin extends AutoPlugin {
  override def requires = sbt.plugins.JvmPlugin
  override def trigger = allRequirements

  object autoImport {
    val upload = taskKey[Unit]("upload a config file from project to server")
  }
  import autoImport._

  override lazy val projectSettings = Seq(
    upload := {
      val n = name.value
      println(s"uploading $n..")
    }
  )
}

build.sbt

Here's how you can use it:

ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.12.5"
ThisBuild / version      := "0.1.0-SNAPSHOT"

lazy val root = (project in file("."))
  .aggregate(project1, project2)
  .settings(
    name := "Hello"
  )

lazy val project1 = (project in file("project1"))

lazy val project2 = (project in file("project2"))

build.sbt does not have to mention anything about UploadPlugin, since it's a triggered plugin. From the shell you can call:

sbt:Hello> project1/upload
uploading project1..
[success] Total time: 0 s, completed Jul 20, 2018
sbt:Hello> project2/upload
uploading project2..
[success] Total time: 0 s, completed Jul 20, 2018
like image 136
Eugene Yokota Avatar answered Oct 13 '22 20:10

Eugene Yokota