1

I want to ask a question for what difference between mvn clean install and mvn clean package -Pbuild-image is when I create a image to be deployed Docker for running it as a container.

I use generally use mvn clean install and I notice that this command named mvn clean package -Pbuild-image is also used for this purpose.

Can you explain its difference? What is the -Pbuild-image ? Can you share its explanation?

S.N
  • 2,157
  • 3
  • 29
  • 78

1 Answers1

1

clean install compiles the source code, runs the tests (unless disabled using -DskipTests), packages the application, and installs to the local repository (a bunch of directories and files in the .m2 directory on your disk).

On the other hand clean package -Pbuild-image runs clean package with the build-image profile. I assume you know the difference between package and install (otherwise, read this). This is where the difference ends from the Maven's perspective.

Maven knows nothing about any profile unless there is any functionality bound to it. Until then, the command is effectively equal to clean package. Once you place a plugin execution under such a profile, then additional things start to happen depending on the configuration (what) and execution phase (when). Here is a sample profile build-image with the plugin execution in the package phase taken from https://piotrminkowski.com/2023/05/26/spring-boot-development-mode-with-testcontainers-and-docker/:

<profile>
  <id>build-image</id>
  <build>
    <plugins>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <to>
            <image>sample-spring-microservices-advanced/${project.artifactId}:${project.version}</image>
          </to>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>dockerBuild</goal>
            </goals>
            <phase>package</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Thank you for your response. Is it possible to explain `-Pbuild-image` with adding its documentation? – S.N Jun 30 '23 at 21:05
  • It is just a custom profile, nothing else. Even if you name it as `jumping-cow` and put the plugin execution under that, the behavior is the same. Search for "Maven Profiles". – Nikolas Charalambidis Jul 01 '23 at 14:00