Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up sbt to publish to artifactory based on git branch

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".

like image 279
mitchus Avatar asked May 10 '16 16:05

mitchus


1 Answers

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")                             
}
like image 57
mitchus Avatar answered Oct 17 '22 23:10

mitchus