Search code examples
javagradlegradle-plugin

Extension method is not detected (Unresolved reference: overlays)


Problem
I am trying to get the plugin "io.freefair.war-overlay" to work. Based on the documentation, it should work, but it doesn't.

I created an empty Gradle project (wrapper version 8.0) and used the following build.gradle.kts:

import io.freefair.gradle.plugins.maven.war.WarOverlay

plugins {
    id("java")
    id("io.freefair.war-overlay") version "8.0.1"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

war {
    overlays {}
}

The documentation (https://docs.freefair.io/gradle-plugins/current/reference/#_io_freefair_war_overlay) states that this should work. But now Gradle gives the error: "Unresolved reference: overlays".

Debugging
I looked at the plugin source code, which can be found here: https://github.com/freefair/gradle-plugins/blob/master/maven-plugin/src/main/java/io/freefair/gradle/plugins/maven/war/WarOverlayPlugin.java. You can clearly see this:

            NamedDomainObjectContainer<WarOverlay> warOverlays = project.container(WarOverlay.class, name -> new WarOverlay(name, warTask));
            warTask.getExtensions().add("overlays", warOverlays);

This leads me to believe that the "overlays" is indeed added as an extension to the war task. If I try to debug this, you can see it sort of works:

tasks.withType<org.gradle.api.tasks.bundling.War> {
    println(extensions["overlays"])
    // Adding "overlays {}" here also fails.
}

// Prints: WarOverlay container

Strangely enough, the following doesn't work:

war {
    println(extensions["overlays"])
}

// Prints error: Extension with name 'overlays' does not exist. Currently registered extension names: [ext, base, defaultArtifacts, sourceSets, reporting, javaToolchains, java, testing]

So apparently there is something different between "war" and grabbing the related task. Also in the related task.

What am I doing wrong? It seems that it should work, and I cannot see my mistake.


Solution

  • The documentation is in Groovy, and you have to convert it to Kotlin. I don't know what is a "proper" way to do it, but this worked for me:

    import io.freefair.gradle.plugins.maven.war.WarOverlay
    
    tasks.withType<org.gradle.api.tasks.bundling.War> {
        val extension: NamedDomainObjectContainer<WarOverlay> = extensions["overlays"] as NamedDomainObjectContainer<WarOverlay>
        extension.create("foo") {
            from("<dependency>@war")
            enabled = true
            provided(false)
        }
    }