How to create a clean Spring Framework project for Gradle with IntelliJ IDEA

How to create a clean Spring Framework project for Gradle with IntelliJ IDEA

Create a clean Spring Framework project for Gradle with IntelliJ IDEA

In this tutorial, we are going to set up a clean Spring framework project with Gradle and IntelliJ IDEA. Here we are not using Spring Boot initializer.

Project Setup with IntelliJ IDEA

Article Image
  • Launch IntelliJ IDEA, click New Project (or go to File > New > Project)
  • On the left side, select Java, give the project name(csbyte), and choose the location to save the project
  • Select Groovy as Gradle DSL
  • Select the latest Gradle version. Here, we are using the default 8.5 Gradle version. Although we need Gradle 9, there might be some issues doing so; we will discuss it later.
  • Give the groupId and ArtifactId, and create the project
The project Structure will look like this:
Article Image
As discussed previously, we will get the following error:
bash
Unsupported class file major version 69
This is due to the incompatibility of the Gradle version used for JDK 25.
One way to do it is to use the required Gradle version inside gradle-wrapper.properties file.
bash
distributionUrl=https\://services.gradle.org/distributions/gradle-9.1-bin.zip
Using this in the properties file, we can get the following error. This is due to a compatibility mismatch between the Gradle 9.x version and the way IntelliJ IDEA injects temporary run scripts behind the scenes.
Error:
bash
Initialization script '/tmp/Main_main__1.gradle' line: 27

* What went wrong:
A problem occurred configuring root project 'spring-tutorial'.
> Could not get unknown property 'convention' for root project 'spring-tutorial' of type org.gradle.api.Project.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
So, we will not use this setup; leave Gradle 8 as it is.

Update build.gradle with a Toolchain

In order to fix this issue, inside build.gradle the file, add the toolchain for the Java version. This will force Gradle to use JDK 25 for the compiler.
bash
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
    }
} 

Update the wrapper properties

Use the following Gradle version in gradle-wrapper.properties
bash
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.4-bin.zip
This will fix the issues.

Create Spring Framework Project

Add dependencies

Now, let's create the project for this, update the build.gradle file with the following dependencies
bash
 // Import Spring Framework BOM to align core versions
    implementation platform("org.springframework:spring-framework-bom:7.0.7")
    
    implementation 'org.springframework:spring-context'
The use of spring-framework-bom dependency is that you don't explicitly need to define the versions for sub dependencies like spring-context, beans etc, we are using Spring Framework 7, so we use the spring-framework-bom 7
spring-context is a core container dependency which includes beans, core, aop, expression, etc.

Create a sample example

Let's create a sample example to test the setup.
Create a service class
java
package org.csbyte.sample;

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public String getGreeting() {
        return "Hello from pure Spring Framework!";
    }
}
Create the config class to set componentScan
java
package org.csbyte.sample;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "org.csbyte")
public class AppConfig {
}
Add your own base package.
Now, update the main entry point.
java
package org.csbyte;

import org.csbyte.sample.AppConfig;
import org.csbyte.sample.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        try (AnnotationConfigApplicationContext context =
                     new AnnotationConfigApplicationContext(AppConfig.class)) {

            // Retrieve a managed bean from the context
            MyService service = context.getBean(MyService.class);

            // Use it
            System.out.println(service.getGreeting());
        } // The try-with-resources block safely closes the context here
    }
} 
Now, run the application
Output:
bash
> Task :Main.main()
Hello world!
Hello from pure Spring Framework!
This is how we can set up the clean Spring Framework project. This is useful for learning the core concept of Spring Framework and also designing the parent microservice build.