Gradle subproject not included in classpath
Asked Answered
B

2

11

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)

Bilestone answered 29/4, 2014 at 17:41 Comment(7)
A Gradle build can only have a single settings.gradle. For detailed information on multi-project builds, see the "multi-project builds" chapter in the Gradle User Guide, and the many samples in the full Gradle distribution.Toomey
Hi Peter, but how can we build Project_B then ?Bilestone
I don't understand the question. I recommend to first study the documentation and samples.Toomey
Ok, read the doc... still feels like we are following the norm (bar subproject is not in a subdir)... Downsized to 2 projects... Still failing... Now failing both in Eclipes and command line...Bilestone
Perhaps update your question then.Toomey
It's not clear to me what your directory structure is, and what you mean by "Project_A includes Project_B". Anyway, most likely your settings.gradle is in the wrong directory.Toomey
Hi Peter, spent most of teh day trying things, no luck yet. I have just added the directory structure in case it helps you nailing it down. Thx, JMBilestone
B
1

Ok, in case this is useful to someone, I finally got this working with actually removing (git rm) the .claspath file. It was in the .gitignore, but for some reason, it seems it had been comitted at some point and was playing around.

After doing this and re-importing the projects in Eclipse everything went back to normal.

Thanks to the ones who tried to help.

Bilestone answered 9/5, 2014 at 13:20 Comment(0)
A
10

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
August answered 2/5, 2014 at 13:20 Comment(4)
Hi, AFAIU, includeFlat is meant to do the same (including project at the same directory level). The projects are well included (their tasks are called when calling main project tasks), the issue is that they do not seem to be present in main project build path...Bilestone
@Bilestone You're right about includeFlat, sorry about that. However, I haven't been able to reproduce your problem (see my edited question...).August
many thanks, I'll try putting settings.gradle outside the main project but I do not have much hope. As you proved, it should work either way and it was working for us. For a reason I do not understand it got broken and reverting the code back does not fix. I am very perplexed.Bilestone
@Bilestone I hope you get it working and figure out the cause. Maybe run gradle dependencies on Project_A and see if Project_B is listed. The only other things that comes to mind is maybe the latest version of Gradle will magically solve the problem? Maybe you're putting the dependency in the wrong place (like under buildscript { dependencies { compile project(':Project_B') } } instead of the root dependencies { })? But those are just shots in the dark... Good luck!August
B
1

Ok, in case this is useful to someone, I finally got this working with actually removing (git rm) the .claspath file. It was in the .gitignore, but for some reason, it seems it had been comitted at some point and was playing around.

After doing this and re-importing the projects in Eclipse everything went back to normal.

Thanks to the ones who tried to help.

Bilestone answered 9/5, 2014 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.