Search code examples
javaspring-bootspring-profilesmaven-profiles

Spring Boot Maven Application Containerized with Docker - build WAR based on profile flag


I have a spring boot application that makes use of profile files 'application-dev.properties' and 'application-test.properties'. The project is containerized by using Docker, look at the code below.

Running 'RUN mvn package' works, but I want to build based on one of the profiles. Is there a way I can achieve this? Because "RUN mvn package -Dmaven.test.skip=true -P test" doesn't seem like to work..

 FROM maven:3.3.9-jdk-8-alpine as build-env
 COPY pom.xml /tmp/
 COPY settings.xml /root/.m2/settings.xml
 COPY src /tmp/src/

 WORKDIR /tmp/
 RUN mvn package -Dmaven.test.skip=true -P test

 FROM tomcat:8.0
 COPY --from=build-env /tmp/target/cbm-server-0.0.1-SNAPSHOT.war $CATALINA_HOME/webapps/cbm- server.war

 EXPOSE 8009
 EXPOSE 8080
 CMD ["catalina.sh", "run"]

Solution

  • You need to provide a profile to the runtime phase. You can do it with an environment variable.

    ENV JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=test"