2017-01-20 4 views
0

私の場合、既存のタイプに関数を追加したいと思います。これはNativeLibrarySpecです。Gradle:既存のタイプのDLSを拡張する

私はgradle extensionsで試してみましたが、まだまだ働いていますが、現在はDSLの標準機能であればそのまま使えるように一般化したいです。NativeLibrarySpec 問題は、設定(ブロックが自分の関数を含む)の後にしかアクセスできないため、リンクする前にspecialConfigを呼び出そうとしたために失敗するということです...

ここにコードがあります)この例では、ネイティブソフトウェアC++のためであることを気にしない:ここでは

// File: build.gradle 
apply plugin: 'cpp' 

class SpecialConfig { 
    NativeComponentSpec componentSpec 

    SpecialConfig(NativeComponentSpec componentSpec) { 
     this.componentSpec = componentSpec 
    } 

    def something(boolean enabled) { 
     componentSpec.sources { 
      cpp { 
       // Some important stuffs 
      } 
     } 
    } 
} 

model { 
    components { 
     main(NativeLibrarySpec) { 
      // How to bring this out ?? 
      project.extensions.create('specialConfig', SpecialConfig, it) 

      // This is the new functionality I want to use 
      specialConfig { 
       something(true) 
      } 
     } 
    } 
} 

は、他の例であるが、それは唯一のプロジェクトから動作します* https://dzone.com/articles/gradle-goodness-extending-dsl

答えて

0

をあなたはこのしてください

を試すことができます。
plugins { 
    id 'cpp' 
} 

class SpecialConfig { 
    // no need to pass this to the constructor we can just make it a 
    // method arg instead 
    SpecialConfig() { 
    } 

    // manipulate spec as desired 
    def something(NativeComponentSpec spec, boolean enabled) { 
     println "Hello something!" 
     spec.sources { 
      cpp { 
       // Some important stuffs 
      } 
     } 
    } 
} 

// add your extension earlier 
project.extensions.create('specialConfig', SpecialConfig) 
model { 
    components { 
     main(NativeLibrarySpec) { mySpec -> 
      // we can call our extension via the project 
      project.specialConfig { config -> 
       // our closure is handed an instance of the class 
       // on that we can call methods 
       config.something(mySpec, true) 
      } 
      // or more simply we could make it a one liner 
      project.specialConfig.something(mySpec, true) 
     } 
    } 
} 

出力:

$ gradle build 
Configuration on demand is an incubating feature. 
Hello something! 
:linkMainSharedLibrary UP-TO-DATE 
:mainSharedLibrary UP-TO-DATE 
:createMainStaticLibrary UP-TO-DATE 
:mainStaticLibrary UP-TO-DATE 
:assemble UP-TO-DATE 
:check UP-TO-DATE 
:build UP-TO-DATE 

BUILD SUCCESSFUL 

Total time: 1.104 secs 
+0

はお時間をいただき、ありがとうございます:)残念ながら、私は任意のパラメータを渡すことなく、それを行うための方法を探しています、コードが流暢になります – 56ka

関連する問題