Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle java-platform plugin and platform definition

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?

like image 751
JamesGuthrie Avatar asked Feb 12 '19 10:02

JamesGuthrie


People also ask

What is Java Gradle plugin?

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.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.

What is platform in Gradle?

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.

What is Gradle and Gradle plugin?

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.

How do I use the Java platform plugin in Gradle?

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.

Does IntelliJ IDEA support Gradle-based development?

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.

What is the default JRE used for Gradle?

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

What is a software component in Gradle?

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.


Video Answer


1 Answers

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
    }
}
like image 182
Louis Jacomet Avatar answered Oct 12 '22 06:10

Louis Jacomet