openjfx / javafx-gradle-plugin

Gradle plugin that makes it easy to work with JavaFX 11+
https://openjfx.io/
BSD 3-Clause "New" or "Revised" License
361 stars 62 forks source link

Please add Kotlin DSL example on how to add variants in the documentation #160

Open MarceloRuiz opened 1 year ago

MarceloRuiz commented 1 year ago

Please improve the documentation to 'tweak' 3rd party configurations by adding a Kotlin DSL example.

I got the following error after trying to apply the quarkus plugin to minimal JavaFx app:

Execution failed for task ':jfxapp:quarkusGenerateCode'.
> Could not resolve all files for configuration ':jfxapp:quarkusProdBaseRuntimeClasspathConfiguration'.
   > Could not resolve org.openjfx:javafx-controls:21.0.1.
     Required by:
         project :jfxapp
      > Cannot choose between the following variants of org.openjfx:javafx-controls:21.0.1:
          - linux-aarch64Runtime
          - linuxRuntime
          - mac-aarch64Runtime
          - macRuntime
          - runtime
          - winRuntime
        All of them match the consumer attributes:
          - Variant 'linux-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'linuxRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'mac-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'macRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'winRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'windows' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it

I read the documentation on how to add the variants to the configuration (in my case with using Kotlin dsl) and after hitting my head to the wall for quite some time, I ended up with something like this:

configurations.named("quarkusProdBaseRuntimeClasspathConfiguration") {
    attributes {
        attribute(
            Attribute.of("java-runtime", Usage::class.java),
            objects.named(Usage::class.java, Usage.JAVA_RUNTIME))
        attribute(
            Attribute.of("linux", OperatingSystemFamily::class.java),
            objects.named(OperatingSystemFamily::class.java, OperatingSystemFamily.LINUX))
        attribute(
            Attribute.of("x86-64", MachineArchitecture::class.java),
            objects.named(MachineArchitecture::class.java, MachineArchitecture.X86_64))
    }
}

but I got nowhere with an even more confusing error:

Execution failed for task ':jfxapp:quarkusGenerateCode'.
> Could not resolve all files for configuration ':jfxapp:quarkusProdBaseRuntimeClasspathConfiguration'.
   > Could not resolve org.openjfx:javafx-controls:21.0.1.
     Required by:
         project :jfxapp
      > The consumer was configured to find attribute 'java-runtime' with value 'java-runtime', attribute 'linux' with value 'linux', attribute 'x86-64' with value 'x86-64'. However we cannot choose between the following variants of org.openjfx:javafx-controls:21.0.1:
          - compile
          - enforced-platform-compile
          - enforced-platform-runtime
          - javadoc
          - linux-aarch64Compile
          - linux-aarch64Runtime
          - linuxCompile
          - linuxRuntime
          - mac-aarch64Compile
          - mac-aarch64Runtime
          - macCompile
          - macRuntime
          - platform-compile
          - platform-runtime
          - runtime
          - sources
          - winCompile
          - winRuntime
        All of them match the consumer attributes:
          - Variant 'compile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'enforced-platform-compile' capability org.openjfx:javafx-controls-derived-enforced-platform:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'enforced-platform' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'enforced-platform-runtime' capability org.openjfx:javafx-controls-derived-enforced-platform:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'enforced-platform' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'javadoc' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'documentation' but the consumer didn't ask for it
                  - Provides org.gradle.dependency.bundling 'external' but the consumer didn't ask for it
                  - Provides org.gradle.docstype 'javadoc' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'linux-aarch64Compile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'linux-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'linuxCompile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'linuxRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'mac-aarch64Compile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'mac-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'macCompile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'macRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'platform-compile' capability org.openjfx:javafx-controls-derived-platform:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'platform' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'platform-runtime' capability org.openjfx:javafx-controls-derived-platform:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'platform' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'sources' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'documentation' but the consumer didn't ask for it
                  - Provides org.gradle.dependency.bundling 'external' but the consumer didn't ask for it
                  - Provides org.gradle.docstype 'sources' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'winCompile' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'windows' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-api' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')
          - Variant 'winRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Doesn't say anything about java-runtime (required 'java-runtime')
                  - Doesn't say anything about linux (required 'linux')
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'windows' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
                  - Doesn't say anything about x86-64 (required 'x86-64')

I also tried the following, using just Strings:

configurations.named("quarkusProdBaseRuntimeClasspathConfiguration") {
    attributes {
        attribute(Attribute.of("org.gradle.native.architecture", String::class.java), "x86-64")
        attribute(Attribute.of("org.gradle.native.operatingSystem", String::class.java), "linux")
        attribute(Attribute.of("org.gradle.usage", String::class.java), "java-runtime")
    }
}

that gives me the following error:

Execution failed for task ':jfxapp:quarkusGenerateCode'.
> Could not resolve all dependencies for configuration ':jfxapp:quarkusProdRuntimeClasspathConfiguration'.
   > Could not resolve org.openjfx:javafx-controls:21.0.1.
     Required by:
         project :jfxapp
      > Cannot choose between the following variants of org.openjfx:javafx-controls:21.0.1:
          - linux-aarch64Runtime
          - linuxRuntime
          - mac-aarch64Runtime
          - macRuntime
          - runtime
          - winRuntime
        All of them match the consumer attributes:
          - Variant 'linux-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'linuxRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'linux' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'mac-aarch64Runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'aarch64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'macRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'macos' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'runtime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
          - Variant 'winRuntime' capability org.openjfx:javafx-controls:21.0.1:
              - Unmatched attributes:
                  - Provides org.gradle.category 'library' but the consumer didn't ask for it
                  - Provides org.gradle.libraryelements 'jar' but the consumer didn't ask for it
                  - Provides org.gradle.native.architecture 'x86-64' but the consumer didn't ask for it
                  - Provides org.gradle.native.operatingSystem 'windows' but the consumer didn't ask for it
                  - Provides org.gradle.status 'release' but the consumer didn't ask for it
                  - Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it

I am clearly doing everything wrong, so an improvement in the documentation would be greatly appreciated. In the meantime, could anyone that knows how I can solve this problem using the Kotlin DSL please tell me how to do so? Thank you!

MarceloRuiz commented 1 year ago

Just in case, the code that solved the problem, written with the Kotlin dsl is bellow. The most important point for my case is that the new attributes are applied to configurations that are resolvable (and in my particular case, filtered to contain the string "quarkus"):

configurations.matching {
    it.isCanBeResolved && it.name.toString().contains("quarkus")
}.configureEach {
    attributes {
        attribute(Usage.USAGE_ATTRIBUTE, objects.named<Usage>(Usage.JAVA_RUNTIME))
        attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named<OperatingSystemFamily>("linux"))
        attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named<MachineArchitecture>("x86-64"))
    }
}

Thanks to Vampire from the Gradle Forums for the answer!