Search code examples
gradleeclipse-plugintransitive-dependencymaven-publish

Gradle excludes self when excluding all transitive dependencies


I've com across a particular problem which I have been unable to solve and would be grateful for any help. Originally I included some jar dependencies as artifact only dependencies in my Java project. This looked like the following:

    compile "com.example:projectA:1.0.0@jar"

So far so good. Let's call this project 'A'. I have included project A in another Java project B, again with gradle. I've noticed that A published with maven-publish did not exclude all its transitive dependencies, in its pom file, when using it in B.

So I started using the transitive flag:

dependency("com.example:projectA:1.0.0") {
    transitive = false
}

This makes sure that in project B, I excluded all transitive deps of A when using the eclipse plugin and gradle itself.

However the problem with the missing exclusion in the published pom.xml remained. Then I found this issue which seems to be solved at the time of writing and for my gradle version: GRADLE-2945

So I tried the following:

dependency("com.example:projectA:1.0.0") {
    exclude group: '*'
}

The pom file now correctly has the desired exclude rules for transitive dependencies in accordance with the maven doc:

<groupId>com.example</groupId>
<artifactId>projectA</artifactId>
<version>1.0.0</version>
<exclusions>
  <exclusion>
    <groupId>*</groupId>
    <artifactId>*</artifactId>
  </exclusion>
</exclusions>

However this lead to a different set of problems; Neither eclipse nor gradle itself when using the compileJava task could compile any code in Project B when including A in that fashion. Along with its transitive dependencies A itself had disappeared.

The strange thing though is that according to the dependencies task it is part of the compile classpath.

I am using a JRE7 and gradle 2.3 with eclipse 4.41 and we use nexus as internal maven repository.

I would expect that I am not required to use the transitive flag and manipulate the generated pom file manually by adding the desired exclusions.


Sample project to reproduce the problem using dbcp as project A:

Main.java:

import org.apache.commons.dbcp.BasicDataSource;
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
        BasicDataSource ds = new BasicDataSource();
    }
}

build.gradle:

version = '0.0.1'
group = 'com.example'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

repositories {
    mavenCentral()
}

dependencies {
    compile ('commons-dbcp:commons-dbcp:1.4')
    {
        transitive=false
    }
//  {
//      exclude group: '*'
//  }
} 

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

Solution

  • I think this may have been a bug in Gradle. I tested your example files with both Gradle 2.3 and 2.5 from the command line. If I use transitive = false, both versions compile the project fine. Switching to the exclude group: '*' syntax causes the 2.3 build to break. Gradle 2.5 continues to work fine.

    In short, an Gradle upgrade should fix your problem.