spotify / docker-maven-plugin

INACTIVE: A maven plugin for Docker
Apache License 2.0
2.67k stars 575 forks source link

Overriding arguments from command line doesn't work #367

Closed Davio closed 6 years ago

Davio commented 6 years ago

Description

Overriding arguments from command line doesn't work

How to reproduce

Specify repository and tag in your config

<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <configuration>
          <repository>foo/repo</repository>
          <tag>latest</tag>
        </configuration>
      </plugin>

Run the plugin with mvn dockerfile:tag -Ddockerfile.repository=other/repo -Ddockerfile.tag=override

What do you expect

The repository and tag should be overridden with the values from the command line

What happened instead

The values from the POM are used instead. --- dockerfile-maven-plugin:1.3.7:tag (default-cli) @ my-module --- [INFO] Tagging image abcdefg as foo/repo:latest The created docker-info.jar reflects these wrong values

Software:

Server: Version: 17.09.0-ce API version: 1.32 (minimum version 1.12) Go version: go1.8.3 Git commit: afdb6d4 Built: Tue Sep 26 22:45:38 2017 OS/Arch: linux/amd64 Experimental: true

mattnworb commented 6 years ago

I believe this is simply how Maven works - you cannot override a <configuration> property explicitly set in your pom.xml from the command line.

Here is an example with the maven-compiler-plugin, where the target property defaults to the maven.compiler.target user property if not otherwise configured:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mattnworb</groupId>
    <artifactId>sandbox</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Verifying that 1.8 is the value that is used (from the pom.xml setting):

$ mvn clean compile -X | grep -B 20 "target ="
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.1:compile' with basic configurator -->
...
[DEBUG]   (f) target = 1.8

Attempting to override it with -Dmaven.compiler.target has no effect:

$ mvn -Dmaven.compiler.target=1.9 clean compile -X | grep -B 20 "target ="
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.1:compile' with basic configurator -->
...
[DEBUG]   (f) target = 1.8

If I remove <target>1.8</target> from the pom though, then the -D setting works:

[mattbrown@mattmbp sandbox]$ mvn -Dmaven.compiler.target=1.9 clean compile -X | grep -B 20 "target ="
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.1:compile' with basic configurator -->
...
[DEBUG]   (f) target = 1.9

If you want to have a default in your pom.xml that you can override with a -D property, then I think the path to go is to have a <properties> section in your pom.xml such as:

  <properties>
    <tag>latest</tag>
  </properties>
...
          <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <configuration>
              <repository>gcr.io/foobar/${project.artifactId}</repository>
              <tag>${tag}</tag>
            </configuration>
            ...
          </plugin>
$ mvn package | grep "Image will be built as"
[INFO] Image will be built as gcr.io/foobar/foobar:latest

$ mvn package -Dtag=this-was-overridden | grep "Image will be built as"
[INFO] Image will be built as gcr.io/foobar/foobar:this-was-overridden