How to set project.eclipseJdt.inputFile in a custom Plugin?

I have the following in .gradle script (which I’m applying to build.gradle)

apply plugin: "eclipse"
project.eclipseJdt {
    def jdtInputFile ="${project.properties.buildTemplates}/for/eclipse/org.eclipse.jdt.core.prefs"
    inputFile=project.file("${jdtInputFile}")
    doLast { println "Forked eclipseJdt.inputFile from ${jdtInputFile} for project '${project.name}'" }
}

Instead of the script, I would like to use a custom plugin.

How would I achieve the same using my plugin, e.g.

class ForkEclipseJdtPlugin implements Plugin<Project>{
      @Override
    public void apply(final Project project) {
        project.getPlugins().apply("eclipse")
        //TODO: how to achive forking the eclipse jdt inputfile here?
    }
}

I got it.

class ForkEclipseJdtPlugin implements Plugin<Project>{
      @Override
    public void apply(final Project project) {
        project.extensions.create("forkJdt", ForkEclipseJdtExtension)
        project.getPlugins().apply("eclipse")
        project.apply {
            project.eclipseJdt {
                def jdtInputFile ="${project.properties.buildTemplates}/for/eclipse/org.eclipse.jdt.core.prefs"
                inputFile=project.file("${jdtInputFile}")
                doLast { println "Forked eclipseJdt.inputFile from ${jdtInputFile} for project '${project.name}'" }
            }
        }
    }
}
class ForkEclipseJdtExtension{
    String jdtInputFile
}

However I’m still reading the jdtInput file from the properties, how would that work using ‘forkJdt’ extension, because values for the extension exists (have values) only on project.eclipseJdt.doLast closure?

The ‘project.eclipseJdt { … }’ code should work as-is in the binary plugin. ‘inputFile = project.file("${jdtInputFile}")’ can be simplified to ‘inputFile = project.file(jdtInputFile)’.

How about using forkJdt extension to configure the jdtInputFile, how would that change the project.eclupseJdt { … } code? I tried to set inputFile = project.forJdt.jdtInputFile but it didn’t work.

in build.gradle I have

apply plugin: ForkEclipseJdtPlugin
forkJdt { jdtInputFile = "${project.properties.buildTemplates}/for/eclipse/org.eclipse.jdt.core.prefs" }

It didn’t work -> it didn’t set the inputFile for jdt but used the default input file.

Plugins must defer all read access to the build model until (at least) the end of the configuration phase. There are several techniques for doing so, such as wrapping the read access with ‘gradle.projectsEvaluated { … }’ or using the internal convention mapping mechanism. For more information on this topic, search this forum or Stack Overflow for similar questions.

The ‘project.apply {’ line should be removed.

Thanks, gradle.projectsEvaluated did the trick.

class ForkEclipseJdtPlugin implements Plugin<Project>{
      @Override
    public void apply(final Project project) {
        project.extensions.create("forkJdt", ForkEclipseJdtExtension)
        project.getPlugins().apply("eclipse")
        project.apply {
            project.gradle.projectsEvaluated {
                project.eclipseJdt {
                    def jdtInputFile ="${project.forkJdt.jdtInputFile}"
                    inputFile=project.file(jdtInputFile)
                    doLast { println "Forked eclipseJdt.inputFile from ${jdtInputFile} for project '${project.name}'" }
                }
            }
        }
    }
}
class ForkEclipseJdtExtension{
    String jdtInputFile
}

‘project.apply {}’ is only for applying plugins. It should be removed. ‘public’ can be omitted. ‘getPlugins()’ can be simplified to ‘plugins’. The ‘inputFile’ handling can be simplified to ‘inputFile = project.forkJdt.jdtInputFile’.