1. sbt 啟動器組態

sbt 啟動器組態 

啟動器可以透過下列方式之一進行配置,優先順序依序遞增

  • 取代啟動器 jar 中的 /sbt/sbt.boot.properties 檔案
  • 將名為 sbt.boot.properties 的組態檔案放在類別路徑上。將其放在類別路徑的根目錄中,且不帶 /sbt 字首。
  • 在命令列上指定替代組態的位置,可以是路徑或絕對 URI。這可以透過將位置指定為系統屬性 sbt.boot.properties 或將位置指定為啟動器的第一個引數並加上 @ 字首來完成。系統屬性的優先順序較低。相對路徑的解析會先嘗試使用目前工作目錄,然後使用使用者主目錄,最後使用包含啟動器 jar 的目錄。

如果這些嘗試都失敗,則會產生錯誤。

範例 

作為應用程式的 sbt 的預設組態檔案看起來像這樣

[scala]
  version: ${sbt.scala.version-auto}

[app]
  org: ${sbt.organization-org.scala-sbt}
  name: sbt
  version: ${sbt.version-read(sbt.version)[0.13.5]}
  class: ${sbt.main.class-sbt.xMain}
  components: xsbti,extra
  cross-versioned: ${sbt.cross.versioned-false}

[repositories]
  local
  typesafe-ivy-releases: https://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
  maven-central
  sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots

[boot]
  directory: ${sbt.boot.directory-${sbt.global.base-${user.home}/.sbt}/boot/}

[ivy]
  ivy-home: ${sbt.ivy.home-${user.home}/.ivy2/}
  checksums: ${sbt.checksums-sha1,md5}
  override-build-repos: ${sbt.override.build.repos-false}
  repository-config: ${sbt.repository.config-${sbt.global.base-${user.home}/.sbt}/repositories}

讓我們詳細查看所有啟動器組態區段

1. Scala 組態 

[scala] 區段用於配置 Scala 的版本。它具有一個屬性

  • version - 應用程式使用的 Scala 版本,如果應用程式不是跨版本,則為 auto
  • classifiers - 要解析的其他 Scala 成品的 (選用) 清單,例如來源。

2. 應用程式識別 

[app] 區段會配置啟動器如何使用 Ivy 相依性管理員來尋找您的應用程式。它包含下列屬性

  • org - 與 Ivy 模組相關聯的組織。(Maven 術語中的 groupId)
  • name - Ivy 模組的名稱。(Maven 術語中的 artifactId)
  • version - Ivy 模組的版本。
  • class - 應用程式的「進入點」名稱。進入點必須是符合下列條件之一的類別

    • 擴充 xsbti.AppMain 介面。
    • 擴充 xsbti.ServerMain 介面。
    • 包含簽名為 static void main(String[]) 的方法
    • 包含簽名為 static int main(String[]) 的方法
    • 包含簽名為 static xsbti.Exit main(String[]) 的方法
  • components - Ivy 應解析的其他元件的選用清單。
  • cross-versioned - 一個選用字串,表示此應用程式的發佈方式。如果 app.cross-versioned 是二進位檔,則解析的模組 ID 為 {app.name+'_'+CrossVersion.binaryScalaVersion(scala.version)}。如果 app.cross-versionedtruefull,則解析的模組 ID 為 {app.name+'_'+scala.version}。指定 scala.version 屬性時,不得為 auto 且為跨版本。
  • resources - 應新增至應用程式類別路徑的 jar 檔案的選用清單。
  • classifiers - 應使用此應用程式解析的其他分類器的選用清單,例如來源。

3. 儲存庫區段 

[repositories] 區段會配置 Ivy 將在何處以及如何尋找您的應用程式。每一行都表示 Ivy 將尋找的儲存庫。

  • 注意:此區段配置 Ivy 將尋找的預設位置,但這可以透過使用者組態覆寫。

有數個內建字串可用於常見的儲存庫

  • local - 本機 Ivy 儲存庫 ~/.ivy2/local
  • maven-local - 本機 Maven 儲存庫 ~/.m2/repository
  • maven-central - Maven Central 儲存庫 repo1.maven.org

除了內建儲存庫之外,其他儲存庫可以使用下列語法進行配置

name: url(, pattern)(,bootOnly)(,descriptorOptional)(,skipConsistencyCheck)(,allowInsecureProtocol)

name 屬性是 Ivy 用於快取從此位置解析的模組的識別碼。name 在所有儲存庫中應為唯一。

url 屬性是 Ivy 應尋找模組的基本 url

pattern 屬性是 Ivy 應如何 尋找模組的選用規格。依預設,啟動器假設儲存庫採用 maven 樣式格式。

bootOnly 字串用於告知 Ivy 僅在啟動期間使用此儲存庫。例如,尋找 sbt 自己的 JAR 和任何外掛程式的 JAR。具有 bootOnly 字串的儲存庫將不會用於建置時的相依性解析。

skipConsistencyCheck 字串用於告知 Ivy 不要驗證其解析的檔案的總和檢查碼和簽名。

allowInsecureProtocol 字串會告知 SBT 不要輸出關於此儲存庫為 http:// 的警告。在使用 HTTP 儲存庫之前請仔細考慮,因為它們可能會帶來重大的安全性風險

