IntelliJ gradle integration issue with independent gradle subprojects - gradle

I have two independent gradle projects that I also want to build from one main gradle project:
main-project
build.gradle
settings.gradle
--subproject-one
build.gradle
settings.gradle
--module-a
build.gradle
--subproject-two
build.gradle
settings.gradle
--module-b
build.gradle
My top settings.gradle includes all modules from the sub projects:
file('subproject-one').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
file('subproject-two').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
Now i have a dependency from module-b in subproject-two to module-a in subproject-one. I use a compile switch (useProjectDependencies) to include them as project dependencies when I build from the main project and as normal dependencies when i build from the subprojects:
if(useProjectDependencies){
compile project(':module-b')
} else {
//Client dependencies
compile "my.group.id:module-b
}
Via gradle I'm now able to build the main-project including the subproject and also each subproject on it's own.
When I open the main-project now via IntelliJ I see all the modules are recognized correctly and also the cross references to classes between the modules work correctly (refactoring interface between to components are working great).
When I now try to use the gradle integration from Intellij and click on assemble for the module-b then it incorrectly uses the build.gradle form and settings.gradle from subproject-two and doesn't use the project dependencies. It looks like IntelliJ or the gradle daemon searches for the first settings.gradle in a bottom up way from the module-b instead of using the one in the main-project.
When I build via terminal from the main-project folder all works correctly:
gradlew :module-b:assemble //correct *settings.gradle* and *build.gradle* is used
When I delete the settings.gradle and build.gradle from subproject-two then the build.gradle from the main-project is correctly used.
Is there a way to specify the settings.gradle and build.gradle that is used as part of the IntelliJ gradle integration?

Related

Gradle tasks shown in intellij but can not be run

I have a multi module project with the following structure:
root_project
module1
module2
module3
I try to apply the java plug-in to all projects using the following code:
allprojects {
apply plugin: 'java'
group = 'com.mysoftware'
sourceCompatibility = 1.8
version = '1.3'
repositories {
mavenCentral()
}
}
Additionally I add the javafx plugin to module3. The java and javafx tasks are now shown in the intellij gradle view, but when trying to execute them, I get this error:
Task 'jfxJar' not found in root project 'module3'.
Furthermore, running the tasks task show me that neither the java tasks nor the javafx tasks are available, despite being shown in the gradle view in intellij.
I tried rebuilding and refreshing the whole project without success. I use the Use default gradle wrapper configuration.
The error message you got Task 'jfxJar' not found in root project 'module3' indicates that Gradle considers the subproject module3 as a Root project: this can happen if you created a settings.gradle file in the sub-project directory, which is not a valid setup (only one settings.gradle file can exist in a multiproject build, located in the root directory)

subprojects dependencies failure with Gradle

I'm struggling with Gradle and the build configuration of the following project structure (pretty simple...):
/projA
/projB
/projC
projC using classes from projB.
In projA/settings.gradle:
include 'projB'
include 'projC'
In projC/build.gradle:
dependencies{
compile project(':projB')
}
In IntelliJ I have no problem of dependency resolution, but when I'm running a ./gradlew build in projA, I'm facing a compilation error:
ClassC: Unresolved reference: ClassB
(where ClassC is the class of projC which is failing on the use of ClassB which is a class from projB, obviously...)
Notice that the code is in Kotlin language, that I do not have any problem to run the app in IntelliJ (spring boot run), but any build with Gradle give me an error (both in Intellij and command line).
What am I missing?
Regards,
Adrien
It's a common Gradle idiom to have an additional top level directory for your rootProject. That's a special project that's the parent to all other projects in your build, in a multi-project build.
That's where your settings.gradle file goes:
include ':projA:projB'
include ':projA:projC'
Then, I'd recommend having projA as a subdirectory of your rootProject, so your hierarchy would look as follows:
/myProject
settings.gradle
/projA
build.gradle
/projB
build.gradle
/projC
build.gradle
Also, in projC/build.gradle, you'll want instead:
dependencies {
compile project(':projA:projB')
}
That should do it.

