简体   繁体   中英

Change version in build.sbt custom task

I have a custom task defined in build.sbt:

val doSmth = taskKey[Unit]("smth")
doSmth := {
   version := "1.0-SNAPSHOT"
}

But it does not change version. What I actually wanted is to have custom sbt publish task which will publish always the same version to repo I added. Beside that, normal sbt assembly process is using incremental version scheme. So I wanted to make task which will set version, execute sbt publish and then return version to previous value. I've done everything but am blocked in changing version. Is there any way to change value of sbt settings (since they are vals) from build.sbt?

Is this possible at all? I guess I could also copy code from sbt publish command (as someone mentioned on one topic) but that's the worst solution in my opinion.

Thanks

version is a Setting in sbt. Probably the distinction between settings and tasks is the number one thing you need to understand in sbt, else you are going to have really hard time using the tool.

Settings are immutable and initialized when sbt starts (more or less). After that, they cannot change. Tasks on the other hand, are like functions. Everytime you call them, they get re-evaluated.

You see now that it's impossible to have a task mutate a setting. It just doesn't make sense in sbt.

What you can do though, is overshadow a setting in the context of task. This is exactly what you did with version in publish := "SNAPSHOT-1.0" . I don't think there is any other better way to do this.

I found one possible solution, by changing version for sbt publish task, but it is really strange and unintuitive in SBT. For example, I tried

version := sys.env.getOrElse("BUILD_NUMBER", "1.0")

version in publish := "SNAPSHOT-1.0"

I also tried defining different version in Test and Compile configuration with:

version in Compile := sys.env.getOrElse("BUILD_NUMBER", "1.0")
version in Test: = "SNAPSHOT-1.0

but I could not get it to work. SBT behaves really strange. Is it possible at all to use different value of some setting in one task than it's value in all other tasks?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM