question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

resourceGenerators in Compile does not run on compile (but does run on Run)

See original GitHub issue

start a new sbt project:

build.sbt:

resourceGenerators in Compile <+=
  (resourceManaged in Compile, name, version) map { (dir, n, v) =>
    println("saving git hash to resource - this should happen (only) upon compile")
    val file = dir / "version"
    val contents = Process("git rev-parse HEAD").lines.head
    IO.write(file, contents)
    Seq(file)
  }

hw.scala:

object Hi {
  def main(args: Array[String]) = {
    println("Hi!")
    val version = getClass.getResource("version")
    println(s"running from git hash ${scala.io.Source.fromURL(version).mkString}")
  }
}

Setup git: git init git add . git commit -am "initial commit"

sbt compile it, you see only:

[info] Compiling 1 Scala source to target/scala-2.10/classes...
[success] Total time: 1 s, completed Jan 30, 2015 11:37:20 AM

sbt run it, you see:

saving git hash to resource - this should happen (only) upon compile
[info] Running Hi 
Hi!
running from git hash 83931d8fec29fab2e26e7107b743adfd5d51df88

System setup:

Java HotSpot™ 64-Bit Server VM, Java 1.7.0_76)

[info] This is sbt 0.13.7 [info] The current project is built against Scala 2.10.4 [info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, com.typesafe.sbteclipse.plugin.EclipsePlugin [info] sbt, sbt plugins, and build definitions are using Scala 2.10.4

(Ubuntu 14.04.1)

Related issues: https://github.com/sbt/sbt/issues/1046 https://github.com/sbt/sbt-buildinfo/issues/46 http://stackoverflow.com/questions/28223663/humane-guidance-for-sbt-dsl

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
muuki88commented, Apr 1, 2020

This is how we solved this:

  • define a task that we add to the resourceGenerators
  • trigger task when compile is being called
val yarnBuild: TaskKey[Seq[File]] = taskKey[Seq[File]](
  "run yarn build:prod and returns the generated assets"
)

// in settings
yarnBuild := { ... },
// add to resourceGenerators
Compile / resourceGenerators += yarnBuild.taskValue,
// trigger on compile for hot reloading, e.g. ~reStart
yarnBuild := yarnBuild.triggeredBy(Compile / compile).value
1reaction
larsrhcommented, Feb 22, 2016

@jsuereth You mentioned attaching triggers to the compile task, could you sketch what that would look like?

Read more comments on GitHub >

github_iconTop Results From Across the Web

sbt does not execute generators on compile - Stack Overflow
I have added this to my build.sbt: resourceGenerators in Compile <+= (resourceManaged in Compile, name, version) map { (dir, n, ...
Read more >
sbt Reference Manual — Generating files
sbt provides standard hooks for adding source and resource generation tasks. ... Executing run (or package , not compile ) will add a...
Read more >
sbt/sbt - Gitter
/home/arne/proj/tensorDslScala/build.sbt:36: error: No implicit for Append. ... but then why is resource generators not of type List[Task] and documented as ...
Read more >
<+= operator in 0.13 - Google Groups
1) Is that operator in fact actually going away? ... resourceGenerators in Compile += myTaskKey.value // Error cannot run a task in a...
Read more >
Generating files — sbt Documentation - Huihoo
Executing 'run' will print "Hi". Change Compile to Test to make it a test source. For efficiency, you would only want to generate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found