Home > Mobile >  Can I compile Spring Boot application to have only one controller and disable the rest?
Can I compile Spring Boot application to have only one controller and disable the rest?

Time:10-12

I have a (very very large) Spring Boot application. This is a large legacy application. Assume that I have dozens of Controllers and routes.

I want to do limited test deployments. Also, for licensing reasons, I want to package and deploy 5 controllers and remove the rest (not just disable, but remove).

Is this possible to do using some Gradle/Spring magic ? Can I select just 5 controllers to be packaged as part of the jar and the rest 30 should not even be present in the output ? I would not like to ship the code for the other controllers.

I can force each controller to be in a different package. So I can say that I want package a, b,c to be in the JAR file...but remove d,e,f. Or some other way ?

CodePudding user response:

From the Spring Boot Gradle Plugin Reference Guide:

The BootJar and BootWar tasks are subclasses of Gradle’s Jar and War tasks respectively. As a result, all of the standard configuration options that are available when packaging a jar or war are also available when packaging an executable jar or war.

Based on the above this should work:

bootJar {
  excludes = [
    'com/acme/controller/NotPaidForController.class',
    'com/acme/controller/shoppingcart/*.class',
    'com/acme/controller/**/*Delete.class'
    // etc.
  ]
}

If it does work then I'd assume a similar solution using the bootWar task if your legacy application is packaged as such.

Note that physically excluding classes from the jar/war might cause severe runtime errors, i.e. Spring might still look for them when initializing the application context. In order to avoid this, I recommend using Spring @Profiles or @Conditional components instead. Using these techniques the controller classes would be still part of the deployment, they would just not be considered as candidates during the initialization of the application context which as you mentioned might not be what you want.

  • Related