Put multiple Main classes in one Spring Project

Overview

If your project has multiple batches, you have the option of putting multiple batches in one project, splitting them into multiple projects, and so on. In this article, I tried the former approach of putting multiple batches in one project and separating the Main class.

Premise

>gradle -v

------------------------------------------------------------
Gradle 6.5
------------------------------------------------------------

Build time:   2020-06-02 20:46:21 UTC
Revision:     a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4

Kotlin:       1.3.72
Groovy:       2.5.11
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.2 (Oracle Corporation 11.0.2+9)
OS:           Windows 10 10.0 amd64

STS 3.9.9.RELEASE (I have to make it a sloppy 4 system ...)

Project structure

multiple-main └src/main/java  └com/example/dem   ├MultipleApplication.java   └MultipleApplication2.java

How to call the Main class

For STS

When you try to execute Spring Application, the screen to select the Main class will appear as shown in the figure below, so select the one you want to execute. スクリーンショット 2020-11-03 094542.png

For Gradle

After loading the application plug-in, specify the mainClass. It's inconvenient because you have to rewrite build.gradle one by one. (Reference link 1)

plugins {
	id 'org.springframework.boot' version '2.3.5.RELEASE'
	id 'io.spring.dependency-management' version '1.0.10.RELEASE'
	id 'java'
	id 'application'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
mainClassName = "com.example.demo.MultipleMainApplication2"

repositories {
	mavenCentral()
}

In case of jar execution

java -cp multiple-main-0.0.1-SNAPSHOT.jar -Dloader.main=com.example.demo.MultipleMainApplication org.springframework.boot.loader.PropertiesLauncher

(Reference link 2)

PropertiesLauncher is Who? However, even if the mainClass of build.gradle is MultipleMainApplication2, I was able to start MultipleMainApplication (unmarked).

Summary

It's not impossible, but I felt it was awkward. In the Main class, it is difficult to handle beans under Spring management (it is not impossible to handle), so if you want to put multiple processes in one project, I think that it is better to switch Runner by referring to the following article. (This method was also adopted in the field).

Put multiple CommandLineRunner / ApplicationRunner implementation classes in the executable jar of Spring Boot and switch the class to be executed by the properties at startup --Qiita

Reference link

  1. Chapter 45 Application Plugins
  2. Spring Boot: Main Class Settings
  3. Put multiple CommandLineRunner / ApplicationRunner implementation classes in the executable jar of Spring Boot and switch the class to be executed by the properties at startup --Qiita

Recommended Posts

Put multiple Main classes in one Spring Project
Create Java Spring Boot project in IntelliJ
Run a Spring Boot project in VS Code
Java tips-Create a Spring Boot project in Gradle
How to define multiple orm.xml in Spring4, JPA2.1
View the Gradle task in the Spring Boot project
Get multiple Resources that match the pattern in spring
How to create a Spring Boot project in IntelliJ
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)