使用 SBT 进行多版本构建 [英] Multi-version build with SBT

查看:28
本文介绍了使用 SBT 进行多版本构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 SBT 中实现以下构建:

I want to implement the following build in SBT:

  • 有多个 Scala 版本
  • 有多个目标版本(因为依赖库)

以1.0、2.0、3.0和Scala 2.92、2.10.2版本中存在的外部Lib为例,我想编译、组装和发布:

Let's take for example an external Lib, that exist in version 1.0, 2.0, 3.0, and Scala 2.92, 2.10.2, I would like to compile, assembly and publish:

  • MyProject_externalLib1.0_scala_2.9.2
  • MyProject_externalLib1.0_scala_2.10.2
  • MyProject_externalLib2.0_scala_2.9.2
  • MyProject_externalLib2.0_scala_2.10.2
  • MyProject_externalLib3.0_scala_2.9.2
  • MyProject_externalLib3.0_scala_2.10.2

但是,默认情况下,我希望在单个版本中工作.只有当我启动一个发布过程时,所有的版本都要编译、测试和组装.

By default, however, I would want to work in a single version. Only when I launch a release processes, all the versions should be compiled and tested and assembled.

有没有一种简单的方法可以在 Sbt 中实现这种多版本构建?

Is there a simple way to implement this multiple-version build in Sbt?

推荐答案

cross building

在 sbt 中拥有多个 Scala 版本是相当普遍的,这被称为交叉构建或交叉发布.请参阅 sbt 的交叉构建页面细节.您所要做的就是:

cross building

Having multiple Scala version in sbt is fairly common, and it's called cross building or cross publishing. See sbt's Cross-building page for details. All you have to do is:

scalaVersion := "2.10.2"

crossScalaVersions := Seq("2.10.2", "2.9.2")  

根据 sbt 的命名约定,您发布的工件将具有二进制版本后修复,对于 Scala 2.10.x 为 _2.10,对于 Scala 2.9 为 _2.9.2.2.这是为了利用 2.10 系列之间的二进制兼容性.您可以使用 crossVersion 来控制该方面.

By sbt's naming convention, your published artifact will have binary version post fix, which is _2.10 for Scala 2.10.x, and _2.9.2 for Scala 2.9.2. This is to take advantage of the binary compatibility among the 2.10 series. You can use crossVersion to control that aspect.

与外部库交叉构建有点棘手.我能想到的壁橱情况是一个用于调度的插件.在那里,我创建了一个名为 dispatchVersion 的自定义设置:

Cross building against an external library is a bit more tricky. The closet situation I can think of is a plugin for Dispatch. In there, I created a custom setting called dispatchVersion:

lazy val dispatchVersion = SettingKey[String]("x-dispatch-version")

这在 buildSettings 中设置为

dispatchVersion := "0.10.1"

和我的版本以 dispatch0.10.0_ 为前缀:

and my version prefixed with dispatch0.10.0_:

version <<= dispatchVersion { dv => "dispatch" + dv + "_0.1.0" }

以下是使用密钥添加依赖项的方法:

Here's how to add dependencies using the key:

libraryDependencies <++= (dispatchVersion) { (dv) => Seq(
  "net.databinder.dispatch" %% "dispatch-core" % dv,
  "net.databinder.dispatch" %% "dispatch-json4s-native" % dv
)}

对于 Java 库,它应该是 % 而不是 %%:

For Java libraries it should be % instead of %%:

libraryDependencies <++= (externalLibVersion) { (el) => Seq(
  "org.apache" % "foo" % el,
  "org.apache" % "foo-extention" % el
)}

除了制作像 sbt-cross-building 这样的自定义插件外,这并不容易迭代一系列外部版本的方法.您可以编写如下的 shell 脚本:

Aside from making a custom plugin like sbt-cross-building, there's no easy way to iterate over a sequence of external versions. You can write a shell script like:

sbt "set externalLibVersion := \"1.0\"" +assembly "set externalLibVersion := \"2.0\"" +assembly "set externalLibVersion := \"3.0\"" +assembly

这篇关于使用 SBT 进行多版本构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