Dynamic task setting in parent build.gradle

I have a multi-project C ++ Gradle build that creates several libraries and executables. I am trying to get subprojects of executable files (but not libraries) to compile with a fingerprint object. This works great if I cover something like this in separate build.gradle subprojects:

 compileMain.doFirst { // code to generate a 'BuildInfo.cpp' from from a template. // embeds name of executable in so has to be generated anew for each exe } 

Following the principles of DRY, I would rather do it once and for all at the top level of build.gradle . This is my attempt to apply it only to subprojects using the cpp-exe plugin by following these instructions :

 configure(subprojects.findAll { it.plugins.hasPlugin('cpp-exe') }) { compileMain.doFirst { // same code as above } } 

Alas, this does not work. However, if I put smth, like this, in a less restrictive configure block, this demonstrates that the idea of ​​a plugin request should work:

 configure(subprojects.findAll { true }) { task mydebug << { if ( project.plugins.hasPlugin( 'cpp-exe' ) ) { println ">>> $project.name has it!" } } } 

Could it be that plugins are not applied to subprojects during the evaluation of configure close (at the top level of build.gradle )? Maybe a much easier way to achieve this?

+4
source share
2 answers

You are probably using the cpp-exe plugin in building scripts for child projects. By default, the parent assembly of the script is evaluated before its children, which explains why it does not find projects that have cpp-exe .

There are several ways to solve this problem. One way is to move the entire configuration specific to the cpp-exe project (for example, using a plugin and adding an action) to the same place. Either you make such a configuration from the parent script assembly (for example, listing the cpp-exe subprojects and setting them up with one configure(cppExeProjects) { ... } ), or move the specific cpp-exe configuration to your own script construct (say gradle/cpp-exe.gradle ) and apply it to the selected subprojects as follows: apply from: "$rootDir/gradle/cpp-exe.gradle" .

Another solution is to reorder the build scripts. But I would use it only as a last resort, and it certainly is not needed here.

+4
source

Gradle 1.5 recently, I'm not sure if this is a new feature, but what it looks like, you can solve the problem using afterEvaluate . Take a look at section 53.6.1 at http://www.gradle.org/docs/current/userguide/build_lifecycle.html

Sort of:

 subprojects {subProject -> afterEvaluate { if ( subProject.plugins.hasPlugin('cpp-exe')){ println "Project $subProject.name has plugin cpp-exe" } } } 

will give you a start.

+1
source

Source: https://habr.com/ru/post/1403068/


All Articles