Exclude dependency from pom using Maven Publish Plugin?

1.8k Views Asked by At

I use the following plugins:

id 'maven-publish'
id "com.github.johnrengelman.shadow" version "7.0.0"

my dependencies:

dependencies {
    shadow gradleApi()
    shadow localGroovy()
    implementation 'com.example:lib:0.1.0'

my publishing block:

publishing {
    publications {
        pluginJar(MavenPublication) {  publication ->
            from project.shadow.component(publication)
            artifact sourceJar
        }
    }
}

When I run publishToMavenLocal task I can see that in result pom.xml contains a dependency that I do not want there.

Let's say it's:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>lib</artifactId>
  <version>0.1.0</version>
  <scope>runtime</scope>
</dependency>

How I can configure publications block in order to get rid of this dependency from the pom.xml (and module) file?

2

There are 2 best solutions below

0
On BEST ANSWER

I discovered that since my library had one runtime dependency I could use the following:

components.java.withVariantsFromConfiguration(configurations.runtimeElements) {
    skip()
}
0
On

Dependencies in pom.xml populates from apiElements (compile scope) and runtimeElements (runtime scope) configurations. You can remove dependencies you want to exclude from pom.xml from this configurations.

Example (I will use Kotlin DSL because I'm not familiar with Groovy):

setOf("apiElements", "runtimeElements")
    .flatMap { configName -> configurations[configName].hierarchy }
    .forEach { configuration ->
        configuration.dependencies.removeIf { dependency ->
            dependency.name == "lib"
        }
    }
from(components["java"])

Convert this code to Groovy and paste it in your publication closure.