Problem with model{} block when switching from 2.2.1 to 2.4

Hello, we are currently in the process of transitioning 250+ java and cpp projects from make scripts to gradle.

We use a custom gradle plugin as a base for a set of cpp projects. Inside the plugin we specify toolChains, platforms, etc. and a set of helper tasks.

From plugin:

@Override
public void apply(Project project) {

	project.with {

		apply plugin: 'cpp'
		
		[...]

		model {
			toolChains {
				visualCpp(org.gradle.nativeplatform.toolchain.VisualCpp) {
					installDir output
					windowsSdkDir windowsSdk
				}
			}
			platforms {
				win32 {
					architecture "x86"
					operatingSystem "windows"
				}
				win64 {
					architecture "x86_64"
					operatingSystem "windows"
				}
			}
		}

		[...]
	}
}

Since switching from 2.2.1 to 2.4 we are no longer able to apply our plugin to a project build script and configure the project components.

From build script:

apply plugin: 'standardCppProject'

model {

	buildTypes {
		debug
		release
	}

	components {
		hello(NativeLibrarySpec) {
			binaries.all {
				sources {
					source {
						srcDir "src/hello/cpp"
						include "**/*.cpp"
					}
				}
			}
		}
	}
}

We get the following error message:

Cannot add Mutate rule ‘model.buildTypes’ for model element ‘buildTypes’ when element is in state GraphClosed.

Do you have an idea which would give us an oportunity to furthermore use a plugin to specify some parts of the model block in a plugin and other parts in the build script?

I have a similar issue:
C plugin error in 2.4

Hi,

It’s unclear what’s happening based on the report. Would you be able to boil it down to a small reproducible sample and share it so that we can run it?

Hi, i´ve attached an example which reproduces the error on gradle 2.4. I´ve tried to strip as much as possible. It is basicly just a hello world cpp program and the plugin residing in buildSrc.

CPPBaseExample.zip (3.9 KB)

I was able to solve the problem, by moving back the execution of the problematic code with project.afterEvaluate{}.

In my previously attached example, this means replacing line 34 in com.company.CPPBasePlugin with:

project.afterEvaluate {
    assemble.dependsOn('deployVisualStudio') // <-- THIS triggers the error (not happening in gradle 2.2.1).
}
1 Like