How to publish to multiple repositories in SBT?
sbt, scala
Solution
With Commands and How to change a version setting inside a single sbt command? I could define a new command - `myPublishTo` - that changes `publishTo` setting before executing the original `publish` task:
def myPublishTo = Command.command("myPublishTo") { state =>
val extracted = Project.extract(state)
Project.runTask(
publish in Compile,
extracted.append(List(publishTo := Some(Resolver.file("file", target.value / "xxx"))), state),
true
)
Project.runTask(
publish in Compile,
extracted.append(List(publishTo := Some(Resolver.file("file", target.value / "yyy"))), state),
true
)
state
}
commands += myPublishTo
With this, execute `myPublishTo` as any other command/task.
You could also define a couple of `alias`es - `pxxx`, `pyyy` and `pxy` - in `build.sbt` that would execute a series of commands using `;`.
addCommandAlias("pxxx", "; set publishTo := Some(Resolver.file(\"file\", target.value / \"xxx\")) ; publish") ++
addCommandAlias("pyyy", "; set publishTo := Some(Resolver.file(\"file\", target.value / \"yyy\")) ; publish") ++
addCommandAlias("pxy", "; pxxx ; pyyy")
In sbt console you can execute them as any other commands/tasks.
[sbt-0-13-1]> alias
pxxx = ; set publishTo := Some(Resolver.file("file", target.value / "xxx")) ; publish
pyyy = ; set publishTo := Some(Resolver.file("file", target.value / "yyy")) ; publish
pxy = ; pxxx ; pyyy
[sbt-0-13-1]> pxy
[info] Defining *:publishTo
[info] The new value will be used by *:otherResolvers, *:publishConfiguration
[info] Reapplying settings...
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
...
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/0.1-SNAPSHOT/sbt-0-13-1_2.10-0.1-SNAPSHOT-javadoc.jar
[success] Total time: 1 s, completed Jan 9, 2014 11:20:48 PM
[info] Defining *:publishTo
[info] The new value will be used by *:otherResolvers, *:publishConfiguration
[info] Reapplying settings...
...
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/yyy/default/sbt-0-13-1_2.10/0.1-SNAPSHOT/sbt-0-13-1_2.10-0.1-SNAPSHOT-javadoc.jar
[success] Total time: 0 s, completed Jan 9, 2014 11:20:49 PM
Problem
I am in the middle of upgrading Nexus version. As part of the process I've set up a new Nexus instance which will run in parallel with the older Nexus instance. While migrating to the new instance I want to thoroughly test and vet the new instance before pulling the plug on older instance. This requires me to temporarily modify the `publish` workflow in such a way that sbt publishes the artifacts to both the Nexus instances. I highly doubt the following code will actually work: ``` publishTo <<= (version) { version: String => if (version.trim.endsWith("SNAPSHOT")) Some("snapshots" at "http://maven1.dev.net:8081/nexus/content/" + "repositories/snapshots/") else Some("releases" at "http://maven1.dev.net:8081/nexus/content/" + "repositories/releases/") }, credentials += Credentials("Sonatype Nexus Repository Manager", "maven1.dev.net", "release-eng", "release"), publishTo <<= (version) { version: String => if (version.trim.endsWith("SNAPSHOT")) Some("snapshots" at "http://maven2.dev.net:8081/nexus/content/" + "repositories/snapshots/") else Some("releases" at "http://maven2.dev.net:8081/nexus/content/" + "repositories/releases/") }, credentials += Credentials("Sonatype Nexus Repository Manager", "maven2.dev.net", "release-eng", "release"), ``` I also tried looking into a plugin called sbt-multi-publish but I couldn't compile and use it, either.