2017-09-06 47 views
0

我們公司有一個Gradle腳本插件,其中包含許多任務。例如,它包括從this answer的Jacoco afterEvaluate塊:如何使用Gradle腳本插件創建我自己的配置塊?

def pathsToExclude = ["**/*Example*"] 

jacocoTestReport { 
    afterEvaluate { 
     classDirectories = files(classDirectories.files.collect { 
      fileTree(dir: it, exclude: pathsToExclude) 
     }) 
    } 
} 

我們想採取pathsToExclude變量,定義,在我們build.gradle文件,並有邏輯的腳本插件其餘的(姑且稱之爲company-script-plugin.gradle 。例如:

apply from: http://example.com/company-script-plugin.gradle 

companyConfiguration { 
    pathsToExclude = ["**/*Example*"] 
} 

我們最初的想法是在生成腳本中添加任務,這樣我們就可以得到一個companyConfiguration

task companyConfiguration { 
    ext.pathsToExclude = [] 
} 

但是,我們認爲這是一個拙劣的解決方法,因爲運行該任務不會執行任何操作。什麼是創建我自己的配置塊的正確方法?

我們希望它儘可能簡單,如果可能的話,做一個腳本插件(而不是二進制插件)。

回答

0

在這裏,你已經一個例子可以怎麼做:

apply plugin: CompanyPlugin 

companyConfiguration { 
    pathsToExclude = ['a', 'b', 'c'] 
} 

class CompanyPlugin implements Plugin<Project> { 

    void apply(Project p) { 
    println "Plugin ${getClass().simpleName} applied" 
    p.extensions.create('companyConfiguration', CompanyConfigurationExtension, p) 
    } 

} 

class CompanyConfigurationExtension { 
    List<String> pathsToExclude 

    CompanyConfigurationExtension(Project p) { 
    } 

} 

task printCompanyConfiguration { 
    doLast { 
    println "Path to exclide $companyConfiguration.pathsToExclude" 
    } 
} 

另外,請看看在docs

+0

我不清楚這是如何與腳本插件一起使用的。我試着從':company-script-plugin.gradle''後面加'apply plugin:CompanyPlugin'申請,但得到「無法獲得未知屬性'CompanyPlugin'」。我誤解了你的建議嗎? – Thunderforge

+0

@Thunderforge,只需將'CompanyPlugin'添加到'company-script-plugin.gradle'文件中,並保留'apply from:...'塊。 – Opal

相關問題