Gradle java-platform plugin and platform definition
Asked Answered
H

1

7

I'm trying to use the java-platform plugin to share dependency constraints in a multi-module project. What I would like to do is set up a module named platform which I can import using platform(project(":platform")) in other modules. Additionally I would like to import a BOM into my platform project using platform("group-id:of-the-bom:and-version").

An example is the following structure:

// settings.gradle
include("platform")
include("consumer")
// platform/build.gradle
apply plugin: 'java-platform'

repositories {
    mavenCentral()
}

dependencies {
    constraints {
        api platform("org.wildfly.bom:wildfly-javaee8:15.0.1.Final")
    }
}
// consumer/build.gradle
apply plugin: 'java-library'

repositories {
    mavenCentral()
}

dependencies {
    implementation platform(project(":platform"))
    implementation "org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec"
}

When I run gradle consumer:dependencies I get output containing the following:

compileClasspath - Compile classpath for source set 'main'.
+--- project :platform
\--- org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec FAILED

It seems as though the platform module is not "re-exporting" constraints which were pulled in through the usage of platform().

I have found two "workarounds" which don't really solve my problem but seem to indicate that the issue is the usage of platform() together with the java-platform plugin:

  1. Replace implementation platform(project(":platform")) with implementation platform("org.wildfly.bom:wildfly-javaee8:15.0.1.Final") in consumer/build.gradle.
  2. Put an explicit entry in platform/build.gradle such as api org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec:1.0.1.Final.

Is the java-platform plugin not intended to be used in this way, or is there some configuration option I'm missing?

Hydrophilic answered 12/2, 2019 at 10:18 Comment(0)
C
13

Importing a BOM in Gradle means that you want to depend on the BOM to get its provided constraints applied.

What you add by default to a platform are constraints. But constraints only appear in a graph if and only if there is a matching dependency declaration. Also constraints only inform about the module targeted. They never bring any transitive information.

So what you need to do is declare that your platform project depends on the BOM. It will then export its constraints as expected.

// platform/build.gradle
apply plugin: 'java-platform'

repositories {
    mavenCentral()
}

javaPlatform {
    // Declare that your platform contains dependencies
    allowDependencies()
}

dependencies {
    // This is a dependency on the BOM that will bring its constraints transitively
    api platform("org.wildfly.bom:wildfly-javaee8:15.0.1.Final")
    constraints {
        // Additional constraints not covered by the platform above go here
    }
}
Caparison answered 13/2, 2019 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.