🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Project Structure
The project structure will look like this:
blogger
├── build.gradle
├── settings.gradle
├── blogger-core
│ ├── build.gradle
│ └── src/main/java/com/example/core
├── blogger-common
│ ├── build.gradle
│ └── src/main/java/com/example/common
├── blogger-web
│ ├── build.gradle
│ └── src/main/java/com/example/web
└── gradle
└── wrapper
├── gradle-wrapper.jar
└── gradle-wrapper.properties
Step 1: Create the Parent Project
- Create the parent project directory:
mkdir blogger
cd blogger
- Create
settings.gradlefile:
rootProject.name = 'blogger'
include 'blogger-core', 'blogger-common', 'blogger-web'
- Create
build.gradlefile for the parent project:
plugins {
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.example'
version = '1.0.0'
sourceCompatibility = '17'
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-core:5.1.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
test {
useJUnitPlatform()
}
}
Step 2: Create a Core Module
- Create the core module directory:
mkdir -p blogger-core/src/main/java/com/example/core
- Create
build.gradlefile for the core module:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
- Add a simple service class:
blogger-core/src/main/java/com/example/core/GreetingService.java
package com.example.core;
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
}
Step 3: Create a Common Module
- Create the common module directory:
mkdir -p blogger-common/src/main/java/com/example/common
- Create
build.gradlefile for the common module:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
- Add a utility class:
blogger-common/src/main/java/com/example/common/StringUtils.java
package com.example.common;
public class StringUtils {
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
}
Step 4: Create a Web Module
- Create the web module directory:
mkdir -p blogger-web/src/main/java/com/example/web
- Create
build.gradlefile for the web module:
dependencies {
implementation project(':blogger-core')
implementation project(':blogger-common')
implementation 'org.springframework.boot:spring-boot-starter-web'
}
- Add a controller class:
blogger-web/src/main/java/com/example/web/GreetingController.java
package com.example.web;
import com.example.core.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@Autowired
private GreetingService greetingService;
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return greetingService.greet(name);
}
}
Step 5: Running the Application
- Add the main application class:
blogger-web/src/main/java/com/example/web/WebApplication.java
package com.example.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.example.core", "com.example.common", "com.example.web"})
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
- Run the application:
./gradlew :blogger-web:bootRun
Conclusion
In this tutorial, we've set up a multi-module Spring Boot project using Gradle. Each module has a specific responsibility, making the codebase more modular and easier to maintain. You can extend this setup with more modules and functionalities as needed. For more information and best practices, refer to the Spring Boot documentation and Gradle documentation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment