<?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>

    <parent>
        <groupId>io.confluent</groupId>
        <artifactId>common</artifactId>
        <version>4.0.3</version>
    </parent>

    <groupId>io.confluent</groupId>
    <artifactId>common-docker</artifactId>
    <packaging>pom</packaging>

    <name>${project.artifactId}</name>
    <description>Common POM with Docker image support</description>

    <modules>
        <module>assembly-plugin-boilerplate</module>
        <module>utility-belt</module>
        <module>base</module>
    </modules>

    <repositories>
        <repository>
            <id>confluent</id>
            <url>http://packages.confluent.io/maven/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>confluent</id>
            <url>http://packages.confluent.io/maven/</url>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <GIT_COMMIT>unknown</GIT_COMMIT>
        <BUILD_NUMBER>-1</BUILD_NUMBER>
        <docker.skip-build>true</docker.skip-build>
        <docker.skip-test>true</docker.skip-test>
        <docker.registry>placeholder/</docker.registry>  <!-- Registry for this build. Must specify a value, or exec plugin throws NPE :((( -->
        <docker.tag>${project.version}</docker.tag>  <!-- Tag for this build -->
        <docker.upstream-registry>${docker.registry}</docker.upstream-registry>  <!-- Registry for base images -->
        <docker.upstream-tag>${docker.tag}</docker.upstream-tag>  <!-- Tag for base images -->
        <docker.test-registry>${docker.upstream-registry}</docker.test-registry>  <!-- Registry for integration test dependencies -->
        <docker.test-tag>${docker.upstream-tag}</docker.test-tag>  <!-- Tag for integration test dependencies -->
        <dockerfile-maven-plugin.version>1.3.6</dockerfile-maven-plugin.version>
        <exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
        <licenses.version>${project.version}</licenses.version>
        <slf4j-api.version>${slf4j.version}</slf4j-api.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>assembly-plugin-boilerplate</artifactId>
            <version>${confluent.version}</version>
            <classifier>resources</classifier>
            <type>zip</type>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>docker</id>
            <activation>
                <property>
                    <name>env.DOCKER_HOST</name>
                </property>
            </activation>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unpack-assembly-plugin-boilerplate</id>
                                <goals>
                                    <goal>unpack-dependencies</goal>
                                </goals>
                                <phase>process-sources</phase>
                                <configuration>
                                    <includeArtifactIds>assembly-plugin-boilerplate</includeArtifactIds>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>make-assembly-for-docker</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <descriptors>
                                        <descriptor>target/dependency/assembly-plugin-boilerplate-${project.version}/package.xml</descriptor>
                                    </descriptors>
                                    <archive>
                                        <manifest>
                                            <mainClass>${main-class}</mainClass>
                                        </manifest>
                                    </archive>
                                    <attach>false</attach>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>${exec-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>create-licenses-for-docker</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>io.confluent.licenses.LicenseFinder</mainClass>
                                    <arguments>
                                        <!-- Note use of development instead of package so we pick up all dependencies.
                                             This assumes both subprojects will be packaged together. -->
                                        <argument>-i ${project.build.directory}/${project.build.finalName}-package/share/java/${project.artifactId}</argument>
                                        <argument>-f</argument>
                                        <argument>-h ${project.build.directory}/${project.build.finalName}-package/share/doc/${project.artifactId}/licenses.html</argument>
                                        <argument>-l ${project.build.directory}/${project.build.finalName}-package/share/doc/${project.artifactId}/licenses</argument>
                                        <argument>-n ${project.build.directory}/${project.build.finalName}-package/share/doc/${project.artifactId}/notices</argument>
                                        <argument>-x licenses-${project.version}.jar</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>python-docker-tests</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <skip>${docker.skip-test}</skip>
                                    <environmentVariables>
                                        <TOX_TESTENV_PASSENV>DOCKER_REGISTRY DOCKER_TAG DOCKER_TEST_REGISTRY DOCKER_TEST_TAG DOCKER_HOST</TOX_TESTENV_PASSENV>
                                        <DOCKER_REGISTRY>${docker.registry}</DOCKER_REGISTRY>
                                        <DOCKER_TAG>${docker.tag}</DOCKER_TAG>
                                        <DOCKER_TEST_REGISTRY>${docker.test-registry}</DOCKER_TEST_REGISTRY>
                                        <DOCKER_TEST_TAG>${docker.test-tag}</DOCKER_TEST_TAG>
                                    </environmentVariables>
                                    <executable>tox</executable>
                                </configuration>
                            </execution>
                        </executions>

                        <configuration>
                            <includeProjectDependencies>true</includeProjectDependencies>
                            <includePluginDependencies>true</includePluginDependencies>
                            <executableDependency>
                                <groupId>io.confluent</groupId>
                                <artifactId>licenses</artifactId>
                            </executableDependency>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>io.confluent</groupId>
                                <artifactId>licenses</artifactId>
                                <version>${licenses.version}</version>
                            </dependency>
                        </dependencies>
                    </plugin>

                    <plugin>
                        <groupId>com.spotify</groupId>
                        <artifactId>dockerfile-maven-plugin</artifactId>
                        <version>${dockerfile-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>package</id>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <buildArgs>
                                        <ARTIFACT_ID>${project.artifactId}</ARTIFACT_ID>
                                        <BUILD_NUMBER>${BUILD_NUMBER}</BUILD_NUMBER>
                                        <DOCKER_REGISTRY>${docker.registry}</DOCKER_REGISTRY>
                                        <DOCKER_TAG>${docker.tag}</DOCKER_TAG>
                                        <DOCKER_UPSTREAM_REGISTRY>${docker.upstream-registry}</DOCKER_UPSTREAM_REGISTRY>
                                        <DOCKER_UPSTREAM_TAG>${docker.upstream-tag}</DOCKER_UPSTREAM_TAG>
                                        <GIT_COMMIT>${GIT_COMMIT}</GIT_COMMIT>
                                        <PROJECT_VERSION>${project.version}</PROJECT_VERSION>
                                    </buildArgs>
                                    <skip>${docker.skip-build}</skip>
                                    <tag>${docker.tag}</tag>
                                    <repository>${docker.registry}confluentinc/${project.artifactId}</repository>
                                    <pullNewerImage>false</pullNewerImage>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
