Search code examples
gradlemulti-project

why "jar.enabled = false" affects dependencies in Gradle?


The story: I have recently discovered that setting jar.enabled = false in project b-features of a multi-projects Gradle project will stop project a-features of the same project from being able to refer to B via

dependencies {
    compile project(':b-features')
}

The question: Why is jar.enabled = false stopping project A from successfully referring to project B?

Additional info:

build.gradle of the root project:

group 'gradle.studies'
apply plugin: 'java'
...

settings.gradle of the root project:

rootProject.name = 'multi-project-reference'
include 'a-features'
include 'b-features'

build.gradle of the project a-features:

group 'gradle.studies'
apply plugin: 'java'
...
dependencies {
    compile project(':b-features')
}
...

build.gradle of the project b-features

group 'gradle.studies'   
apply plugin: 'java'    
jar.enabled = false    
...

Class A in a-features project:

package outerproject;

import innerproject.B;

public class A {
    public static void main(String[] args) {
        B b = new B();
        System.out.println(b.getMsg());
    }
}

Class B in b-features project:

package innerproject;
public class B {

    public B() {
        this.msg = "Hello World";
    }

    private String msg;

    public String getMsg() {
        return msg;
    }
    //...
}

The error:

14:27:50: Executing external task 'build'...
:compileJava NO-SOURCE :processResources NO-SOURCE :classes UP-TO-DATE
:jar UP-TO-DATE :assemble UP-TO-DATE :compileTestJava NO-SOURCE
:processTestResources NO-SOURCE :testClasses UP-TO-DATE :test
NO-SOURCE :check UP-TO-DATE :build UP-TO-DATE :b-features:compileJava
UP-TO-DATE :b-features:processResources NO-SOURCE :b-features:classes
UP-TO-DATE :b-features:jar SKIPPED
/home/nikita/IdeaProjects/multi-project-reference/a-features/src/main/java/outerproject/A.java:3:
error: package innerproject does not exist import innerproject.B;
                   ^ /home/nikita/IdeaProjects/multi-project-reference/a-features/src/main/java/outerproject/A.java:7:
error: cannot find symbol
        B b = new B();
        ^   symbol:   class B   location: class A /home/nikita/IdeaProjects/multi-project-reference/a-features/src/main/java/outerproject/A.java:7:
error: cannot find symbol
        B b = new B();
                  ^   symbol:   class B   location: class A 3 errors :a-features:compileJava FAILED

FAILURE: Build failed with an exception.

Solution

  • What you actually depend on, is the artifact (JAR file) that is produced by the project. As you disabled the generation of the artifact (jar task), the needed classes of course are not found any longer.