Setting up sbt to publish to artifactory based on git branch

大兔子大兔子 提交于 2019-12-10 03:36:49

问题


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


回答1:


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")                             
}


来源:https://stackoverflow.com/questions/37144050/setting-up-sbt-to-publish-to-artifactory-based-on-git-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!