0

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?

pixel
  • 24,905
  • 36
  • 149
  • 251
  • How does that com.example.lib dependency look in your `build.gradle`? – Michael Jul 21 '21 at 13:18
  • @Michael updated my answer, my goal is to bundle this dependency into jar using shadow plugin (already done) and get rid of it from `pom.xml` / `module` – pixel Jul 21 '21 at 14:25

2 Answers2

2

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.

1

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

components.java.withVariantsFromConfiguration(configurations.runtimeElements) {
    skip()
}
pixel
  • 24,905
  • 36
  • 149
  • 251