Prasanna
Prasanna

Reputation: 3771

Gradle: Configure distribution task for select sub projects

I have a multi project gradle build. I want to configure distribution task only for 2 of the sub projects.

Lets assume I have a root project and sub projects A, B & C. I want to configure distribution task for B & C only.

The following way works:
root_project/build.gradle

subprojects{

   configure ([project(':B'), project(":C")]) {

       apply plugin: 'java-library-distribution'
       distributions {
       main {
            contents {
                from('src/main/') {
                    include 'bin/*'
                    include 'conf/**'
                }
            }
        }
    }
}

But I am interested in making it work this way

subprojects{

   configure (subprojects.findAll {it.hasProperty('zipDistribution') && it.zipDistribution}) ) {

       apply plugin: 'java-library-distribution'
       distributions {
       main {
            contents {
                from('src/main/') {
                    include 'bin/*'
                    include 'conf/**'
                }
            }
        }
    }
}

And in build.gradle for B & C, I will have the following:

ext.zipDistribution = true

In the latter approach I have the following 2 problems:

Problem 1

* What went wrong:
Task 'distZip' not found in root project 'root_project'.

* Try:
Run gradle tasks to get a list of available tasks.

Problem 2

I tried to verify whether the property zipDistribution can be read in the root_project with the following code

subprojects {
    .....
//    configure ([project(':B'), project(":C")]) {

        apply plugin: 'java-library-distribution'
        distributions {
            /* Print if the property exists */

            println it.hasProperty('zipDistribution')
            main {
                contents {
                    from('src/main/') {
                        include 'bin/*'
                        include 'conf/**'
                    }
                }
            }
        }
//    }

      .....
}

The above prints null for it.hasProperty('zipDistribution').

Can someone let me what is the right approach so that I do not see these problems?

Upvotes: 2

Views: 3763

Answers (1)

Blanka
Blanka

Reputation: 7501

This is because subprojects are configured after the root project. Which is why ext.zipDistribution is null at that point in time (it hasn't been set yet).

You need to use afterEvaluate to avoid that:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('zipDistribution') && project.zipDistribution) {
            ....
        }
    }
}

Upvotes: 1

Related Questions