Search code examples
springdockermavenpom.xml

Build docker image with a custom name using Spring Boot Maven plugin


In my pom.xml file, I configured the imageName to be the project name by default:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-image</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <imageName>${project.name}</imageName>
                </configuration>
            </plugin>
        </plugins>
</build>

When I run mvn spring-boot:build-image, it works fine.

When I try to override the image name using mvn spring-boot:build-image -Dspring-boot.build-image.imageName=customname, I was expecting to get a docker image named customname. I'm still getting the project name instead. This means that maven plugin is still using ${project.name}.

Am using the command in a wrong way?


Solution

  • In your case the name of image always comes from plugin configuration <imageName>${project.name}</imageName>. If you want to support both options to specify target image name (i.e. maven base configuration and CLI) you may take advantage of using maven properties, i.e.:

    <properties>
      <module.image.name>${project.name}</module.image.name>
    </properties>
    
    
    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build-image</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <imageName>${module.image.name}</imageName>
                    </configuration>
                </plugin>
            </plugins>
    </build>
    

    after that both mvn spring-boot:build-image and mvn spring-boot:build-image -Dmodule.image.name=customname (note another name of system property) will work as expected