Gradle multi-project build - spring

I have the following project structure:
my-project
+my-project-data
+my-project-service
+my-project-example
Where each of these my-project-data, my-project-service, my-project-examples are defined as sub project to my-project in settings.gradle file. Of course, my-project-services has a dependency to my-project-data. And my-project-examples has a dependencies to my-project-data and my-project service.
In the project my-project-examples I want to have some class with main method and make some queries to DB (Assume that main class is in package com.project.main). Moreover I want to execute this in command line:
java -jar my-project.jar
But to do this I have to set Main-Class attribute. Where do I have to do this in root project build.gradle or somewhere else and what have to be the value of attribute?

You can use the application plug-in which provides exactly what you need.
Add the following to your example's project build.gradle:
apply plugin: 'application'
mainClassName = "com.company.package.YourClassWithMain"
Then, simply run the sample by executing 'gradle run' (or using gradle wrapper) under the example project directory.

Related

Jib Main class error with spring boot multi module (gradle)

root-projec
ㄴ sub1-project
build.gradle
ㄴ sub2-project
build.gradle
build.gradle
I'm trying to build all subprojects as jibs in the root project.
The following script is the build.gradle file of the root and sub modules.
...
jib {
from {
image = "eclipse-temurin:17"
}
to {
image = "abc/${project.name}:${project.version}"
tags = ["latest"]
}
}
cd root-project
./gradlew jib
Execution failed for task ':jib'.
> com.google.cloud.tools.jib.plugins.common.MainClassInferenceException: Main class was not found, perhaps you should add a `mainClass` configuration to jib
Of course there are no classes in the root project. Why do you want to include the main class in the root project when building the jib? I'd like to find a way to exclude this.
Additionally, is there any way to build with jib when building with a specific profile as follows?
There is a way to write a script in maven, but gradle has grammatical difficulties.
./gradlew clean build -P build-docker-image

Gradle custom task type defined in buildSrc not accessible in subprojects for multi-project structure

I have a gradle multi-project hierarchy:
root
-buildSrc
 -src/main/groovy/com/CustomTask.groovy
-subproj1
-subproj1-build.gradle-settings.gradle I am able to invoke custom task - com.CustomTask only from root build.gradle file but I get - Unable to resolve class CustomTask at line import com.CustomTask when called from subproj1/build.gradle
import com.CustomTask
task customTask(type: CustomTask) {
setModuleRoot(project.projectDir)
}
Can anyone please suggest me how can I invoke a custom task defined under root level buildSrc from subproject's build.gradle file? (I am on gradle Gradle 7.1.1 version)

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 do I run or create a single Gradle task to run all subprojects and root in a multi-project build?

I have multiple projects configured using build.gradle for each and settings.gradle at the top level.
I want to define or use a single Gradle task that will build all of the subprojects and the root.
How do I run or create a single Gradle task to run all subprojects and root in a multi-project build?
It depends on what all your projects are.
You can call gradle(w) build from the root dir if all your subprojects extend the javaplugin.
Other project types (like the ear plugin) need to be attached to the build task manually. The way I do this is by creating the build task like: task build and in the ear project: build.dependsOn ear

Resources