Help migrating native projects to Gradle 2.3

I have a plugin that creates a native library ‘main’ I want users of my plugin to be able ot override the deafult location of the source files.

With Gradle 2.2 i could do this:

project.sources.main.cpp {
        source {
            srcDir "."
            include "*.cpp"
        }
    }

but I’m having trouble finding the equivalent in Gradle 2.3. I thought I should b able to do this:

project.components.main.sources.cpp {
        source {
            srcDir "."
            include "*.cpp"
        }
    }

but that just claims that there is no property ‘main’…

I’ll have to admint that I have a hard time figuring out the object model from looking at gradle sample files. For example the release notes have:

model {
    components {
        hello(NativeLibrarySpec)
          main(NativeExecutableSpec) {
            sources {
               cpp {
                    lib library: "hello"
                    source {
                        srcDir "src/source"
                        include "**/*.cpp"
                    }
                    exportedHeaders {
                        srcDir "src/include"
                    }
               }
            }
        }
    }
}

but when I tried:

project.model.components.main.sources.cpp {
        source {
            srcDir "."
            include "*.cpp"
        }
    }

it complained about ‘model’ not being a property.

There are a couple of gotchas that are currently there with the new component model for what you are trying to do. First “components.main” won’t work - components needs to be followed by a closure. I’m not sure off the top of my head whether this is necessary or just a quirk of the current implementation. The other thing is that you must specify a component type when declaring a component (ie “components { main(NativeExecutableSpec) }”). So between those two things, you should be able to do something like this in your plugin:

class NativePlugin implements Plugin<Project> {
    void apply(Project project) {
        project.plugins.apply("cpp")
        project.model {
            components {
                main(NativeExecutableSpec) {
                    sources.cpp.source {
                        srcDir "some/path"
                        include "*.cpp"
                    }
                }
            }
        }
    }
}

Then your users can add new srcDirs in their build script as such:

model {
    components {
        main {
            sources.cpp.source {
                srcDir "some/other/path"
            }
        }
    }
}

If you are sure that the component in question already exists, you can omit the component type when referencing a component in the ‘components’ container.

As Gary pointed out, the important difference is that the dot-notation for referencing nested elements is not currently supported for the new configuration model. It’s not yet certain what the final syntax will look like.