Christian Bongiorno
Christian Bongiorno

Reputation: 5648

gradle multiproject task configuration from plugin - How do I configure each project?

I have tried repeatedly over the years to embrace gradle and I often come upon the same problem when it comes to configuring subprojects.

I have a directory containing multi msbuild projects. They may or may not have a sln file and if they don't, they need to just be skipped. However, where there is one, I am trying to configure an msbuild plugin. I have tried 30 different combinations and read all over the internet - All I need to do is be able to configure msbuild in the subproject and tell it where the solution file is. And, no matter what I try, gradle tells me I have unknown symbols.

I know the plugin is applied based on some debug.

Can someone explain how to configure these?

in settings.gradle.kts

rootProject.name = File(".").canonicalFile.name
File(".").let{
    start -> start.walkTopDown().filter { it.extension == "sln" }.map { it.path.split("/")[1] } }.forEach { include(it) 
}

in build.gradle.kts

subprojects {
    apply(plugin = "com.ullink.msbuild")
    apply(plugin = "com.ullink.nuget")

    tasks.register("hello") {
        doLast {
            println("I'm ${this.project.name}")
        }
    }
    msbuild {
        solutionFile = this.buildDir.walkTopDown().find { it.extension == "sln" }
        projectName = project.name
        verbosity = "normal"
        targets = listOf("Clean", "Rebuild")
        configuration = "Release"
    }
    tasks.findByName("msbuild")?.let{
        // this outputs
        it.javaClass.declaredMethods.map{ it.name }.forEach { println(it) }
    }
}

Here are the errors:

  Line 15:     msbuild {
               ^ Unresolved reference: msbuild

  Line 16:         solutionFile = this.buildDir.walkTopDown().find { it.extension == "sln" }
                   ^ Unresolved reference: solutionFile

  Line 17:         projectName = project.name
                   ^ Unresolved reference: projectName

  Line 18:         verbosity = "normal"
                   ^ Unresolved reference: verbosity

  Line 19:         targets = listOf("Clean", "Rebuild")
                   ^ Unresolved reference: targets

  Line 20:         configuration = "Release"
                   ^ Unresolved reference: configuration

**

**

I have since converted the build script to groovy and, as I suspected, it's because kotlin is being quite stringent. The below script works even though I don't like the solution file find code. If someone could explain how to make this work in Kotlin, that'd be hot

plugins {
  id "com.ullink.msbuild" version "3.8" apply(false)
  id "com.ullink.nuget" version "2.20" apply(false)
}

subprojects{
  apply plugin: 'com.ullink.msbuild'
  apply plugin: 'com.ullink.nuget'

  msbuild{
    solutionFile = {
      def result
      this.project.projectDir.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.sln/) {result = it}
      result
    }()
    projectName = project.name
    verbosity = "normal"
    targets = ["Clean", "Rebuild"]
    configuration = "Release"
  }
}

results:

$ gradle msbuild

> Task :workflowadaptor-cwi_fl_lake_ui:msbuild
Build started 4/27/2020 1:36:27 PM.
Project "/Users/cbongiorno/dev/xx/prism/PRISM-WFAdaptors/workflowadaptor-cwi_ri_statewide_ui/CWI_RI_STATEWIDE_UISol/CWI_RI_STATEWIDE_UISol.sln" on node 1 (Clean;Rebuild target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "/Users/cbongiorno/dev/xx/prism/PRISM-WFAdaptors/workflowadaptor-cwi_ri_statewide_ui/CWI_RI_STATEWIDE_UISol/CWI_RI_STATEWIDE_UISol.sln" (1) is building "/Users/cbongiorno/dev/xx/prism/PRISM-WFAdaptors/workflowadaptor-cwi_ri_statewide_ui/CWI_RI_STATEWIDE_UISol/PRISM.WFAdaptor.CWI.RI.STATEWIDE.UI/PRISM.WFAdaptor.CWI.RI.STATEWIDE.UI.csproj" (2) on node 1 (Clean target(s)).
CoreClean:

I found the correct syntax here: And, gradle stops complaining but now, it's not configuring the task as the previous groovy script did. It says the solution file doesn't exist - And, I am sure it does, but I can't even get it to print debug


plugins {
    id("com.ullink.msbuild") version "3.8" apply false
    id("com.ullink.nuget") version "2.20" apply false
}

subprojects {
    apply(plugin = "com.ullink.msbuild")
    apply(plugin = "com.ullink.nuget")

    tasks.existing(com.ullink.Msbuild::class){
        doFirst{
            println("solution is ${solutionFile}")
        }
        dependsOn("nugetRestore")
        solutionFile = this.project.projectDir.walkTopDown().find { it.extension == "sln" }
        projectName = project.name
        verbosity = "normal"
        targets = listOf("Clean", "Rebuild")
        configuration = "Release"
    }
}

Upvotes: 1

Views: 365

Answers (1)

Louis Jacomet
Louis Jacomet

Reputation: 14500

When using the Kotlin DSL, extensions have type safe model accessors only when the plugins are applied with the plugins {} block.

Since that is not possible in a subprojects block, you have to explicitly type them.

So if it was an extension, msbuild { line would need to become something like:

configure<Msbuild> {

Note that you can use the kotlinDslAccessorsReport to get information about the available model elements and how to access them.

However, since msbuild is a task, it can be configured as follows:

plugins {
    id("com.ullink.msbuild") version "3.8" apply(false)
}

import com.ullink.Msbuild

apply(plugin = "com.ullink.msbuild")

tasks.named<Msbuild>("msbuild") {
    projectName = project.name
}

Edited since msbuild is a task and not an extension.

Upvotes: 3

Related Questions