I am using SBT 0.13.16
I have a task that generates a file by calling the main method in the source. This task calculates the path to the file and passes it to the main method as an argument.
build.sbt:
lazy val myTask = taskKey[Unit]("generates file by executing main method") myTask := Def.taskDyn { val file = (resourceManaged in Compile).value / "fileFromTask.txt" val path = file.getAbsolutePath Def.task { (runMain in Compile).toTask(s" com.company.Main $path").value } }.value assembly := (assembly dependsOn myTask).value resourceGenerators in Compile += Def.task { val file = (resourceManaged in Compile).value / "fileFromTask.txt" Seq(file) }.taskValue Main.scala:
object Main extends App { val path = args(0) val file: Path = Paths.get( path ) Files.createDirectories(file.getParent) Files.write(file, "Hi! I am a simple text file".getBytes(StandardCharsets.UTF_8)) } I cannot understand why the sbt assembly command generates a file (I see it in the target\scala-2.12\resource_managed folder), but does not add it to the jar ?
BUT!
If you type sbt myTask assembly , then the file, as expected initially, will be added to the jar . But this is the same thing (in my opinion), which is just the sbt assembly . Why is that?
I noticed that if the file is generated in the task itself, and not in the main method, then the sbt assembly can also add it to the jar . Why?