Gradle multi-project setup in Eclipse - java

I will have the following setup:
MyRootProj
|
---- MyJavaProj1
|
---- MyJavaProj2
|
---- MyWebProj
I want to do the development on Eclipse and have installed BuildShip plugin.
From this discussion it seems that its not possible to create a multi-project setup in Eclipse for Gradle.
I tried the following steps:
I created a directory MyRootProj and inside that ran:
gradle init
This created the default files.
Then I modified the build.xml of MyRootProj to:
plugins {
id 'eclipse'
}
allprojects {
repositories {
mavenCentral()
}
apply plugin : 'eclipse-wtp'
apply plugin : 'java'
}
Till this point everything is fine.
Now following the steps in above link, I create MyJavaProj1 directory under MyRootProj and add a blank build.gradle inside it.
I then modify the settings.gradle file of MyRootProj to:
rootProject.name = 'MyRootProj'
include 'MyJavaProj1'
And then did a gradle refresh on the root project as recommended.
This creates the MyJavaProj1 with all the files. However, this does not have a Gradle recommended package structure for java projects already.
When I right click on eclipse project explorer and do a New -> Gradle Project, the project which gets created comes with src-main-java, src-main-test, etc. structure out of the box.
Why does that not happen with the way I am creating MyJavaProj1 above?
Long story short, I have created the root project. Now I want to create sub projects. However, I want to do it in Eclipse and I want the sub-projects to have autocreated Gradle recommended structure for Java Projects.

Related

Using Dependencies

When I try using using the dependency implementation 'com.github.dhaval2404:imagepicker:2.1' or implementation 'com.github.dhaval2404:imagepicker-support:1.7.1' I will get a warning message that says "Failed to resolve: com.github.dhaval2404:imagepicker-support:1.7.1
Show in Project Structure dialog</ a >
Affected Modules: app</ a >"
How do I get rid of this warning?
This is the link to the github I am working on https://github.com/Dhaval2404/ImagePicker
I tried adding the build.gradle from the folder but I don't know where to add it, when I copy and paste the entire thing I get an error saying
"Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app\build.gradle'"
go to the Gradle Scripts folder and click settings.gradle. Copy and paste maven { url "https://jitpack.io" } in the repositories{}. Now you can go back to build.gradle in your Gradle Scripts folder to add in your implementations.

How to add Gradle to existing JavaFX Project (IntelliJ)

