Hands-On: Building a Custom Spring Boot Starter with Gradle
We’ll create a Spring Boot starter for a “Custom Logging Service”, which can be reused across multiple projects. This service will log incoming requests with a custom header.
Step 1: Set Up the Spring Boot Starter Project
1.1. Create a New Project
Use the Spring Initializr or manually create a Gradle project. Include the following dependencies:
spring-boot-starter
spring-boot-starter-web
Gradle Build File:
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.0'
id 'io.spring.dependency-management' version '1.1.3'
id 'maven-publish'
}
group = 'com.codefarm'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = JavaVersion.VERSION_21
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Step 2: Implement the Logging Service
2.1. Create a Filter for Custom Logging
Add a filter that logs incoming requests.
@WebFilter("/*")
@Component
public class LoggingUtilFilter implements Filter {
private final Logger logger = LoggerFactory.getLogger(LoggingUtilFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
logger.info("Endpoint Called - : {} {}", httpServletRequest.getMethod(), httpServletRequest.getRequestURI());
chain.doFilter(request, response);
}
}
2.2. Add a Configuration Class
This will enable the logging feature conditionally.
@Configuration
@ConditionalOnProperty(name = "custom.logging.enabled", havingValue = "true", matchIfMissing = true)
public class LoggingAutoConfig {
private Logger logger = LoggerFactory.getLogger(LoggingAutoConfig.class);
@Bean
LoggingUtilFilter loggingFilter(){
logger.debug("Instantiating LoggingFilter");
return new LoggingUtilFilter();
}
}
Step 3: Register the Auto-Configuration
3.1. Add the autoconfig file details
Add a File: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.codefarm.logging.config.LoggingAutoConfig
Note — Before Spring 3.x below was the way to do this
File: META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.codefarm.logging.config.LoggingAutoConfig
Step 4: Publish the Starter
4.1. Configure Gradle for Publishing
Add the following to your build.gradle
file to publish the JAR file to a local or remote repository:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
url = layout.buildDirectory.dir("local-maven-repo")
}
}
}
Run the following command to publish the starter locally:
./gradlew publish
The starter JAR will be available in the build/local-maven-repo
directory.
Step 5: Use the Starter in Another Project
5.1. Add the Starter Dependency
Include the published starter in a new project:
Build File: build.gradle
dependencies {
implementation 'com.codefarm:logging:0.0.1-SNAPSHOT'
}
To pull the starter from the local directory add the below code too in build.gradle
repositories {
mavenCentral()
maven{
url = uri("file://E:\\codefarm\\SpringBoot\\custom-starter\\logging-starter\\build\\local-maven-repo")
}
}
5.2. Enable Custom Logging
In application.properties
:
custom.logging.enabled=true
Step 6: Test the Application
- Start the application.
- Send a request to any endpoint (e.g.,
GET /hello
). - Verify that the custom log message (
Endpoint called ………
) appears in the console.
This example demonstrates how to create and reuse a custom Spring Boot starter.
If you found this useful clap the article and follow me for more article.
Here is the video on Youtube channel #codefarm to demo this which you should checkout