How to set defaultTasks depends on buildType at runtime?

Hi,

We have multiple cpp projects in which is some projects create shared library and some creates static library.

Currently I mentioned the buildTypes in one gradle file as:

buildTypes {
        debug
        release
            }

And for each project I have specified the default tasks as;

defaultTasks "debug${project.name}StaticLibrary", "release${project.name}StaticLibrary"
// Project which create static libs
defaultTasks "debug${project.name}SharedLibrary", "release${project.name}SharedLibrary" // Project which create shared libs

When I run the gradle command it compiles both Release/Debug version.

However I want to compile the project either in Release mode or Debug mode or both depends on the input parameter. and it should update defaltTasks of all projects depends on BuildType.

like : gradle < parameter for BuildType > Please provide some pointers to solve this problem.

Thanks Mandar

What about…

task release(dependsOn: tasks.matching { it.name.startsWith("release") })
task debug(dependsOn: tasks.matching { it.name.startsWith("debug") })
task both(dependsOn: [ release, debug ])

Usage: gradle release or gradle debug or gradle both

You can do a lot of other things with matching than just matching on name. See http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/TaskCollection.html and http://www.gradle.org/docs/current/userguide/more_about_tasks.html#sec:adding_dependencies_to_tasks

Thanks Sterling.

I could able to solve my requirement with this solution. Posting solution here, it could be useful to others.

//Commadline > gradle -PreleaseBuild -PdebugBuild
  ext {
    libType = 'StaticLibrary'
}
allprojects {
            task relBuild << {
            println "Release build"
        }
    task dBuild << {
            println "Debug build"
        }
          if (project.hasProperty("releaseBuild") && !project.hasProperty("debugBuild")) {
                        relBuild.dependsOn {
            tasks.findAll { task -> task.name.startsWith('release') && task.name.contains("$ext.libType")}
        }
    }
    else if (project.hasProperty("debugBuild") && !project.hasProperty("releaseBuild")) {
                   dBuild.dependsOn {
            tasks.findAll { task -> task.name.startsWith('debug') && task.name.contains("$ext.libType")}
        }
    }
    else {
                  relBuild.dependsOn {
            tasks.findAll { task -> task.name.startsWith('release') && task.name.contains("$ext.libType")}
        }
                  dBuild.dependsOn {
            tasks.findAll { task -> task.name.startsWith('debug') && task.name.contains("$ext.libType")}
        }
                    }
}