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
354 stars 62 forks source link

Cannot choose between the following variants of JavaFX component modules like org.openjfx:javafx-fxml:21 #159

Open DaiYuANg opened 12 months ago

DaiYuANg commented 12 months ago

When I upgraded the plugin to version 0.1.0, I encountered an Cannot choose between the following variants of JavaFX component modules error. I've already found a solution in the project's README, but my understanding of Gradle is still quite basic. Could someone help me take a look? There is my build.gradle.kts

plugins {
    id("org.javamodularity.moduleplugin")
    id("org.beryx.jlink")
    application
}

dependencies {
    implementation("io.vertx:vertx-core:4.4.5")
    implementation("io.github.mkpaz:atlantafx-base:2.0.1")
    implementation("com.github.oshi:oshi-core-java11:6.4.6")
    implementation("com.google.code.gson:gson:2.10.1")
    implementation("com.dlsc.preferencesfx:preferencesfx-core:11.16.0")
    implementation("commons-io:commons-io:2.14.0")
    implementation("org.apache.commons:commons-configuration2:2.9.0")
    implementation("com.dustinredmond.fxtrayicon:FXTrayIcon:4.0.1")
    implementation("dev.dirs:directories:26")
    implementation("io.ebean:ebean:13.15.0-jakarta")
    implementation("io.ebean:ebean-querybean:13.15.0-jakarta")
    implementation("io.ebean:ebean-sqlite:13.22.1-jakarta")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
    implementation("org.xerial:sqlite-jdbc:3.43.0.0")
    testImplementation("io.ebean:ebean-test:13.15.0-jakarta")
    testImplementation("org.testfx:testfx-junit5:4.0.16-alpha")
    testImplementation("io.vertx:vertx-junit5:4.4.5")
    annotationProcessor("io.ebean:querybean-generator:13.15.0-jakarta")
}

application {
    mainModule.set("org.xxx.xxx")
    mainClass.set("org.xxx.xxx.XXXApplication")
}

configurations{
    all{
        exclude("javax.annotation",module = "javax.annotation-api")
        exclude("javax.annotation",module = "javax.annotation-api")
    }
}

java {
    modularity.inferModulePath.set(false)
}

jlink {
    imageZip.set(project.file("${layout.buildDirectory}/distributions/app-${javafx.platform.classifier}.zip"))
    launcher { name = "VisualModeling" }
    group = "distribution"
    mergedModule {
        requires("org.slf4j")
        requires("jakarta.inject")
        requires("jakarta.annotation")
    }
}

jlink{
    forceMerge("org.slf4j")
//    forceMerge{
//        additive = true
//        requires("org.slf4j")
//    }
}

And there is root projectbuild.gradle.kts

subprojects {
    apply {
        plugin("org.openjfx.javafxplugin")
        plugin("io.freefair.sass-java")
    }

    javafx {
        version = "21"
        modules = listOf(
            "javafx.controls",
            "javafx.fxml",
            "javafx.media",
            "javafx.media",
            "javafx.web",
            "javafx.swing"
        )
        configurations = arrayOf("implementation", "testImplementation")
    }
}
MarceloRuiz commented 11 months ago

I have the same problem. Impossible to use the plugin with Gradle Kotlin DSL. The readme file of the plugin gives the following configuration example:

Kotlin

javafx {
    modules("javafx.controls", "javafx.fxml")
    platform = 'mac' 
}

But it is not possible to configure the platform. It does not take a single quoted string (impossible in kotlin). The type of the property is JavaFXPlatform, but it cannot be set because gradle complains that Val cannot be reassigned It would be great if it could work like this:

platform = JavaFXPlatform.LINUX
MarceloRuiz commented 11 months ago

Also, following the documentation, the configurations are not possible to set because of a Type Mismatch: it requires a configure closure instead of a string:

Kotlin

javafx {
    modules("javafx.controls", "javafx.fxml")
    configurations("implementation", "testImplementation")
}
MarceloRuiz commented 11 months ago

@DaiYuANg

I actually was able to solve my problem, and it might help you with yours, or at least point you in the right direction. Here is how I solved it, thanks to the answer from Vampire in the Gradle Forums. You can check at the questions and answers here:

[https://discuss.gradle.org/t/how-can-i-re-declare-variants-for-a-configuration-using-gradle-kotlin-dsl/46919](Gradle Forum Question 46919)

[https://discuss.gradle.org/t/how-can-i-add-an-attribute-disambiguation-rule-for-all-configurations-in-a-project/46940](Gradle Forum Question 46940)

Here is the relevant code. What was key in my case is that the attributes were added to configurations that are resolvable:

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"))
    }
}