Publishing java-platform as a BOM scope question


(Nicholas Perkins) #1

So I would like to use the the java-platform and maven-publish plugins to publish a BOM.
However, the docs are a little vague about if there are any ways to customize the behavior or not.
Here’s an example build.gradle to illustrate my point:

plugins {
    id 'java-platform'
    id 'java'
    id 'maven-publish'
}

group 'com.foo'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

javaPlatform {
    allowDependencies()
}

dependencies {
    api platform(group: 'org.junit', name: 'junit-bom', version: '5.4.0')

    constraints{
        api group: 'commons-io', name: 'commons-io', version: '2.6'
    }
}

publishing {
    publications {
        myPlatform(MavenPublication) {
            from components.javaPlatform
        }
    }
}

which produces this BOM:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.foo</groupId>
  <artifactId>publish-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.4.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

My issue is that the scope element for my commons-io dependency was unexpected.
From my understanding you generally don’t want to declare scope elements in a BOM for most dependencies because that can cause maven to override scope behavior in unexpected ways (like if one of the declarations in here turns out to be a transitive test dependency then it might be coerced to be compile).

Is there a way to have the published BOM omit the scope for the dependency?