Issue
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, href="https://springdoc.org/#maven-plugin" rel="nofollow noreferrer">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?
Solution
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.
- Annotate the Scheduling
@Configuration
in order to be disabled when thetest
profile is active. - Define the active profile equal to
test
insrc/test/resources/application.yml
- Define the active profile equal to
test
in thespring-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:
- point 1 allows disabling scheduling when the test profile is active.
- point 2 allows disabling scheduling during the execution of test classes.
- 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.
Answered By - 1Z10
Answer Checked By - Willingham (JavaFixing Volunteer)