vehovmar
vehovmar

Reputation: 1587

Gradle distribution plugin extract common parts to closure

I'm trying to create several distributions of my project using gradle distribution plugin.

I was successful however there was a lot of duplication and I was wondering if there is a way how to define closure to cover similarities in different distributions?

Something like this would be great:

apply plugin: 'distribution'

def commonPart = { location ->
    into('a') {
        from("$projectDir/src/main/config/$location/A")
    }
    into('b') {
        from("$projectDir/src/main/config/$location/B")
    }
    ..
    <lots more>
}

distributions {

    firstPackage {
        contents {
            ['shared', 'concrete-a'].each commonPart
        }
    }

    secondPackage {
        contents {
            ['shared', 'concrete-b'].each commonPart
        }
    }
}

But I'm getting this:

Could not find method firstPackage() for arguments [build_dt0cpe0f6o27n2ggb10318bwh$_run_closure2$_closure10@5e60e639] on project ':test.project'.

Upvotes: 1

Views: 195

Answers (1)

Opal
Opal

Reputation: 84786

It will be:

apply plugin: 'distribution'

def commonPart = { location ->
    return {
         into('a') {
         from("$projectDir/src/main/config/$location/A")
       }
       into('b') {
         from("$projectDir/src/main/config/$location/B")
       }
    }
}

distributions {

    firstPackage {
        ['shared', 'concrete-a'].collect { contents commonPart(it) }
    }

    secondPackage {
        ['shared', 'concrete-b'].collect { contents commonPart(it) }  
    }
}

Here you can find a demo.

Upvotes: 1

Related Questions