Reputation: 1773
I have a large Spring Boot monolithic Web Application project. This application is packaged as an executable JAR and serves all kinds of JSON REST endpoints.
Now, I sometimes want to run a piece of Java code to process or import a large file, or clean up certain database tables from the command line.
What would be good way to do this with Spring Boot?
I first looked into CommandLineRunner
interface but this seems to serve a completely different use case. This is executed always when running the Spring Boot application, followed by starting the main application.
I would like to have this functionality in the same application as the main web app for various reasons:
Upvotes: 0
Views: 1580
Reputation:
If you want to reuse the same jar, you could use a combination of Profiles
and CommandLineRunners
.
@Configuration
public class BatchConfig {
@Bean
@Profile("import")
public CommandLineRunner import() {
// ...
}
@Bean
@Profile("dbClean")
public CommandLineRunner dbClean() {
// ...
}
}
Then, when you run the jar, pass the desired profile as argument.
java -jar -Dspring.profiles.active=dbClean yourJar.jar
In this way, your command line runners are executed only when the profile matches.
Upvotes: 2