Search code examples
javaspringgradledependenciesmulti-module

Using gradle to conditionally add beans to a spring project


I'm trying to think of a way to conditionally add (or augment) behaviour in my Spring boot project. The use-case is such: When a project is built for a particular customer, they have custom functionality requirements. I don't want the (unused) custom functionality class files to exist in the deployment for other customers, so I'm thinking about a multi module project that's using a gradle build argument to include the custom functionality module. However, I think there's a circular dependency issue where the custom functionality depends on components from the main project - but to include the custom functionality in the build, the main project has to depend on the custom module. I've tried including the module in the parent gradle.settings, having the module depend on the main project and then using the same package name and structure as what exists in the main project in a hopeful attempt that classpath scanning would pick up the class and create the bean - but it seems unless I have the main project depend on the custom code it doesn't seem to want to work.

Is there something simple I've missed or a better way to go about what I'm trying to do? Regards


Solution

  • I discovered a simple and effective way to do it:

    if (project.hasProperty("feature-a")) {
        runtimeOnly project(':feature-a')
    }