My problem is:
I can not import classes inside java-files within my-single-module-project, although I have declared the dependecy to some-subproject-B and doing an includeBuild
inside settings.gradle. In Gradle terms this “includeBuild” thing is called “Composite Build” as far as I know.
I have done some research on this and found this post, which helped me letting the gradle files compile and its commands execute, but still I can’t access the Java files within the declared dependency.
I have the following folder structure:
some-folder/my-multi-module-project
some-folder/my-multi-module-project/some-subproject-A
some-folder/my-multi-module-project/some-subproject-B
some-folder/my-multi-module-project/some-subproject-C
some-folder/my-single-module-project
From the gradle perspective I have these configurations inside the different build.gradle/settings.gradle files:
//some-folder/my-multi-module-project/settings.gradle:
rootProject.name = 'my-multi-module-project'
include 'some-subproject-A'
include 'some-subproject-B'
include 'some-subproject-C'
//some-folder/my-single-module-project/settings.gradle
rootProject.name = "my-single-module-project"
includeBuild ".."
//some-folder/my-single-module-project/build.gradle
...
dependencies {
...
compile ":my-multi-module-project:some-subproject-B"
testCompile ":my-multi-module-project:some-subproject-B"
...
}
...
I can successfully run this gradle command, which outputs that is has successfully found the wanted subproject:
some-folder/gradlew -b my-single-module/build.gradle build--info
...
Registering project ':my-multi-module-project:some-subproject-A' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-A'.
Registering project ':my-multi-module-project:some-subproject-B' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-B'.
Registering project ':my-multi-module-project:some-subproject-C' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-C'.
...
BUILD SUCCESSFUL in 8s
3 actionable tasks: 3 up-to-date
But after adding some java-code to some java file in my-single-module-project, which uses a class from the some-subproject-B, the command above fails to execute, saying that it does not find the class:
some-folder/gradlew -b my-single-module/build.gradle build--info
...
Registering project ':my-multi-module-project:some-subproject-A' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-A'.
Registering project ':my-multi-module-project:some-subproject-B' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-B'.
Registering project ':my-multi-module-project:some-subproject-C' in composite build. Will substitute for module 'my-multi-module-project:some-subproject-C'.
...
C:\some-folder\my-multi-module-project\my-single-module-project\src\test\java\com\some\package\structure\SomeTest.java:10: error: package some.subproject.B.package does not exist
System.out.println(new some.subproject.B.package.SomeSubprojectBClass());
...
BUILD FAILED in 9s
4 actionable tasks: 1 executed, 3 up-to-date
My expectation here was: I can use any Java class from the subproject declared in the dependecy closure, but that is obviously not the case. Why?
Side-Note:
I know that from the folder structure perspective it seems a bit awkward having a single-module folder inside of a multi-module-project without being part of the multi-module-project from the gradle perspective, but this is how this historically grown application currently looks like.
Finally I want to rephrase the initial sentence of this post as a question, which is hopefully clear enough to be anwsered:
Does anyone know how I can declare a dependency from within a gradle-single-module-project to a gradle-subproject nested inside a gradle-multi-module-project AND use classes from that declared dependency?
UPDATE:
I was able to go a different route than “Composite Build” by declaring the dependency to the subproject as follows (of course removing the includedBuild inside the my-single-module-project/settings.gradle):
//some-folder/my-single-module-project/build.gradle
...
dependencies {
...
compile files("../some-subproject-B/src/main/java")
testCompile files("../some-subproject-B/src/main/java")
...
}
...
Now the Class of the subproject is accessible in the my-single-module-project.
Alas, in my opinion the “Composite Build” approach constitutes a more consise way of declaring the dependency. What is wrong with that and why cant I just access the classes?