pditommaso
pditommaso

Reputation: 3381

How to exclude dependencies in the POM file generated by the Gradle

I'm using the "maven" plugin to upload the artifacts created by Gradle build to Maven central repository. I'm using a task similar to the following one:

uploadArchives {
  repositories {
    mavenDeployer {
      beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

      pom.project {
        name 'Example Application'
        packaging 'jar'
        url 'http://www.example.com/example-application'

        scm {
          connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'

          url 'http://foo.googlecode.com/svn/trunk/'
        }

        licenses {
          license {
            name 'The Apache License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
          }
        }

      }
    }
  }
}

However the POM file created by this task does not report correctly the dependencies that have been excluded in my Gradle build file. For example:

dependencies {
    compile('org.eclipse.jgit:org.eclipse.jgit.java7:3.5.2.201411120430-r') { exclude module: 'commons-logging' }
    compile('com.upplication:s3fs:0.2.8') { exclude module: 'commons-logging' }
}

How to have excluded dependencies managed correctly in the resulting POM file?

Upvotes: 9

Views: 10061

Answers (3)

acker9
acker9

Reputation: 503

Using 'exclude' on a Gradle dependency is normally the correct answer, but I still needed to remove some of my "runtimeOnly" dependencies from the POM that led me to this StackOverflow page. My testing using Gradle 4.7 seems to show that using "compileOnly" leaves the dependency out of the pom entirely, but "runtimeOnly" adds a "runtime" dependency in the pom, which in my case, is not what I wanted. I couldn't figure out a "standard" Gradle way of leaving runtime dependencies out of the POM.

The pom.whenConfigured method shown in another answer works for legacy "maven" plugin publishing, but doesn't work for the newer "maven-publish" plugin. My experimentation led to this for "maven-publish":

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            pom.withXml {
                asNode().dependencies.dependency.each { dep ->
                  if(dep.artifactId.last().value().last() in ["log4j", "slf4j-log4j12"]) {
                      assert dep.parent().remove(dep)
                  }
                }
            }
        }
    }
}

Upvotes: 5

pditommaso
pditommaso

Reputation: 3381

The problem was that in the exclude definition was not specified the group but only the module.

Adding the both of them the exclusions are added correctly in the POM file. For example:

compile('org.eclipse.jgit:org.eclipse.jgit.java7:3.5.2.201411120430-r') { 
    exclude group: 'commons-logging', module: 'commons-logging' 
}
compile('com.upplication:s3fs:0.2.8') { 
    exclude group: 'commons-logging', module: 'commons-logging' 
}

Upvotes: 3

Amnon Shochot
Amnon Shochot

Reputation: 9386

You can simply override the dependencies of the pom by filtering out the unwanted dependencies, e.g. to exclude junit you can add the following lines to the mavenDeployer configuration:

pom.whenConfigured {
    p -> p.dependencies = p.dependencies.findAll { 
        dep -> dep.artifactId != "junit" 
    }
}

Upvotes: 11

Related Questions