Gradle subproject not included in classpath

jmc34 picture jmc34 · Apr 29, 2014 · Viewed 11.3k times · Source

We have a set up with 2 project, 1 main and 1 subproject, they are Java projects. They are all under the same directory.

Here is how the directory structure looks like :

 ./dev
   ./Project_A
     build.gradle
     settings.gradle
   ./Project_B
     build.gradle

Project_A includes Project_B.

Project_A settings.gradle looks like :

includeFlat 'Project_B'

Project_A build.gradle contains :

compile project(':Project_B')   

The issue Project_A misses the classes from Project_B when compiling from command line (gradlew clean build). It looks like Project_B does not belong to Project-A's classpath.

Here is (a part of the ouput) from gradlew clean build ran in Project_A directory (after that it is all "package project_b.x.y missing" and "cannot find symbol" (from Project_B) :

:clean
:Project_B:clean
:Project_B:compileJava
:Project_B:processResources
:Project_B:classes
:Project_B:jar UP-TO-DATE
:compileJava
...Starts erroring out here...

I would guess it is a classpath issue, but I just cannot figure out how to fix it.

Thanks in advance, JM

PS : edited question as I was able to reproduce the issue with a 2 projects build (from 3 initally)

Answer

kuporific picture kuporific · May 2, 2014

Based on the Java Quckstart: Multi-project Java build:

1) your settings.gradle file needs to be in dev/, not dev/project_A and it should contain something like this:

include 'Project_A', 'Project_B'

2) Then your dev/Project_A/build.gradle file should contain

dependencies {
    compile project(':Project_B')
}

Edit:

I have created a toy example following the project layout you've described in your question. However, I haven't been able to reproduce the problem. Perhaps you'll be able to spot some difference that is causing your particular error:

Directory Tree

├── Project_A
│   ├── build.gradle
│   ├── settings.gradle
│   └── src
│       └── main
│           └── java
│               └── a
│                   └── A.java
└── Project_B
    ├── build.gradle
    └── src
        └── main
            └── java
                └── b
                    └── B.java

Project_A/build.gradle

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'a.A'

dependencies {
    compile project(':Project_B')
}

Project_A/settings.gradle

includeFlat 'Project_B'

A.java

package a;
import b.B;

public class A {
    public static void main(String[] args) {
        System.out.println("From A.main...");
        B.call();
    }
}

Project_B/build.gradle

apply plugin: 'java'

B.java

package b;

public class B {
    public static void call() {
        System.out.println("Calling B");
    }
}

When running Gradle from Project_A, the output is:

$ gradle clean build run
:clean UP-TO-DATE
:Project_B:clean UP-TO-DATE
:Project_B:compileJava
:Project_B:processResources UP-TO-DATE
:Project_B:classes
:Project_B:jar
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:Project_B:assemble
:Project_B:compileTestJava UP-TO-DATE
:Project_B:processTestResources UP-TO-DATE
:Project_B:testClasses UP-TO-DATE
:Project_B:test UP-TO-DATE
:Project_B:check UP-TO-DATE
:Project_B:build
:run
From A.main...
Calling B

BUILD SUCCESSFUL