1. sbt new 與模板

sbt new 與模板 

sbt 0.13.13 加入了一個名為 new 的新指令,可從模板建立新的建置定義。new 指令可透過稱為 模板解析器 的機制進行擴充。

嘗試 new 指令 

首先,您需要 sbt 的啟動器版本 0.13.13 或以上。通常 sbt 啟動器的確切版本並不重要,因為它會使用 project/build.propertiessbt.version 指定的版本;但是,由於此指令在沒有 project/build.properties 的情況下也能運作,因此需要新的 sbt 啟動器 0.13.13 或以上版本。

接著,執行

$ sbt new scala/scala-seed.g8
....
name [hello]:

Template applied in ./hello

這會使用 Giter8 執行模板 scala/scala-seed.g8,並提示輸入「name」的值(預設值為「hello」,我們按下 [Enter] 接受),並在 ./hello 下建立建置。

scala-seed 是「最小」Scala 專案的官方模板,但它絕對不是唯一的一個。

Giter8 支援 

Giter8 是一個由 Nathan Hamblen 於 2010 年發起的模板專案,現在由 foundweekends 專案維護。Giter8 的獨特之處在於它使用 GitHub(或任何其他 git 儲存庫)來託管模板,因此允許任何人參與模板建立。以下是一些官方來源提供的模板

如需更多資訊,請參閱 Giter8 Wiki 上的 Giter8 模板。sbt 透過隨附 Giter8 的模板解析器,提供 Giter8 模板的開箱即用支援。

Giter8 參數 

您可以將 Giter8 參數附加到指令的結尾,例如,若要指定特定分支,您可以使用

$ sbt new scala/scala-seed.g8 --branch myBranch

如何建立 Giter8 模板 

如需建立新 Giter8 模板的詳細資訊,請參閱 建立您自己的模板

$ sbt new foundweekends/giter8.g8

使用 CC0 1.0 作為模板授權 

我們建議以 CC0 1.0 授權軟體模板,這會放棄所有版權和相關權利,類似於「公有領域」。

如果您居住在伯恩公約涵蓋的國家/地區(例如美國),版權將在沒有註冊的情況下自動產生。因此,如果您未宣告授權條款,人們將沒有合法權利使用您的模板。棘手的是,即使是寬鬆的授權(例如 MIT 授權和 Apache 授權)也需要在使用模板的軟體中註明您的模板。若要移除對範本程式碼片段的所有主張,請在 CC0 下散佈它,CC0 是國際上等同於公有領域的授權。

License
-------
Written in <YEAR> by <AUTHOR NAME> <AUTHOR E-MAIL ADDRESS>
[other author/contributor lines as appropriate]
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.

如何擴充 sbt new 

本頁的其餘部分說明如何擴充 sbt new 指令,以提供對 Giter8 模板以外的項目支援。如果您對擴充 new 不感興趣,可以略過此章節。

模板解析器 

模板解析器是一個部分函數,它會檢視 sbt new 後面的引數,並判斷它是否可以解析為特定模板。這類似於 resolvers 從網際網路解析 ModuleID

Giter8TemplateResolver 會取得第一個不以連字號 (-) 開頭的引數,並檢查它看起來是否像 GitHub 儲存庫或以「.g8」結尾的 git 儲存庫。如果符合其中一個模式,它會將引數傳遞給 Giter8 進行處理。

若要建立您自己的模板解析器,請建立一個具有 template-resolver 作為相依性的函式庫

val templateResolverApi = "org.scala-sbt" % "template-resolver" % "0.1"

並擴充 TemplateResolver,其定義為

package sbt.template;

/** A way of specifying template resolver.
 */
public interface TemplateResolver {
  /** Returns true if this resolver can resolve the given argument.
   */
  public boolean isDefined(String[] arguments);
  /** Resolve the given argument and run the template.
   */
  public void run(String[] arguments);
}

將函式庫發佈到 sbt 社群儲存庫或 Maven Central。

templateResolverInfos 

接著,建立一個 sbt 外掛程式,將 TemplateResolverInfo 新增至 templateResolverInfos

import Def.Setting
import Keys._

/** An experimental plugin that adds the ability for Giter8 templates to be resolved
 */
object Giter8TemplatePlugin extends AutoPlugin {
  override def requires = CorePlugin
  override def trigger = allRequirements

  override lazy val globalSettings: Seq[Setting[_]] =
    Seq(
      templateResolverInfos +=
        TemplateResolverInfo(ModuleID("org.scala-sbt.sbt-giter8-resolver", "sbt-giter8-resolver", "0.1.0") cross CrossVersion.binary,
          "sbtgiter8resolver.Giter8TemplateResolver")
    )
}

這種間接方式允許模板解析器具有與其餘建置無關的類別路徑。