Explain Codes LogoExplain Codes Logo

How do I tell Spring Boot which main class to use for the executable jar?

java
spring-boot
maven
gradle
Anton ShumikhinbyAnton Shumikhin·Nov 16, 2024
TLDR

In structuring your Spring Boot application, setting up the start-class property (for Maven) or mainClassName property (for Gradle) is your magic wand:

Maven:

<properties> <start-class>your.main.Class</start-class> </properties>

Gradle:

springBoot { mainClassName = 'your.main.Class' }

Remember to replace your.main.Class with your main class's fully qualified name. This makes your main class the star of the show when running the jar.

Enlightening details phase

Maven: Setting up the main class

Setting up your <start-class> in your Maven project's pom.xml propels your project configurability to new heights. It's like a breadcrumb trail Maven plugins, like spring-boot-maven-plugin, follow when bundling your project.

Maven: Defining your main class in the plugin

But hey, sometimes we need to take the reins. Let's mingle with the spring-boot-maven-plugin configuration to get a firmer grasp:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>your.main.Class</mainClass> </configuration> </plugin> </plugins> </build>

Your designated <mainClass> better be having a friendly public static void main(String[] args) get-together, since that's your application's launchpad. No main method, no party! 🎉

Gradle: The place of your main class

Gradle isn't any different – see it in build.gradle:

springBoot { mainClassName = 'your.main.Class' // Gradle says hi! 👋 }

It helps, especially in multi-module Gradle projects, where the entry point class might feel like playing hide-and-seek.

Selector for main classes

When it feels like a main class convention, it's time to pick a single winner. This eliminates spring startup conflicts and guarantees that the application takes off smoothly.

Smoothing operations phase

Trust Spring Boot's judgement

In many cases, embracing Spring Boot's defaults is your best bet. If you're using spring-boot-starter-parent, your main class is usually brought into the spotlight without an audition.

Packaging regulations

Made some tweaks? Time for a fresh repackage! Maven can help with mvn package. Make sure your jar maintains its good looks and structure—yes, it's still a ZIP file at heart.

Avoid setting traps for yourself

Ensure your configuration is not leading to application startup failure due to these common culprits:

  1. A multiple main classes ballgame. Make sure Spring Boot is not partaking in this.
  2. Conflicting beans or context issues with your main class.

Stay sharp, learn more phase

Don't forget to do your homework

The official Spring Boot documentation is always waiting with open arms. A treasure trove of golden nuggets, right from how you can fine-tune Spring Boot's functionalities to executable jar configurations.

Stay tuned for updates

Spring Boot is a thrill ride of releases and updates. Stay updated and prepared to surf its waves!

Drone or be a part of the community

Spring Boot boasts an enormous, friendly community. A treasure trove of challenges and solutions lie in forums, discussions, and blogs. Don't miss out!