2016-03-23 11 views
0

Aşağıdaki Gradle kod var: denedimGradle Ant görevinde ortak kodu nasıl yeniden düzenlerim?

ant.jdiff(destdir: outputDir) { 
    old(name: "platform-${oldVersion}") { 
    oldVersionRoot.eachDirMatch({ dir -> 
     new File("${dir}/src").exists() 
    }) { dir -> 
     dirset(dir: "${dir}/src") 
    } 
    } 

    'new'(name: "platform-${currentVersion}") { 
    currentVersionRoot.eachDirMatch({ dir -> 
     new File("${dir}/src").exists() 
    }) { dir -> 
     dirset(dir: "${dir}/src") 
    } 
    } 
} 

:

final getSrcDirSets = { root -> 
    final result = [] 

    root.eachDirMatch({ dir -> 
    new File("${dir}/src").exists() 
    }) { dir -> 
    result.append(dirset(dir: "${dir}/src")) 
    } 

    result 
} 

ant.jdiff(destdir: outputDir) { 
    old(name: "example-${oldVersion}") { 
    getSrcDirSets(oldVersionRoot) 
    } 

    'new'(name: "example-${currentVersion}") { 
    getSrcDirSets(currentVersionRoot) 
    } 
} 

ama bu aşağıdaki hata neden olur: Ortak kod içine refactored nasıl

Caused by: org.gradle.api.internal.MissingMethodException: Could not find method old() for arguments [{name=example-1.2.3}, build_at5jtticx[email protected]26f75d38] on task ':jdiff'. 

ayrı bir işlev?

cevap

0

DefaultAntBuilder tüm örnekleri etkilenir beri İdeal olmasa da bir yolu, Groovy monkeypatching geçer:

ant.metaClass.jDiff_getSrcDirSets = { root -> 
    root.eachDirMatch({ dir -> 
    new File("${dir}/src").exists() 
    }) { dir -> 
    dirset(dir: "${dir}/src") 
    } 
} 

ant.property(name: "JDIFF_HOME", value: jdiffHome) 
ant.jdiff(
    destdir: outputDir, 
    verbose: 'off', 
    stats: 'on', 
    docchanges: 'off', 
    source: '1.8') { 
    old(name: "${project.name}-${oldVersion}") { 
    jDiff_getSrcDirSets(oldVersionRoot) 
    } 

    'new'(name: "${project.name}-${currentVersion}") { 
    jDiff_getSrcDirSets(currentVersionRoot) 
    } 
}