2014-09-26 42 views
4

例如:如果我有一個gradle文件,它可以接受並使用名爲DB 的屬性,該屬性用於對名稱進行測試。如何在gradle中記錄項目屬性?

gradle test將運行對一些默認

gradle test -PDB=asd1將運行鍼對一個特定的實例

,但有沒有簡單的方法來顯示一個特定屬性的處理

作爲用戶我的用戶會期望它在gradle tasks輸出

我應該定義一些任務,例如advancedOptions並列出這些東西在該任務的輸出T'

回答

1

目前沒有好的方法來使屬性自我記錄。但這是一個很好的問題。你介意在Gradle forum上發佈這個問題嗎?這當然是對Gradle核心有幫助的東西。目前,用戶將不得不依賴項目的文檔。

+0

鏈接到gradle論壇問題:http://forums.gradle.org/gradle/topics/how-to -document項目 - 屬性 - 在-gradle這個 – 2015-01-13 08:32:27

0

我想,你可以利用任務rules的(但我不知道它是如何工作與gradle tasks)或準備如果可能的測試環境的列表,併爲每個位置創建任務

僞代碼:

def envs = ['e1', 'e2', 'e3'] 

envs.each { e -> 
    projects.task(e) << { 
     //TODO 
    } 
} 

設想二將打印的所有任務列表。

+0

我也嘗試過這種方法,但我最終每個任務都有3-4個參數,有時候沒有緊密連接的參數;在這種情況下可以做到這一點,但這是另一種痛苦 – 2014-10-08 09:59:01

+0

你能否準備好預期的輸入和輸出的例子? – Opal 2014-10-08 10:10:04

+0

爲每個可能的變化添加單獨的任務將產生一個龐大的任務列表,這將是沒有意義的(並且會在維護期間造成巨大的開銷);我無法舉一個例子,因爲這是一個懸而未決的問題...我很欣賞任何想法 – 2014-10-08 11:01:03

0

你可以「文件」,在任務描述。

task test { 
    description = "Does stuff against thing declared by property 'db'. Ex: gradle test -Ddb=foo' 
} 

當運行gradle tasks時,它會顯示在任務名稱旁邊。

通過CLI將參數傳遞給任務的另一種方法是使用Gradle的@Option批註,但這是一個可能更改的內部API。

+0

任務描述很好;但對於交叉屬性,例如可以改變所有測試任務行爲的屬性,我會看看@Option – 2014-09-27 08:54:48

+0

記錄具有全局行爲的屬性會有點棘手。像這樣的事情,我會考慮放在一個'gradle.properties'文件,以便您可以評論他們。 – 2014-09-27 20:59:03

+0

這些屬性都有可行的默認值; gradle.properties在VCS上;屬性由插件處理,我不想更新所有模塊屬性文件,因爲我添加了一個新的屬性,這是處理 – 2014-10-08 10:01:46

2

我應該說我從ANT到搖籃和我的項目是不是基於Java的開始,所以很可能我不會做事總是在最佳gradle這個或常規的方式...

那說,我還想記錄命令行屬性,並驗證與值一起輸入的值(如果它們只接受特定的值列表)。 我的方法是將可能的命令行屬性映射到包含可能值列表的對象,其中第一個爲默認值,其餘爲其他可接受值,因此可以驗證命令行上設置的屬性。 該對象還有一個描述字段,我用它來記錄屬性,非常類似於任務描述字段。 除了自動設置命令行屬性默認值之外,這允許我驗證命令行屬性,並在定製「描述」任務中顯示此信息以及關鍵任務描述(作爲幫助保留)

下面是一些示例代碼,當編譯開始確認命令行屬性此設置命令行屬性信息和運行(爲簡便起見,我沒有在這裏檢查顯示的值):

task setCommandLineDefaults() { 
    description 'set defaults for missing command line properties, and defines the possible values for each' 
    project.ext.cmdLineDefaults = [ 
     _platform: new CmdLineInfo(values:["ios", "googleplay", "kyowon", "k_ios", "fuhu", "air", "awe"], description: "k_ios is for kyowon on ios - we may want to use this prop for just the OS target and have the partnerID separate down the road...") 
     , _debug: new CmdLineInfo(values:["true", "false"], description:"if true, sets compiler-directives so that debug code (e.g., logging) can be excluded from the compiled code") 
     , _showStats: new CmdLineInfo(values:["true", "false"], description: "if not set on the command line, the setting of _debug is used for the default") 
     , _connect: new CmdLineInfo(values:["false", "true"], description: "only is relevant for DEBUG true, looks for an IP debug connection on startup if true") 
     , _buildType: new CmdLineInfo(values:["dev", "adhoc", "release"], description: "(adhoc is ios only)") 
     , _buildLevel: new CmdLineInfo(values:["", "alpha", "beta", "release"], description: "default is value in version.properties") 
     ... 
    ] 
    // create the 'documentation' text for the properties 
    project.ext.cmdLineInfo = "Command line info: this list comprises the only command line settings allowed, the first item in values is the default, if more than one, then those are the only valid values" 
    cmdLineDefaults.each() { prop, value -> 
     cmdLineInfo += "\n$prop: $value" 
    } 
    // verify that any command line properties set are expected 
    def cmdLineProps = cmdLineDefaults.keySet() as String[] 
    String errStr = "" 
    def alts 
    gradle.startParameter.projectProperties.each() { prop, value -> 
     def values = cmdLineDefaults[prop]?.values 
     if (!values) { 
      alts = getClosestStrings(cmdLineProps, prop, 3) // this uses fuzzy string comparison to get at most 3 of the properites the user might have intended 
      errStr += "Command line property '${prop}' not known, did you mean one of these? $alts \n" 
     } 
    } 
    if (errStr) { 
     throw new InvalidUserDataException("$errStr ${cmdLineInfo}") 
    } 
} 

所以像命令行

gradle compile -P_platfrm=ios

將返回如下錯誤:

Command line property '_platfrm' not known, did you mean one of these? [_platform]

然後我也有描述使用該屬性信息上面還有任務描述設置任務:

task describe() {} 
describe << { 
    // tasks to leave out of description list (as I mentioned, my project is not Java, so a lot of built in stuff is not relevant) 
    def excludeTasks = ["buildDependents","buildNeeded","check","checkForFatalErrors","classes","compileJava","compileTestJava","jar","javadoc","processResources","processTestResources","test","testClasses"] 
    project.tasks.each { 
     if (it.description && !excludeTasks.contains(it.name)) { 
      println "task ${it.name}: ${it.description}" 
     } 
    } 
    println "\n${cmdLineInfo}" 
} 

我喜歡這個比剛纔好「任務」任務,因爲我的項目中存在大量重複和不相關的信息,這也增加了屬性描述。 希望這對某人有幫助。