Search code examples
dependenciessbtsbt-plugin

accessing dependent (not child) projects in sbt plugin


Please find below sample build.sbt file that uses our plugin. In this sample BasePlugin, we want to full path to a/project, b/project directory :-

import sbt._
import Keys._
import BasePlugin._


BasePlugin.settings

lazy val root = Project("root", file(".")).dependsOn(
                                                    ProjectRef( uri("../some/where/a"), "a" ),
                                                    ProjectRef( uri("../some/where/b"), "b" )
                                                )       




enablePlugins(BasePlugin)

Also, find below, simplified sbt plugin BasePlugin.scala :-

package base

import sbt.{ThisBuild, Def, TaskKey, AutoPlugin}
import sbt._
import Keys._

/**
 * Created by mogli on 4/23/2017.
 */
object BasePlugin extends AutoPlugin {

  object autoImport {
    lazy val customtask: TaskKey[Unit] = TaskKey("customtask")
  }

  import autoImport.customtask


  override def projectSettings: Seq[Def.Setting[_]] = Seq(
    customtask := {
      //expectation: to get an iterator or collection sort of thing for dependent projects, but they are not in this variable (projectDependencies)
      val deps = projectDependencies
      deps map { c => println("project : " + c) }
    }
  )
}

How to access dependent projects in sbt plugin.


Solution

  • To get the project's dependencies do

    val deps = thisProject.value.dependencies.map { dep => dep.project }

    This will work as expected if you are accessing thisProject within the projectSettings method's body.