I would like to set up an sbt project so that it can publish to the proper artifactory repository based on the (git) branch.
The solution proposed for this question suggests to hardcode the repository in the build.sbt file.
However, I would like the master branch to publish to "releases", and another branch to publish to "snapshots", using the same build.sbt file.
Ideally, I would like the following:
val gitBranch = taskKey[String]("Determines current git branch")
gitBranch := Process("git rev-parse --abbrev-ref HEAD").lines.head
publishTo := {
val myArtifactory = "http://some.where/"
if (gitBranch.value == "master")
Some("releases" at myArtifactory + "releases")
else
Some("snapshots" at myArtifactory + "snapshots")
}
but this yields "error: A setting cannot depend on a task".
One near-solution is to work with the sbt-release
plugin, and then to use isSnapshot
(which is a setting) in order to select the repository.
The solution to the original problem is to simply make gitBranch
a setting:
val gitBranch = settingKey[String]("Determines current git branch")
instead of
val gitBranch = taskKey[String]("Determines current git branch")
Note that a setting is only computed once, at the beginning of the sbt session, so this is not suitable if there is any branch-switching within a session.
Thus, the whole code snippet will become:
val gitBranch = settingKey[String]("Determines current git branch")
gitBranch := Process("git rev-parse --abbrev-ref HEAD").lineStream.head
publishTo := {
val myArtifactory = "http://some.where/"
if (gitBranch.value == "master")
Some("releases" at myArtifactory + "releases")
else
Some("snapshots" at myArtifactory + "snapshots")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With