fabric8io / docker-maven-plugin

Maven plugin for running and creating Docker images
https://dmp.fabric8.io
Apache License 2.0
1.88k stars 643 forks source link

Imagenames are wrong in case of several profiles #1439

Open Captain-P-Goldfish opened 3 years ago

Captain-P-Goldfish commented 3 years ago

Hi we got the following describes setup. We are using two profiles. One to push into our own registry and one to push into the registry of our customer. If both profiles are executed with mvn clean package -P docker-image-to-customer-registry,docker-image-to-private-registry I would expect to have 2 images in my local machine registry:

  1. customer/${project.artifactId}:${project.version}
  2. private/${project.artifactId}:${project.version}

The log shows clearly that both profiles are executed successfully but both profiles are building an image with the same name:

So it seems clear to me that the last setup name does always win.

<profile>
            <id>docker-image-to-customer-registry</id>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>io.fabric8</groupId>
                            <artifactId>docker-maven-plugin</artifactId>
                            <version>${version.docker.maven.plugin}</version>
                            <executions>
                                <execution>
                                    <id>build-for-customer</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>build</goal>
                                    </goals>
                                </execution>
                            </executions>
                            <configuration>
                                <verbose>true</verbose>
                                <images>
                                    <image>
                                        <registry>${customer.url}</registry>
                                        <name>customer/${project.artifactId}:${project.version}</name>
                                        <build>
                                            <contextDir>.</contextDir>
                                            <assembly>
                                                <mode>dir</mode>
                                                <inline xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
                                                        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
                                                    <files>
                                                        <file>
                                                            <source>
                                                                target/${project.artifactId}-exec.jar
                                                            </source>
                                                        </file>
                                                    </files>
                                                </inline>
                                            </assembly>
                                            <tags>
                                                <tag>latest</tag>
                                            </tags>
                                        </build>
                                    </image>
                                </images>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
        <profile>
            <id>docker-image-to-private-registry</id>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>io.fabric8</groupId>
                            <artifactId>docker-maven-plugin</artifactId>
                            <version>${version.docker.maven.plugin}</version>
                            <executions>
                                <execution>
                                    <id>build-for-private</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>build</goal>
                                    </goals>
                                </execution>
                            </executions>
                            <configuration>
                                <verbose>true</verbose>
                                <images>
                                    <image>
                                        <registry>${private.url}</registry>
                                        <name>private/${project.artifactId}:${project.version}</name>
                                        <build>
                                            <contextDir>.</contextDir>
                                            <assembly>
                                                <mode>dir</mode>
                                                <inline xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
                                                        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
                                                    <files>
                                                        <file>
                                                            <source>
                                                                target/${project.artifactId}-exec.jar
                                                            </source>
                                                        </file>
                                                    </files>
                                                </inline>
                                            </assembly>
                                            <tags>
                                                <tag>latest</tag>
                                            </tags>
                                        </build>
                                    </image>
                                </images>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
rohanKanojia commented 3 years ago

@Captain-P-Goldfish : Thanks a lot for reporting this. Do you have some quick reproducer project which we can try out to reproduce ?

Captain-P-Goldfish commented 3 years ago

here you go: https://github.com/Captain-P-Goldfish/maven-docker-plugin-issue-1439

rohanKanojia commented 3 years ago

Thanks a lot for sharing the reproducer project. I can reproduce this issue.

rohanKanojia commented 3 years ago

On debugging, I'm realizing that it's maven issue rather than being related to plugin. I made a simple project with exec-maven-plugin just like yours(https://github.com/r0haaaan/exec-maven-plugin-multiple-profile-execution) and I was able to see same issue.

I had two profiles like this:

    <profiles>
        <profile>
            <id>hello</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <execution>
                                <id>hello</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <executable>maven</executable>
                            <mainClass>maven.testing.HelloPrint</mainClass>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>bye</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                            <execution>
                                <id>bye</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <executable>maven</executable>
                            <mainClass>maven.testing.ByePrint</mainClass>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

When I run mvn clean install -Phello,bye I can see only second profile configuration being picked up:

exec-maven-plugin-multiple-profile-execution : $ mvn clean install -Phello,bye
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< org.example:exec-maven-plugin-testing >----------------
[INFO] Building exec-maven-plugin-testing 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ exec-maven-plugin-testing ---
[INFO] Deleting /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ exec-maven-plugin-testing ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ exec-maven-plugin-testing ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ exec-maven-plugin-testing ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ exec-maven-plugin-testing ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ exec-maven-plugin-testing ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ exec-maven-plugin-testing ---
[INFO] Building jar: /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/target/exec-maven-plugin-testing-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (hello) @ exec-maven-plugin-testing ---
Bye
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (bye) @ exec-maven-plugin-testing ---
Bye
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ exec-maven-plugin-testing ---
[INFO] Installing /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/target/exec-maven-plugin-testing-1.0-SNAPSHOT.jar to /home/rohaan/.m2/repository/org/example/exec-maven-plugin-testing/1.0-SNAPSHOT/exec-maven-plugin-testing-1.0-SNAPSHOT.jar
[INFO] Installing /home/rohaan/work/repos/exec-maven-plugin-multiple-profile-execution/pom.xml to /home/rohaan/.m2/repository/org/example/exec-maven-plugin-testing/1.0-SNAPSHOT/exec-maven-plugin-testing-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.515 s
[INFO] Finished at: 2021-03-13T15:15:07+05:30
[INFO] ------------------------------------------------------------------------
Captain-P-Goldfish commented 3 years ago

oh great... I just tried to put the configuration into the execution-section and it works like this. This will at least do for me. But did not expect this. It's the first time I am having problems with differnet plugin configurations over different profiles...