How add gradle to subproject root project as dependency?

I have the following project structure settings.gradle:
include 'B'
include 'C'
rootProject.name = 'A'
How add gradle to subproject root project as dependency?
As far as the project method is concerned, the root project has no name. So this is the syntax in project B's build.gradle:
dependencies {
compile project(':')
}
However, it is rarely a good idea to do this. It is too easy to end up with circular dependencies. Most multi-module projects have a separate "main" projects (called something like "main", "core" or "base"), and other modules can easily depend on that using compile project(':core') or whatever.
I assume the question being asked is: "How to add a Gradle root project as dependency to a subproject?"
The following worked for me, when I added this in my subproject's build.gradle.
dependencies {
//Add the root/parent project as a dependency
compile project(":")
}
//Without this, my subproject's tests would not compile or run
sourceSets {
test{
compileClasspath += project(":").sourceSets.main.output.classesDirs
runtimeClasspath += project(":").sourceSets.main.output.classesDirs
}
}
Note: I am using gradle version 5.6.4.

How to add other projects in current project in Gradle?

I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB?
And please let me know in case change in the ProjA build.gradle file?
Okay, so you have two projects, whereas ProjA depends on ProjB.
If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.
Example:
settings.gradle
rootProject.name = 'ProjA'
build.gradle
group = 'mygroup'
version = '1.0'
By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories).
Example for a minimal Maven publication:
apply plugin: 'maven-publish'
publishing {
publications {
core(MavenPublication) {
from components.java
}
}
}
The maven-publish plugin will add the task publishToMavenLocal (among others) to your project which installs the jar somewhere to ~/.gradle/caches/.
In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:
dependencies {
compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}
If your projects are co-located, you could have a multi build project.
Say you have a root folder with these contents:
some root directory
project-a
project-a.gradle
src
project-b
project-b.gradle
src
build.gradle
settings.gradle
settings.gradle contains:
include 'project-a', 'project-b'
project-b.gradle then depends on project A:
dependencies {
compile project(':project-a')
}
After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.

Using gradle for a flat, multi-module project where settings.gradle is not the highest-level dependency

I have a multi-module project with a directory structure like:
proj
|-modA
|-modB
|-modMain
\-modSysTest
The dependencies are:
modB -> modA
modMain -> modB
modMain -> modA
modSysTest -> modMain
We currently use Ant as our build system. Every module has a build.xml. There is a master.xml that runs the multi-module build, which is in modMain.
I am investigating switching our project to use gradle. I can get a multi-module build working if I put a build.gradle and settings.gradle in a new sibling directory and set up the dependent modules using includeFlat.
I tried moving the gradle files into modMain, which is the home of both the main component of our system as well as the top-level build script (master.xml) that builds the whole system, but I cannot get the build working this way. My main stumbling block at the moment is trying to configure modSysTest to depend on modMain from within modMain.
Here is the settings.gradle from modMain:
includeFlat 'modA', 'modB', 'modSysTest'
Here is the build.gradle file from modMain:
allprojects {
apply plugin: 'java'
repositories {
mavenCentral()
};
dependencies {
testCompile 'junit:junit:4.11'
}
}
project(':modB') {
dependencies {
compile project(':modA')
}
}
project(':modSysTest') {
dependencies {
compile project(':modMain')
}
}
dependencies {
compile project(':modA'), project(':modB')
}
When using this configuration, I get the error:
A problem occurred evaluating root project 'modMain'.
> Project with path ':modMain' could not be found in project ':modSysTest'.
I am not sure how to specify modMain as a dependency for modSysTest.
The project path of the root project is :, not :modMain. It's not very customary (but possible) to have code in the root project. I don't see how putting build.gradle and settings.gradle into a sibling directory of modMain would give a different result (unless you also changed the contents of settings.gradle). Note that with such a setup, Gradle won't find settings.gradle unless you start the build from the modMain directory or pass the location of the settings file with -c.

Resources