4. Boot 區段 

[boot] 區段用於配置 sbt 啟動器儲存其快取和組態資訊的位置。它包含下列屬性

  • directory - 此處定義的目錄用於儲存已解析啟動器的所有快取 JAR。
  • properties - (選用) 用於任何 read 變數的屬性檔案。

5. Ivy 區段 

[ivy] 區段用於為解析應用程式配置 Ivy 相依性管理員。它包含下列屬性

  • ivy-home - Ivy 的主目錄。這會決定 ivy-local 儲存庫的位置,以及 Ivy 快取的儲存位置。預設值為 ~/.ivy2
  • checksums - Ivy 應使用以驗證成品是否已正確解析的總和檢查碼的逗號分隔清單,例如 md5 或 sha1。
  • override-build-repos - 如果設定此值,則 xsbti.Launcher 介面上的 isOverrideRepositories 方法會傳回其值。此方法的使用方式與應用程式相關,但在 sbt 的情況下,表示啟動器中儲存庫的組態應覆寫任何建置所使用的組態。應用程式應盡可能遵守此慣例。
  • repository-config - 這會指定一個組態位置,也可以在此位置配置 Ivy 儲存庫。如果此檔案存在,則其內容會覆寫 [repositories] 區段。

6. 伺服器區段 

當使用啟動器的 --locate 功能時,此區段會配置伺服器的啟動方式。它包含下列屬性

  • lock - 控制對執行中伺服器的存取的檔案。此檔案將包含伺服器使用的作用中連接埠,而且必須位於支援鎖定的檔案系統上。
  • jvmargs - 包含啟動伺服器時使用的換行分隔 JVM 引數的檔案。
  • jvmprops - 將在伺服器中定義覆寫屬性的屬性檔案位置。在此檔案中定義的所有屬性都將設定為 -D Java 屬性。

變數替代 

屬性值可能包含變數替代。變數替代具有下列形式之一

  • ${variable.name}
  • ${variable.name-default}

其中 variable.name 是系統屬性的名稱。如果存在該名稱的系統屬性,則會替代該值。如果不存在系統屬性且指定了預設值,則會在以遞迴方式替代其中的變數之後,替代預設值。如果系統屬性不存在且未指定預設值,則不會替代原始字串。

還有一個特殊的變數替代

read(property.name)[default]

這將會在 boot.properties 配置的檔案中查找一個值。如果沒有配置 boot.properties 檔案,或者該屬性不存在,則會選擇預設值。

語法 

設定檔是基於行的,以 UTF-8 編碼讀取,並由以下文法定義。 'nl' 是換行符號或檔案結尾,而 'text' 是不包含換行符號或周圍分隔符號(例如括號或方括號)的純文字。

configuration: scala app repositories boot log appProperties
scala: "[" "scala" "]" nl version nl classifiers nl
app: "[" "app" "]" nl org nl name nl version nl components nl class nl crossVersioned nl resources nl classifiers nl
repositories: "[" "repositories" "]" nl (repository nl)*
boot: "[" "boot" "]" nl directory nl bootProperties nl search nl promptCreate nl promptFill nl quickOption nl
log: "[" "log" "]" nl logLevel nl
appProperties: "[" "app-properties" "]" nl (property nl)*
ivy: "[" "ivy" "]" nl homeDirectory nl checksums nl overrideRepos nl repoConfig nl
directory: "directory" ":" path
bootProperties: "properties" ":" path
search: "search" ":" ("none" | "nearest" | "root-first" | "only" ) ("," path)*
logLevel: "level" ":" ("debug" | "info" | "warn" | "error")
promptCreate: "prompt-create"  ":"  label
promptFill: "prompt-fill" ":" boolean
quickOption: "quick-option" ":" boolean
version: "version" ":" versionSpecification
versionSpecification: readProperty | fixedVersion
readProperty: "read"  "(" propertyName ")"  "[" default "]"
fixedVersion: text
classifiers: "classifiers" ":" text ("," text)*
homeDirectory: "ivy-home" ":" path
checksums: "checksums" ":" checksum ("," checksum)*
overrideRepos: "override-build-repos" ":" boolean
repoConfig: "repository-config" ":" path
org: "org" ":" text
name: "name" ":" text
class: "class" ":" text
components: "components" ":" component ("," component)*
crossVersioned: "cross-versioned" ":"  ("true" | "false" | "none" | "binary" | "full")
resources: "resources" ":" path ("," path)*
repository: ( predefinedRepository | customRepository ) nl
predefinedRepository: "local" | "maven-local" | "maven-central"
customRepository: label ":" url [ ["," ivyPattern] ["," artifactPattern] [", mavenCompatible"] [", bootOnly"]]
property: label ":" propertyDefinition ("," propertyDefinition)*
propertyDefinition: mode "=" (set | prompt)
mode: "quick" | "new" | "fill"
set: "set" "(" value ")"
prompt: "prompt"  "(" label ")" ("[" default "]")?
boolean: "true" | "false"
nl: "\r\n" | "\n" | "\r"
path: text
propertyName: text
label: text
default: text
checksum: text
ivyPattern: text
artifactPattern: text
url: text
component: text