mogli
mogli

Reputation: 1609

display source directories from sbt task for current project as well as source directories of project it dependsOn

sbt sourceDirectories

only displays source directories of current project, but doesn’t display source directories of projects that it depends using

dependsOn(ProjectRef)

Below is the simplified task.

lazy val showAllSourceDirs = taskKey[Unit]("show source directories of all projects")
showAllSourceDirs := {
  val projectRefs = loadedBuild.value.allProjectRefs.map(_._1)
  projectRefs foreach { projectRef =>

    /*
      Below line is giving IllegalArgumentException exception :-
             
      [error] java.lang.IllegalArgumentException: Could not find proxy for projectRef: sbt.ProjectRef in
      List(value projectRef, value $anonfun, method sbtdef$1, method $sbtdef, object $7fb70afe92bc9a6fedc3,
      package <empty>, package <root>) (currentOwner= method $sbtdef )
     */

    val sources = (projectRef / Compile / sourceDirectories).value
    sources.foreach( println )
  }
}

Link for simplified project to reproduce problem :-


https://github.com/moglideveloper/Example


Steps :


Go to ApiSpec directory from command line and run below command :-


sbt showAllSourceDirs

Expected output : print all source directories of Api and ApiSpec project


Actual output : throws java.lang.IllegalArgumentException

Upvotes: 1

Views: 343

Answers (1)

Tomer Shetah
Tomer Shetah

Reputation: 8529

I believe you cannot do that, because sbt works with macros. What you can do here, is adding sourceDirectories of Api into sourceDirectories of ApiSpec. This means, that if you add the following into your sbt:

Compile / sourceDirectories ++= (apiModule / Compile / sourceDirectories).value

Then, when running:

sbt sourceDirectories

You get the output:

[info] * /workspace/games/Example/ApiSpec/src/main/scala-2.12
[info] * /workspace/games/Example/ApiSpec/src/main/scala
[info] * /workspace/games/Example/ApiSpec/src/main/java
[info] * /workspace/games/Example/ApiSpec/target/scala-2.12/src_managed/main
[info] * /workspace/games/Example/Api/src/main/scala-2.12
[info] * /workspace/games/Example/Api/src/main/scala
[info] * /workspace/games/Example/Api/src/main/java
[info] * /workspace/games/Example/Api/target/scala-2.12/src_managed/main

There is one thing you need to notice - you are overriding the current sourceDirectories, so be sure you are not using it anywhere else.

Another note, is that you need to add this line on every dependency you have. So I am not sure how big is your project, and how feasible that is.

If you'd like to have a different task, you can do that, bur use the modules themselves, and not thru reflection, again due to macros.

lazy val showAllSourceDirs = taskKey[Unit]("show source directories of all projects")
showAllSourceDirs := {
  println((apiSpecProject / Compile / sourceDirectories).value)
  println((apiModule / Compile / sourceDirectories).value)
}

Upvotes: 2

Related Questions