trying to print the projects version to stdout so ...
# community-support
c
trying to print the projects version to stdout so I can capture it, and only it in a shell variable. It should always be printed
Copy code
var printVersion = tasks.register("printVersion") {
  outputs.upToDateWhen { false }
  println(version)
}
Copy code
> Task :printVersion UP-TO-DATE
Skipping task ':printVersion' as it has no actions.
b
Move the println to a doLast {...} block.
c
although I didn't put this, seems to have the issue that it's not configuration cache safe
1 problem was found storing the configuration cache. - Task
:printVersion
of type `org.gradle.api.DefaultTask`: cannot serialize Gradle script object references as these are not supported with the configuration cache.
I suppose I could always run it with --no-configuration-cache but that feels silly
v
version
is really
project.version
Store
project.version
in a local variable, then CC is happy. Besides that, you don't need the
outputs.upToDateWhen
, because you did not declare any outputs, your task will never be up-to-date anyway. And if you would need to do something like that, better use
doNotTrackState()
instead, but also that is not necessary here.
Copy code
val printVersion by tasks.registering {
  val version = version
  doLast {
    println(version)
  }
}
should do what you want I think
👍 1
Well, not "always", as
println
goes to stdout which goes to lifecycle log level which would be suppressed when using
-q
🙂
c
seemed to be and now doesn't seem to be
❯ ./gradlew printVersion --quiet 0.11.1
this would be a lazy version, meaning that the version (not project.version) won't get calculated until the dolast block is called right?
Copy code
var printVersion = tasks.register("printVersion") {
  val version = project.providers.provider { this.project.extensions.getByType(SemverExtension::class).gitDescribed  }
  doLast { println(version.get()) }
}
v
❯ ./gradlew printVersion --quiet
0.11.1
Huh, ok, must have remembered wrongly, either the log level on which stdout is captured or what quiet displays
this would be a lazy version
yes
meaning that the version won't get calculated until the dolast block is called right?
no Well, depends on whether CC is enabled. Without CC, yes. With CC it would be evaluated at the time the CC entry is serialized. When CC is reused it will not be reevaluated but the value from CC is used. If that is not what you need, you need to use a value source instead.
c
seems to work in this case with/without CC now, I imagine that's because that's reading from git and changing git invalidates that part of the CC
v
Probably depends on how you read from Git.
c
maybe, wasn't able to find a good way out of it though
I actually suspect I'm as lazy as I can get given that the publishing plugins aren't actually lazy
👌 1