From this post (Best way to add Gradle support to IntelliJ Project) I can see that what I need to do is "Add build.gradle in your project's root directory."
I was hoping someone could please explain how I do that in intelliJ? (been searching and reading - still baffled).
I understand a root directory the folder which is the parent for all the project sources, but in standard JavaFX project in intelliJ what is it/how do I find it/assign it, and then how do I add build.gradle?
Note: these steps assume that you are using the latest JDK version (17).
In the project browser, right click on the project name and create a file named build.gradle:
Write a build script. Here is a template for JavaFX applications:
plugins {
id "application" // Use Application plugin
id "org.openjfx.javafxplugin" version "0.0.9" // Use JavaFX plugin
}
mainClassName = "my.package.Application" // Set this to your main class
repositories {
mavenCentral()
}
javafx {
version = "16" // JavaFX Version
modules = [ "javafx.controls" ] // JavaFX modules. Add them to this array.
}
Once you have finished, import the project:
This may take a while depending on your internet speed.
You may have to adjust runtime configurations to use Gradle, but that shouldn't be necessary.
You may encounter the following error:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61
If that is the case, install Gradle CLI (if you haven't already), and open a command prompt/terminal in the project folder.
Run the following command (works on Windows/Mac/Linux):
gradle wrapper --gradle-version=7.3 --distribution-type=bin
Once you have done that, reload the Gradle project.
I could not resist another GIF:
You may also need to rearrange the source folders:
I apologise for the cat photo, but it was just a placeholder image. I definitely didn't specifically choose it.

Understanding gradle dependencies

I have following structure:
ProjectA -> depends on ProjectB
ProjectB -> depends on ProjectC
compiling projectB everything works:
ProjectB/build.gradle:
dependencies {
compile project(':ProjectC')
}
ProjectB/settings.gradle:
include ':ProjectC'
project(':ProjectC').projectDir = new File(settingsDir, '../ProjectC')
However, compiling ProjectA it says it can not find ProjectC
ProjectA/build.gradle:
dependencies {
compile project(':ProjectB')
}
ProjectA/settings.gradle:
include ':ProjectB'
project(':ProjectB').projectDir = new File(settingsDir, '../ProjectB')
This will show following error:
Where:
Build file ProjectB\build.gradle
What went wrong:
A problem occurred evaluating project ':ProjectB'.
Project with path ':ProjectC' could not be found in project ':ProjectB'.
I Could only make it work including ProjectC in ProjectA. But this is not what I want.
I also tried to exclude on ProjectA but didnt work
ProjectA/build.gradle:
dependencies {
compile (project (':ProjectB')) {
exclude module: ':ProjectC'
}
}
But shows same error.
How can I fix this?
Multi-Project builds are not cascadable. You can have either one or no settings.gradle file in a multi-project build, but not multiple.
Besides that it is not working as expected, it can even get more confusing if you call Gradle from different directories. Gradle looks up (and to the side into directories called master) to find the nearest settings.gradle if none is specified. Then it evaluates the settings.gradle and checks whether the project in which your working directory is, is part of the multi-project build defined in that settings.gradle file. If so, it is executed in the context of the multi-project build, if not, it is executed as standalone build.
This means, if you call Gradle from inside ProjectA, you have a completely different build that probably als is configured differently, than if you call Gradle from inside ProjectB.
If you want to compose multiple multi-project builds into one build, you should instead use GradleBuild tasks or composite builds. In both cases the sub-build is completely independent and can itself be a multi-project build. Which one to use depends on the exact use-case.
With gradle you should be using only a single settings.gradle file. See Multiple settings gradle files for multiple projects building
Also just follow gradle multiproject documentation.

IntelliJ execute gradle test from root project

I've a gradle multiproject importend in my IntelliJ, and I want execute some test classes.
The structure is like:
root
|-module-a
|-module-b
module-a depends on module-b, so in the build.gradle
dependencies {
compile project('module-b');
}
When I use gradle from the shell everything it's ok, I have to go in the root project dir and write:
./gradlew :module-a:test
And everything it's been tested.
When I click "Run 'Tests' in 'module-a'" from IntelliJ I have this error:
A problem occurred evaluating root project 'module-a'.
> Project with path ':module-b' could not be found in root project ':module-a'.
So it seems, that IntelliJ is executing the gradle command from module-a and not from the root (this should be the correct behavior from what gradle wants).
How can execute this test inside IntelliJ? What I have to configure?
For a multi-project structure that looks like this
root
|-module-a
|-module-b
There is only one settings.gradle in the root folder, with the content:
include 'module-a', 'module-b'
The subproject folders do not contain a settings.gradle file. Then you refer to sibling projects as:
project(':module-b')
so your dependency would be declared as:
dependencies {
compile project(':module-b');
}
Please see here for more information about multi-project structure.

Gradle sub-project classes dependency

I've got several sub-projects in my gradle project:
Project
Common
Consumer1
Consumer2
.....
ConsumerN
My first - and main goal – is to include classes from Common project into resulting jar of every ConsumerN projects. So I can develop and test shared part (DB logic, some utils) independently in Common project and next other projects will get this classes (already tested) and include them into their jars.
Second goal is to make IntelliJ Idea to understand such dependency and work with it correctly.
Would you please suggest the "most conceptual and right way" to do this in gradle.
Assume You have the following project structure:
root
build.gradle
common
m1
m2
m3
settings.gradle
First of all You need to set a multimodule project - it's done in settings.gradle (this is a normal gradle script) and its content is as follows:
include 'm1', 'm2', 'm3', 'common'
Per project settings are done in dedicated build.gradle files, but the settings You asked can be done globally - in root build.gradle. Here's its content:
subprojects {
apply plugin: 'java'
}
subprojects.findAll { it.name != 'common' }.each {
configure(it) {
dependencies {
project(':common')
}
}
}
The question is what artifacts are produced from mN modules. If these are jar files You need to use fatjar or shadow plugin. If there are web applications war plugin is what You need.
Some further reading.
IntelliJ IDEA should handle these dependencies while importing the project using gradle wrapper.

Categories