2

Short summary: I would like to change the default publishTo with an sbt task, but only in some cases. I'm trying to do something like:

val setSnapshot = taskKey[Unit]("changeRepo")

setSnapshot := {
  System.out.println(publishTo.value)
  publishTo in ThisBuild := Some("test" at "snapshot.myrepository")
  System.out.println(publishTo.value)
}

name := "Hello"
version := "1.0"
scalaVersion := "2.10.2"

publishTo in ThisBuild := Some("test" at "release.myrepository")

However, the publishTo does not change it's value. I learned that setting keys are assigned only once. Is publishTo a setting key? Is there no way to change the target later?


Context:

We want our server to build snapshots on any commit and releases on tags. We want it to release to two different repositories. Snapshots go in one and releases in another one. Is there a way to even change the isSnapshot configuration?

Ideally we would like to give our CI runner different commands it could run, like:

  • sbt setSnapshot publish
  • sbt setRelease publish

setRelease and setSnapshot would set the corresponding destination.

  • sbt publish

Alternatively if it was possible to just use publish, and then check if "isSnapshot" is true or false and then publish in one or the other repository. However, I haven't even figured out how isSnapshot can be modified, without touching the build.sbt-file itself.

I've been going through some of the build.sbt documentation, but I haven't found the right page yet...

Am I attacking this problem from a completely wrong angle?

skofgar
  • 1,607
  • 2
  • 19
  • 26

2 Answers2

1

there is an example in the sbt documentation on how to publish to separate snapshot repository:

publishTo := {
  val nexus = "https://my.artifact.repo.net/"
  if (isSnapshot.value)
    Some("snapshots" at nexus + "content/repositories/snapshots") 
  else
    Some("releases"  at nexus + "service/local/staging/deploy/maven2")
}

see here for more details: https://www.scala-sbt.org/1.x/docs/Publishing.html#Define+the+repository

lev
  • 3,986
  • 4
  • 33
  • 46
  • 1
    Thanks @lev for that suggestion. I came across that article, however I couldn't figure out how to set the `isSnapshot.value` - can you elaborate how to configure that (possibly without hard-coding that in the build.sbt) or would you use multiple project configurations? – skofgar Oct 14 '18 at 14:02
  • 1
    `isSnapshot` already exists in sbt. you can see the definition here: https://github.com/sbt/sbt/blob/1f8e9c965785b4d883bb7d7c8dddec4ff8e8fbbf/main/src/main/scala/sbt/Keys.scala#L436 and this is how it's initialized: https://github.com/sbt/sbt/blob/1f8e9c965785b4d883bb7d7c8dddec4ff8e8fbbf/main/src/main/scala/sbt/Defaults.scala#L2007 so the code in the example should work as is – lev Oct 14 '18 at 15:41
0

What I was looking for was something like mentioned here

isSnapshot can be manually set:

sbt 'set isSnapshot := true' isSnapshot
> ..
> true

Also, as lev brought up, the version can set the isSnapshot setting. Which basically leads to the same solution (but that was my missing piece).

sbt 'set version := "1.0"' isSnapshot
> ..
> false

when setting the version number to something including the keyword SNAPSHOT (please not it is case sensitive) it will result in a snapshot

sbt 'set version := "1.0-SNAPSHOT"' isSnapshot
> ..
> true
skofgar
  • 1,607
  • 2
  • 19
  • 26