0

I have been looking into how to define a block of variables in a small number of subprojects build.gradle.kts, and read these blocks from a task defined in the root project build.gradle.kts.

SubProject1 build.gradle.kts

dependencies {
    "implementation"("dependency.one:dep-one-api")
    ...
}

additionalProjectSpecificVars {
    val additionalOptions = listOf<String>("option1", "option2", ...)
    val additionalLocations = listOf<String>("$projectDir/location1/...", "$projectDir/location2/...")
}

Root Project build.gradle.kts

subprojects {
    if (project.name.endsWith("-wsdl")) {
        apply(plugin = "no.nils.wsdl2java")

        dependencies {
            ...
        }

        tasks.withType<no.nils.wsdl2java.Wsdl2JavaTask> {
            generatedWsdlDir = file("$projectDir/generatedsources")
            wsdlDir = file("$projectDir/wsdl")
            wsdlsToGenerate = gatherWsdlFiles()
        }
    }
}

fun Project.gatherWsdlFiles() : List<ArrayList<String>> {

    // 2-D Array
    var wsdlsToGenerateList = listOf<ArrayList<String>>()

    // Gather all the files in the WSDL directory
    val wsdlCollection = layout.files({
        file("$projectDir/wsdl").listFiles()
    })

    // Filter out only the .wsdl files
    val wsdlOnlyFiles: FileCollection = wsdlCollection.filter { file: File ->
        file.name.endsWith(".wsdl")
    }

    // Iterate the files collection and add the cxf-parameters
    wsdlOnlyFiles.forEach { wsdl: File ->

        val wsdlArray = listOf<String>(
                "-xjc-Xnamespace-prefix",
                "-xjc-XhashCode",
                "-xjc-Xequals",
                "-xjc-XtoString",
                wsdl.name)

        wsdlsToGenerateList.plus(wsdlArray)
    }


    // Read and add the other options from the subprojects 
    // additionalProjectSpecificVars block here and add to
    // wsdlsToGenerateList list


    return wsdlsToGenerateList
}

I've experimented with project.ext, project.extra, gradle.ext etc, but these seem to be if you want to read a property that has been set in the rootProject, rather than the reverse.

Gradle v.6.2.2

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106

1 Answers1

0

Ok, so the way I solved this was by using the extra[] properties but only during a specific build phase.

In my root build.gradle.kts I updated the above if statement to add the properties after the project evaluation step. If I did not include them during this phase, they did not exist. I then passed the three values as parameters to my gatherWsdlFiles() function.

subprojects {
    if (project.name.endsWith("-wsdl")) {
        apply {
            plugin("no.nils.wsdl2java")
        }

        dependencies {
            ...

            "wsdl2java"("...")
        }

        wsdl2javaExt {
            deleteGeneratedSourcesOnClean = true
        }

        tasks.withType<no.nils.wsdl2java.Wsdl2JavaTask> {

            afterEvaluate {
                val wsdlLocations = extra["wsdlLocations"] as List<String>
                val wsdlBindings = extra["wsdlBindings"] as List<String>
                val wsdlAdditionalOptions = extra["wsdlAdditionalOptions"] as List<String>

                generatedWsdlDir = file("$projectDir/generatedsources")
                wsdlDir = file("$projectDir/wsdl")
                wsdlsToGenerate = gatherWsdlFiles(wsdlLocations, wsdlBindings, wsdlAdditionalOptions)
            }

            tasks.named("compileJava") {
                dependsOn("wsdl2java")
                sourceSets {
                    main {
                        java {
                            srcDirs(listOf("generatedsources"))
                        }
                    }
                }
            }
        }
    }
}

In each of my -wsdl subprojects, I then added the three extra properties to the build.gradle.kts files, each with the relevant values for that project. If there were no values required, then I just initalised an empty list. (Using emptyList() failed with Not enough information to infer type variable T)

extra["wsdlLocations"] = listOf("$projectDir/wsdl/wsdl_1.wsdl")
extra["wsdlBindings"] = mutableListOf<String>()
extra["wsdlAdditionalOptions"] = listOf(
        "-p",
        "http://www.csapi.org/wsdl/blah.service",
        "-p",
        "http://www.csapi.org/wsdl/blah._interface")

dependencies {
    "implementation"("dependency.one:dep-one-api")
}
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106