Minemosynne
Minemosynne

Reputation: 43

How to set an environment variable in application.yml - Spring Boot

I'm working on a spring boot application and I'm writing some tests which use MockServer. In order to not have all the requests to the mock servers displayed in the logs, I have to set the environment variable mockserver.logLevel to OFF. When I do it via command line, it works perfectly :

mvn clean install -Dmockserver.logLevel="OFF"

but when I try to do it in my application.yml, it doesn't work. I've tried the following :

mockserver:
    log-level: OFF

mockserver:
    loglevel: OFF

mockserver:
    logLevel: OFF

logging:
  level:
    org.mockserver: OFF

But none of these work. I guess I don't write it correctly in the application.yml but I can't figure out the right way.

Upvotes: 0

Views: 564

Answers (2)

Attila Hejja
Attila Hejja

Reputation: 79

an another 'wiring' solution:

@Configuration
public class MockServerConfiguration {
    ... 
    public MockServerConfiguration(@Value("${mockserver.logLevel}") String mockserverLogLevel) {
        if (Objects.isNull(System.getProperty("mockserver.logLevel"))) {
            System.setProperty("mockserver.logLevel", mockserverLogLevel);
        }
    }
    ...
}

Upvotes: 0

Minemosynne
Minemosynne

Reputation: 43

I finally found a way by adding it directly into the pom.xml as a system property variable in the concerned plugin :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${version.maven-failsafe-plugin}</version>
    <configuration>
        <systemPropertyVariables>
            <mockserver.logLevel>OFF</mockserver.logLevel>
        </systemPropertyVariables>
    </configuration>
</plugin>

Upvotes: 1

Related Questions