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:
implementation platform(project(":platform"))
with implementation platform("org.wildfly.bom:wildfly-javaee8:15.0.1.Final")
in consumer/build.gradle
.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?
The Java Gradle Plugin development plugin can be used to assist in the development of Gradle plugins. It automatically applies the Java Library plugin, adds the gradleApi() dependency to the api configuration and performs validation of plugin metadata during jar task execution.
There are two general types of plugins in Gradle, binary plugins and script plugins.
A platform is a special kind of software component which doesn't contain any sources: it is only used to reference other libraries, so that they play well together during dependency resolution. Platforms can be published as Gradle Module Metadata and Maven BOMs.
Gradle is the build system. You can use it with a lot of plugins. One of these is the Android Gradle plugin. It is used to provide processes and configurable settings that are specific to building and testing Android applications.
To use the Java Platform plugin, include the following in your build script: Example 1. Using the Java Platform plugin A major difference between a Maven BOM and a Java platform is that in Gradle dependencies and constraints are declared and scoped to a configuration and the ones extending it.
The IntelliJ IDEA Ultimate and Community editions bundle the necessary plugins to support Gradle-based development. These IntelliJ IDEA plugins are Gradle and Plugin DevKit, which are enabled by default. To verify these plugins are installed and enabled, see the help section about Managing Plugins.
This SDK will be the default JRE used to run Gradle, and the JDK version used to compile the plugin Java sources. When targeting 2020.3 and later only, using Java 11 is now required, please see blog post
A SoftwareComponent for publishing the production JAR created by the jar task. This component includes the runtime dependency information for the JAR. See also the java extension. Gradle comes with a sophisticated incremental Java compiler that is active by default. Incremental builds are much faster.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With