<?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>cn.home1</groupId>
        <artifactId>maven-build</artifactId>
        <version>0.3.7</version>
    </parent>

    <artifactId>build-docker</artifactId>
    <description>Parent pom for docker projects</description>
    <name>build-docker</name>
    <packaging>pom</packaging>

    <properties>
        <io.fabric8_docker-maven-plugin.version>0.26.1</io.fabric8_docker-maven-plugin.version>
        <com.spotify_docker-maven-plugin.version>1.1.1</com.spotify_docker-maven-plugin.version>
        <com.spotify_dockerfile-maven-plugin.version>1.4.4</com.spotify_dockerfile-maven-plugin.version>

        <dockerfile.build.skip>true</dockerfile.build.skip>
        <dockerfile.useMavenSettingsForAuth>false</dockerfile.useMavenSettingsForAuth>
    </properties>

    <build>
        <extensions>
            <extension>
                <!-- for the dependency type 'docker-info' to be supported -->
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-extension</artifactId>
                <version>${com.spotify_dockerfile-maven-plugin.version}</version>
            </extension>
        </extensions>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>dockerfile-maven-plugin</artifactId>
                    <version>${com.spotify_dockerfile-maven-plugin.version}</version>
                    <configuration>
                        <buildArgs>
                            <ARTIFACT_REPOSITORY>${private-nexus3.repository}</ARTIFACT_REPOSITORY>
                            <BUILD_TIMESTAMP>${maven.build.timestamp}</BUILD_TIMESTAMP>
                            <DOCKER_REGISTRY>${docker.registry}</DOCKER_REGISTRY>
                            <GIT_BRANCH>${git.branch}</GIT_BRANCH>
                            <GIT_COMMIT_ID>${git.commit.id}</GIT_COMMIT_ID>
                            <GIT_COMMIT_ID_ABBREV>${git.commit.id.abbrev}</GIT_COMMIT_ID_ABBREV>
                            <GIT_COMMIT_TIME>${git.commit.time}</GIT_COMMIT_TIME>
                            <GIT_REMOTE_ORIGIN_URL>${git.remote.origin.url}</GIT_REMOTE_ORIGIN_URL>
                            <JAR_FILE>${project.build.finalName}-exec.jar</JAR_FILE>
                            <PROJECT_ARTIFACTID>${project.artifactId}</PROJECT_ARTIFACTID>
                            <PROJECT_GROUPID>${project.groupId}</PROJECT_GROUPID>
                            <PROJECT_VERSION>${project.version}</PROJECT_VERSION>
                        </buildArgs>
                        <tag>${project.version}</tag>
                        <useMavenSettingsForAuth>${dockerfile.useMavenSettingsForAuth}</useMavenSettingsForAuth>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${com.spotify_docker-maven-plugin.version}</version>
                    <configuration>
                        <imageTags>
                            <imageTag>${project.version}</imageTag>
                            <!--<imageTag>latest</imageTag>-->
                        </imageTags>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                                <include>${project.build.finalName}-exec.jar</include>
                            </resource>
                        </resources>
                        <dockerDirectory>${project.basedir}</dockerDirectory>
                        <noCache>true</noCache>
                    </configuration>
                    <executions>
                        <execution>
                            <id>remove-image</id>
                            <phase>clean</phase>
                            <goals>
                                <goal>removeImage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${io.fabric8_docker-maven-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>auto-clean-dockerfile</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <delete failonerror="false">
                                    <fileset dir="${project.basedir}" includes="Dockerfile"/>
                                </delete>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>filtering-dockerfile</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.basedir}</outputDirectory>
                            <delimiters>
                                <!--<delimiter>${*}</delimiter>-->
                                <delimiter>@</delimiter>
                            </delimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/docker</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- @formatter:off -->
        <!-- value of ${docker.image.prefix} should ends with a '/' -->
        <profile>
            <id>default-docker-image-prefix</id>
            <activation><property><name>!docker.image.prefix</name></property><activeByDefault>false</activeByDefault></activation>
            <properties><docker.image.prefix></docker.image.prefix></properties>
        </profile>
        <profile>
            <id>ues-private-docker-registry-no-docker-image-prefix</id>
            <activation>
                <property><name>docker.registry</name></property>
                <file><exists>${basedir}/src/main/resources/docker/Dockerfile</exists></file>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>dockerfile-maven-plugin</artifactId>
                            <configuration>
                                <repository>${docker.registry}/${docker.image.prefix}${project.artifactId}</repository>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>docker-maven-plugin</artifactId>
                            <configuration>
                                <imageName>${docker.registry}/${docker.image.prefix}${project.artifactId}</imageName>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
        <profile>
            <id>use-docker-hub-docker-image-prefix-no-docker-registry</id>
            <activation>
                <property><name>!docker.registry</name></property>
                <file><exists>${basedir}/src/main/resources/docker/Dockerfile</exists></file>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>dockerfile-maven-plugin</artifactId>
                            <configuration>
                                <repository>${docker.image.prefix}${project.artifactId}</repository>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>docker-maven-plugin</artifactId>
                            <configuration>
                                <imageName>${docker.image.prefix}${project.artifactId}</imageName>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

        <profile>
            <id>docker-maven-plugin-lifecycle-binding-when-not-publish-deploy-segregation</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>!mvn_deploy_publish_segregation</name>
                </property>
            </activation>
            <properties>
                <dockerfile.build.skip>false</dockerfile.build.skip>
            </properties>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>dockerfile-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>default</id>
                                    <goals>
                                        <goal>build</goal>
                                        <goal>tag</goal>
                                        <goal>push</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <groupId>com.spotify</groupId>
                            <artifactId>docker-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>build-image</id>
                                    <phase>package</phase>
                                    <goals><goal>build</goal></goals>
                                </execution>
                                <execution>
                                    <id>push-image</id>
                                    <phase>deploy</phase>
                                    <goals><goal>push</goal></goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
        <!-- @formatter:on -->

        <!-- @formatter:off -->
        <profile>
            <!-- see: http://petr.kozelka.net/blog/2009/10/22/how-to-build-a-zip-output-artifact-in-maven/ -->
            <id>deploy-config-package</id>
            <activation>
                <file><exists>${basedir}/src/main/resources/docker/Dockerfile</exists></file>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>deploy-config-package</id>
                                <phase>prepare-package</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <target>
                                        <replace file="docker-compose-base.yml" token="@artifact.repository@" value="${private-nexus3.repository}"/>
                                        <replace file="docker-compose-base.yml" token="@build.timestamp@" value="${maven.build.timestamp}"/>
                                        <replace file="docker-compose-base.yml" token="@docker.registry@" value="${docker.registry}"/>
                                        <replace file="docker-compose-base.yml" token="@git.branch@" value="${git.branch}"/>
                                        <replace file="docker-compose-base.yml" token="@git.commit.id@" value="${git.commit.id}"/>
                                        <replace file="docker-compose-base.yml" token="@git.commit.id.abbrev@" value="${git.commit.id.abbrev}"/>
                                        <replace file="docker-compose-base.yml" token="@git.commit.time@" value="${git.commit.time}"/>
                                        <replace file="docker-compose-base.yml" token="@git.remote.origin.url@" value="${git.remote.origin.url}"/>
                                        <replace file="docker-compose-base.yml" token="@project.artifactId@" value="${project.artifactId}"/>
                                        <replace file="docker-compose-base.yml" token="@project.groupId@" value="${project.groupId}"/>
                                        <replace file="docker-compose-base.yml" token="@project.version@" value="${project.version}"/>
                                        <zip destfile="${project.build.directory}/${project.artifactId}-${project.version}-deploy.zip">
                                            <fileset dir="."
                                                     defaultexcludes="no"
                                                     includes="data/**/.gitignore,data/**/permissions,${project.artifactId}.properties,instance.properties"
                                                     excludes="**/*.log,**/*.tmp,**/*.bak"
                                            />
                                            <fileset dir="." includes="docker-compose*.yml"/>
                                        </zip>
                                        <copy file="${project.build.directory}/${project.artifactId}-${project.version}-deploy.zip"
                                              tofile="${project.basedir}/src/main/docker/deploy.zip"
                                              overwrite="true"
                                        />
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-deploy-config-package</id>
                                <phase>prepare-package</phase>
                                <goals><goal>attach-artifact</goal></goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <classifier>deploy</classifier>
                                            <file>${project.build.directory}/${project.artifactId}-${project.version}-deploy.zip</file>
                                            <type>zip</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!--
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-deploy-config-package</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.basedir}/src/main/docker</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>${project.build.directory}</directory>
                                            <includes>
                                                <include>${project.artifactId}-${project.version}-deploy.zip</include>
                                            </includes>
                                            <filtering>false</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    -->
                    <!-- maven-jar-plugin can not copy .* hidden files -->
                    <!--
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>deploy-config-package</id>
                                <phase>package</phase>
                                <configuration>
                                    <classifier>deploy</classifier>
                                    <classesDirectory>${project.basedir}</classesDirectory>
                                    <includes>
                                        <include>/docker-compose*.yml</include>
                                        <include>/data/**/*</include>
                                    </includes>
                                    <excludes>
                                        <exclude>**/*.log</exclude>
                                        <exclude>**/*.tmp</exclude>
                                        <exclude>**/*.bak</exclude>
                                    </excludes>
                                </configuration>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    -->
                </plugins>
            </build>
        </profile>
        <!-- @formatter:on -->
    </profiles>
</project>
