Search code examples
spring-bootmavengitlab-ci

How to (implicitly) pass environment variables to Maven build in gitlab-ci.yml for a Spring Boot application?


In a gitlab-ci.yml I'm building a Spring Boot app using Maven. To replace placeholder @MY_PROP@ in application.properties (using the Maven resources plugin) MY_PROP is passed as -D parameter to the mvn command. Since there are a lot of parameters in my build, is there a way to pass environment varaibles to Maven without explicitly passing each individual env parameter via -D?

application.properties 
    my.prop=@MY_PROP@

gitlab env variable 
    MY_PROP=4711

gitlab-ci.yml
    Build:
        image: maven:3-jdk-11
        stage: build
        script:
            - mvn clean package deploy -s ci_settings.xml --batch-mode -DMY_PROP=$MY_PROP
        

Solution

  • From the Maven reference: https://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html

    You can do:

    env.*

    Environment variables like PATH and M2_HOME can be referenced using the env.* prefix.

    So in your pom.xml you can reference them as:

    ${env.MY_PROP}
    

    In your case, to replace with resource plugin you can write:

    [email protected]_PROP@