這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
設定檔是工程中常用的初始化參數的配置方式,而設定檔的格式有很多種,不同的作業系統、程式設計語言都會有不同的設定檔的格式,本文羅列了一些常見的設定檔的格式。
不同的設定檔格式有不同的方便使用性, 對於功能的支援也有簡單和複雜之分,很難簡單說那種設定檔是最好的,有時候需要從多個方面去考慮, 比如Windows較早的開發喜歡使用int、java喜歡使用properties、通用的編程喜歡yaml、json等格式,本文也不會對這些格式進行排名,而是簡單介紹一下這些格式,使用者可以根據自己的實際情況進行選擇。
ini
ini檔案是一個無固定標準格式的設定檔。它以簡單的文字與簡單的結構組成,常常使用在Windows作業系統。ini檔案的命名來源,是取自英文“初始(Initial)”的首字縮寫,正與它的用途——初始化程式相應。
檔案格式比較簡單, 分為 節、參數、注釋。下面是一個簡單的ini設定檔:
https://github.com/Microsoft/Windows-driver-samples/blob/master/print/v4PrintDriverSamples/v4PrintDriver-HostBasedSampleDriver/usb_host_based_sample-manifest.ini
12345678910111213 |
[DriverConfig]DriverCategory=PrintFax.PrinterDataFile=usb_host_based_sample.gpd; Note: Please replace the GUID below when building a production driver.PrinterDriverID={00000000-0000-0000-0000-000000000000}Flags=HostBasedDeviceEventFile=usb_host_based_sample_events.xmlRequiredFiles=UNIRES.DLL,STDNAMES.GPD,MSXPSINC.GPD[BidiFiles]BidiUSBFile=usb_host_based_sample_extension.xmlBidiUSBJSFile=usb_host_based_sample.js |
properties
properties是一種主要在Java相關技術中用來儲存應用程式的可配置參數的檔案的副檔名。它們也可以儲存用於國際化和本地化的字串,這種檔案被稱為屬性資源套件(Property Resource Bundles)。
每個參數被儲存為一對字串:一個儲存名稱參數(被稱為“鍵”),另一個儲存值。
每個properties 檔案中的行通常儲存單個屬性。對於每一行可能有這麼幾種格式,包括鍵=值,鍵 = 值,鍵:值,以及鍵 值。
.properties檔案可以使用井號(#)或歎號(!)作為一行中第一個非空白字元來表示它後面的所有文本都是一個注釋。反斜線(\)用於逸出字元。下面是一個properties檔案:
https://github.com/netgloo/spring-boot-samples/blob/master/spring-boot-mysql-springdatajpa-hibernate/src/main/resources/application.properties
12345678910111213141516 |
# ===============================# = DATA SOURCE# ===============================# Set here configurations for the database connection# Connection url for the database "netgloo_blog"spring.datasource.url = jdbc:mysql://localhost:3306/netgloo_blog?useSSL=false# Username and passwordspring.datasource.username = rootspring.datasource.password = root# Keep the connection alive if idle for a long time (needed in production)spring.datasource.testWhileIdle = truespring.datasource.validationQuery = SELECT 1 |
json
JSON(JavaScript Object Notation)是一種輕量級的資料交換語言,以文字為基礎,且易於讓人閱讀。儘管JSON是Javascript的一個子集,但JSON是獨立於語言的文字格式設定,並且採用了類似於C語言家族的一些習慣。
JSON 資料格式與語言無關,脫胎於 JavaScript,但目前很多程式設計語言都支援 JSON 格式資料的產生和解析, 所以也常常用作設定檔。
JSON用於描述資料結構,有以下形式存在。
- 對象(object):一個對象以
{開始,並以}結束。一個對象包含一系列非排序的名稱/值對,每個名稱/值對之間使用,分區
- 名稱/值(collection):名稱和值之間使用
:隔開,一般的形式是:{name:value}
- 值的有序列表(Array):一個或者多個值用,分區後,使用
[,]括起來就形成了這樣的列表
- 字串:以""括起來的一串字元。
- 數值:一系列0-9的數字組合,可以為負數或者小數。還可以用e或者E表示為指數形式。
- 布爾值:表示為true或者false。
JSON的格式描述可以參考RFC 4627。
下面是一個json設定檔的例子:
https://github.com/hashicorp/vault/blob/master/command/server/test-fixtures/config.hcl.json
123456789101112131415161718192021 |
{"listener": [{"tcp": {"address": "127.0.0.1:443"}}],"cluster_cipher_suites": "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA","storage": {"consul": {"foo": "bar","disable_clustering": "true"}},"telemetry": {"statsite_address": "baz"},"max_lease_ttl": "10h","default_lease_ttl": "10h","cluster_name":"testcluster","ui":true} |
xml
xml是一種標記語言。標記指電腦所能理解的資訊符號,通過此種標記,電腦之間可以處理包含各種資訊的文章等。
XML是從1995年開始有其雛形,並向W3C(全球資訊網聯盟)提案,而在1998年二月發布為W3C的標準(XML1.0)。
XML設計用來傳送及攜帶資料資訊,所以也經常用來做設定檔。
xml執行個體:
https://github.com/kpavlov/spring-cloud-config-sample/blob/master/src/main/resources/spring-config.xml
12345678910111213141516171819 |
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:property-placeholder location=" ${config.uri}/company.properties, classpath:product.properties, ${config.uri}/${product.name}/${product.version}/common.properties, ${config.uri}/${product.name}/${product.version}/${config.stage}/stage.properties, ${config.uri}/${product.name}/${product.version}/${config.stage}/${config.node}/node.properties" system-properties-mode="FALLBACK"/> <context:annotation-config/> <context:component-scan base-package="spring"/></beans> |
yaml
YAML(/ˈjæməl/,尾音類似camel駱駝)是一個可讀性高,用來表達資料序列的格式。Clark Evans在2001年首次發表了這種語言,另外Ingy döt Net與Oren Ben-Kiki也是這語言的共同設計者。目前已經有數種程式設計語言或指令碼語言支援(或者說解析)這種語言。
YAML是"YAML Ain't a Markup Language"(YAML不是一種標記語言)的遞迴縮寫。在開發的這種語言時,YAML 的意思其實是:"Yet Another Markup Language"(仍是一種標記語言,但為了強調這種語言以資料做為中心,而不是以標記語言為重點,而用反向縮減語重新命名。
YAML的文法和其他進階語言類似,並且可以簡單表達清單、散列表,標量等資料形態。它使用空白符號縮排和大量依賴外觀的特色, 比較適合用來表達階層式的資料結構。
YAML 1.2 是 JSON 格式的超集。
https://github.com/prometheus/prometheus/blob/master/config/testdata/conf.good.yml
123456789101112131415161718192021 |
# my global configglobal: scrape_interval: 15s evaluation_interval: 30s # scrape_timeout is set to the global default (10s). external_labels: monitor: codelab foo: barrule_files:- "first.rules"- "my/*.rules"remote_write: - url: http://remote1/push write_relabel_configs: - source_labels: [__name__] regex: expensive.* action: drop - url: http://remote2/push |
toml https://zh.wikipedia.org/wiki/TOML
TOML是一種旨在成為一個小規模、便於使用的語義化的設定檔格式,它被設計為可以無二義性的轉換為一個雜湊表。
“TOML”這個名字是“Tom's Obvious, Minimal Language(湯姆的淺顯的、極簡的語言)”的首字母略寫詞。“Tom”指它的作者Tom Preston-Werner。
TOML已在一些軟體工程中使用,並且可在很多程式語言中執行。
TOML的文法廣泛地由key = "value"、[節名]與#注釋構成。
它支援以下資料類型:字串、整形、浮點型、布爾型、日期時間、數組和圖表。
123456789101112131415161718192021222324252627282930313233 |
# This is a TOML document.title = "TOML Example"[owner]name = "Tom Preston-Werner"dob = 1979-05-27T07:32:00-08:00 # First class dates[database]server = "192.168.1.1"ports = [ 8001, 8001, 8002 ]connection_max = 5000enabled = true[servers] # Indentation (tabs and/or spaces) is allowed but not required [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10"[clients]data = [ ["gamma", "delta"], [1, 2] ]# Line breaks are OK when inside arrayshosts = [ "alpha", "omega"] |
HOCON
HOCON,全稱Human-Optimized Config Object Notation(人性化設定物件標記法)是一種人類可讀的資料格式,並是JSON和properties的一個超集。它由Lightbend(用Scala開發的人都知道)開發,主要與Play架構結合使用。它也在Puppet中作為配置格式使用。
基本上也算是Scala官方開發,所以在Scala的一些項目中得到使用。由於它是JSON和properties格式的超集,所以它的格式比較靈活。
格式定義可以參考官方文檔: HOCON。
下面是它的一個執行個體:
https://github.com/marcospereira/play-i18n-hocon/blob/master/samples/scala/conf/application.conf
12345678910111213 |
play.http.secret.key = "changeme"play.modules { # Disable built-in i18n module disabled += play.api.i18n.I18nModule # Enable Hocon module enabled += com.marcospereira.play.i18n.HoconI18nModule}play.i18n { langs = [ "en" ]} |
plist
在OS X的Cocoa,NeXTSTEP和GNUstep編程架構中,屬性列表(Property List)檔案是一種用來儲存序列化後的對象的檔案。屬性列表檔案的副檔名為.plist,因此通常被稱為plist檔案。
Plist檔案通常用於儲存使用者佈建,也可以用於儲存捆綁的資訊,該功能在舊式的Mac OS中是由資源分支提供的。
https://github.com/wahlmanj/com.plex.pms.plist/blob/master/com.plex.pms.plist
1234567891011121314151617181920212223 |
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>Label</key><string>com.plex.pms</string><key>KeepAlive</key><true/><key>ProgramArguments</key><array><string>/Applications/Plex\ Media\ Server.app/Contents/MacOS/Plex\ Media\ Server</string></array><key>RunAtLoad</key><true/><key>UserName</key><string>__USERNAME__</string><key>WorkingDirectory</key><string>/Applications</string><key>ServiceDescription</key><string>PMS</string></dict></plist> |