1

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?

1Z10
  • 2,801
  • 7
  • 33
  • 82
  • Why are you executing install? Do you need this project in another project as dependency? – Simon Martinelli Oct 03 '22 at 13:07
  • I use it to install dependencies, to download the OpenAPI Spec and to generate the OpenAPI Client lib. – 1Z10 Oct 03 '22 at 13:11
  • Yes but for that you don't need to run install. mvn package is all you need – Simon Martinelli Oct 03 '22 at 13:37
  • I tried it but it does not download the API yaml spec nor generate the client. With mvn clean install it does everything in one command. – 1Z10 Oct 03 '22 at 13:44
  • 1
    This is because you have the open API bound to integration-test You should bind this to compile for example. And don't call clean always. https://andresalmiray.com/maven-verify-or-clean-install/ – Simon Martinelli Oct 03 '22 at 14:00

2 Answers2

1

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.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • I updated my question with the test application properties & `@ConditionalOnProperty` on the `@Configuration` `@EnableScheduling` class, however, I'm still getting scheduling enabled during builds. – 1Z10 Oct 03 '22 at 13:07
  • @1Z10 do you have anywhere a `@TestConfiguration` ? – Panagiotis Bougioukos Oct 03 '22 at 13:35
  • no, just a `@SpringBootTest` class and some `@Test` methods. I did also try @MockBean(SchedulingConfiguration.class) without success. – 1Z10 Oct 03 '22 at 13:38
  • @1Z10 try to remove the property `scheduling: enabled: true` from `src/main/resources/application.yml` and then add to your annotation `@ConditionalOnProperty(prefix = "scheduling", value = "enabled", havingValue = "true", matchIfMissing = true)` – Panagiotis Bougioukos Oct 03 '22 at 13:39
  • same thing. Maybe it is enables somewhere else by Spring depencencies ? https://stackoverflow.com/questions/40684903/disable-schedule-on-spring-boot-integrationtest/46783392#46783392 – 1Z10 Oct 03 '22 at 13:55
  • @1Z10 yes `actuator` by default enables scheduling configuration as well. but you must not see the message from `System.out.println("Scheduling enabled: " + this.schedulingEnabled);` since this component would not be registered at all – Panagiotis Bougioukos Oct 03 '22 at 14:00
  • I did just upload a minimum git repo on GitHub https://github.com/MaurizioCasciano/Scheduling-Demo I think the problem is with the start and stop goals of the spring-boot maven plugin. However they are required by the SpringDoc generate goal. It seems to just start the app as usual instead of with test properties file. – 1Z10 Oct 03 '22 at 14:40
0

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.

1Z10
  • 2,801
  • 7
  • 33
  • 82