1

I have the following problem: I need to write a file into a build directory with the content that is defined in the project sbt is going to build.

prj
 |
 |_src/main/scala/FancyScalaFile.scala
 |
 |_build.sbt
 |
 |_project/build.properties

Here is how the FancyScalaFile.scala looks like

object FancyScalaFile {

    def extractString: String() = //...
}

Is there a way to call extractString from build.sbt?

Some Name
  • 8,555
  • 5
  • 27
  • 77
  • 2
    No. Your built tool can not use the logic it is trying to build... However, you can have a Scala file on `project/` which you can use on your build tool. – Luis Miguel Mejía Suárez May 22 '20 at 16:37
  • @LuisMiguelMejíaSuárez Even after the project is built? – Some Name May 22 '20 at 16:43
  • For what do you need that value? – Luis Miguel Mejía Suárez May 22 '20 at 16:47
  • 2
    @LuisMiguelMejíaSuárez Well, actually this is possible. – Dmytro Mitin May 22 '20 at 16:54
  • 2
    @LuisMiguelMejíaSuárez For example sharing some code between build project and main project can be useful for multi-staging programming when during next stage you use code generated during previous stage (pre-compile-time replacement for macros) and you want to use build time as one of stages. Although I surely can't know actual OP's use case. – Dmytro Mitin May 26 '20 at 00:11
  • 1
    @DmytroMitin Actually I have a Scala file that is able to generate a documentation. I want to use it to build a package with the documentation during the build phase. – Some Name May 26 '20 at 10:52

1 Answers1

3

Yes, you can add additional source directory to project/build.sbt (create this file if it doesn't exist; do not confuse it with build.sbt in the project root directory)

Compile / unmanagedSourceDirectories += baseDirectory.value / ".." / "src" / "main" / "scala"

After that you'll be able to call FancyScalaFile.extractString() in build.sbt.

For example if FancyScalaFile is

object FancyScalaFile {
  def extractString(): String = "scala-reflect"
}

then you can write the following in build.sbt

libraryDependencies += scalaOrganization.value % FancyScalaFile.extractString() % scalaVersion.value

and project is built successfully adding scala-reflect dependency.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66