Home > Enterprise >  How to disable Spring-Boot scheduling during Maven Clean Install?
How to disable Spring-Boot scheduling during Maven Clean Install?

Time:10-04

I have a @Configuration class annotated with @EnableScheduling and I want it to be disabled during the mvn clean install command.

The problem is that it also triggers the start & stop goals, due to the Spring-Doc maven plugin, https://springdoc.org/#maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot-maven-plugin.version}</version>
    <configuration>
        <jvmArguments>-Dspring.application.admin.enabled=true</jvmArguments>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>start</goal>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Is there a way to set some environment variable, e.g. SCHEDULING_ENABLED=false, during the maven clean install command in order to avoid the scheduling jobs at compile time ?

Update

I can't simply skip the springdoc generate loagoal because I have to download the OpenAPI yaml file.

I updated my configuration class as follows:

@Configuration
@ConditionalOnProperty(prefix = "scheduling", value = "enabled", havingValue = "true")
@EnableScheduling
public class SchedulingConfiguration {

    @Value("${scheduling.enabled}")
    private Boolean schedulingEnabled;

    @PostConstruct
    public void init(){
        System.out.println("Scheduling enabled: "   this.schedulingEnabled);
    }
}

and these are th two application.yml:

src/main/resources/application.yml

scheduling:
  enabled: true

src/test/resources/application.yml

scheduling:
  enabled: false

However, during the mvn clean install command I alway get Scheduling enabled: true on the console.

Is there something I am missing?

Update 2

I did upload a minimal project on GitHub https://github.com/MaurizioCasciano/Scheduling-Demo showing the problem.

My idea is that the following pom.xml snippet, required by Spring-Doc, simply starts the app as usual without any particular profile, e.g. test properties:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>start</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-maven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <apiDocsUrl>http://localhost:8081/v3/api-docs.yaml</apiDocsUrl>
                <outputFileName>API.yaml</outputFileName>
                <outputDir>src/main/resources/api</outputDir>
            </configuration>
        </plugin>
    </plugins>
</build>

Is it possible to specify here the properties to load or the Spring-Boot profile to activate?

CodePudding user response:

According to the documentation the relevant configuration is the<skip>true</skip>

<plugin>
 <groupId>org.springdoc</groupId>
 <artifactId>springdoc-openapi-maven-plugin</artifactId>
 <version>1.4</version>
 <executions>
  <execution>
   <id>integration-test</id>
   <goals>
    <goal>generate</goal>
   </goals>
  </execution>
 </executions>
  <configuration>
    <skip>true</skip>    <----------------
 </configuration>
</plugin>

This will avoid the application execution during this phase of the build so the scheduller will not be executed as well.

But this will work only if you dont have integration tests related with documentation. Otherwise you would want the application to be started during this phase.

If you still want the application to startup during integration-test phase as to make some integration testing on documentation you can follow this answer in order to exclude the enable scheduling configuration for tests. In integration-test phase normally your application will load application.yml from test classpath. So there you can provide the adviced property to switch it of.

CodePudding user response:

After a lot of trials and errors I came to the conclusion that the only way to disable Scheduling during the maven clean instal command, or in other words during the following goals/phases:

  • Spring-Boot-Maven-Plugin start
  • Spring-Boot-Maven-Plugin stop
  • SpringDoc-OpenAPI-Maven-Plugin integration-test

is as follows.

  1. Annotate the Scheduling @Configuration in order to be disabled when the test profile is active.
  2. Define the active profile equal to test in src/test/resources/application.yml
  3. Define the active profile equal to test in the spring-boot-maven-plugin execution configuration.

@Configuration
@Profile("!test")
@EnableScheduling
public class SchedulingConfiguration {
    @PostConstruct
    public void init(){
        System.out.println("Scheduling enabled");
    }
}

spring:
  profiles:
    active: test

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <configuration>
                        <profiles>
                            <profile>test</profile>
                        </profiles>
                    </configuration>
                    <goals>
                        <goal>start</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Explanation of what I understood:

  1. point 1 allows disabling scheduling when the test profile is active.
  2. point 2 allows disabling scheduling during the execution of test classes.
  3. point 3 allows disabling scheduling when the app is started for allowing the download of the OpenAPI documentation.

Here https://github.com/MaurizioCasciano/Scheduling-Demo/tree/Disable-Scheduling-On-Tests you can find the working code on GitHub.

  • Related