Spring Boot Exit Code Generator - implementation does not work

Viewed 3719

I realize there have been a couple similar unsolved questions on this topic here on SO, however, I am including a cloneable MCVE on GitHub so it will be easy to reproduce the problem scenario.

Scenario

I am trying to implement a Spring Boot exit code generator, per the Spring Boot documentation. It looks like Spring Boot is invoking the exit code generator, but the application exits with code 0, not the given exit code. What am I missing?

@SpringBootApplication
@Configuration
@Slf4j
class App implements CommandLineRunner {

    @Autowired ConfigurableApplicationContext ctx

    static void main(String[] args) {
        SpringApplication.run(App.class, args)
    }

    @Override
    void run(String... args) throws Exception {
        SpringApplication.exit(ctx, new ExitCodeGenerator() {
            @Override
            int getExitCode() {
                log.info 'retrieving exit code.'
                return -1
            }
        })
    }
}

Output:

C:\Users\pc\IdeaProjects\spring-exit-code-gen>gradlew bootRepackage

C:\Users\pc\IdeaProjects\spring-exit-code-gen>java -jar build/libs/spring-exit-code-gen-1-0.0.1-SNAPSHOT.jar

2017-09-13 12:26:53.819  INFO 2832 --- [           main] com.scarlatti.App                        : Starting App on pc-PC with PID 2832 (C:\Users\pc\IdeaProjects\spring-exit-code-gen\build\libs\spring-exit-code-gen-1-0.0.1-SNAPSHOT.jar started by pc in C:\Users\pc\IdeaProjects\spring-exit-code-gen\build\libs)
2017-09-13 12:26:53.824  INFO 2832 --- [           main] com.scarlatti.App                        : No active profile set, falling back to default profiles: default
2017-09-13 12:26:53.966  INFO 2832 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2752f6e2: startup date [Wed Sep 13 12:26:53 CDT 2017]; root of context hierarchy
2017-09-13 12:26:54.924  INFO 2832 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-09-13 12:26:54.962  INFO 2832 --- [           main] com.scarlatti.App                        : retrieving exit code.
2017-09-13 12:26:54.963  INFO 2832 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2752f6e2: startup date [Wed Sep 13 12:26:53 CDT 2017]; root of context hierarchy
2017-09-13 12:26:54.964  INFO 2832 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-09-13 12:26:54.969  INFO 2832 --- [           main] com.scarlatti.App                        : Started App in 1.945 seconds (JVM running for 3.132)

C:\Users\pc\IdeaProjects\spring-exit-code-gen>echo %ERRORLEVEL%
0

Clone from GitHub here to try it out.

1 Answers
Related