<?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>com.atlassian.bitbucket.mesh</groupId>
        <artifactId>mesh-distribution-parent</artifactId>
        <version>1.4.0-alpha-9dfe04095b</version>
    </parent>

    <artifactId>mesh-distribution-docker</artifactId>
    <packaging>pom</packaging>

    <name>Bitbucket Mesh - Docker Image</name>

    <properties>
        <docker.image.name>docker.atl-paas.net/bitbucket-mesh</docker.image.name>
        <docker.image.tag>${project.version}</docker.image.tag>
        <docker.path.home>/var/atlassian/application-data/bitbucket-mesh</docker.path.home>
        <docker.path.install>/opt/atlassian/bitbucket-mesh</docker.path.install>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.atlassian.bitbucket.mesh</groupId>
            <artifactId>mesh-app</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <executions>
                    <!-- There's an interaction issue between the Assembly and Enforcer plugins when trying to
                         use dependencySets in the Docker plugin which breaks the build. The workaround is to
                         disable the Enforcer execution. -->
                    <!-- See https://github.com/fabric8io/docker-maven-plugin/issues/1146 -->
                    <execution>
                        <id>ban-undesired-dependencies</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>enforce-build-environment</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                    <execution>
                        <id>push</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <images>
                        <image>
                            <name>${docker.image.name}:${docker.image.tag}</name>
                            <build>
                                <from>ubuntu:22.04</from>
                                <tags>
                                    <tag>${project.version}</tag>
                                </tags>
                                <volumes>
                                    <volume>${docker.path.home}</volume>
                                </volumes>
                                <runCmds>
                                    <run>apt-get update</run>
                                    <run>apt-get upgrade -y</run>
                                    <run>apt-get install -y --no-install-recommends openjdk-8-jre-headless git tini</run>
                                    <run>apt-get clean autoclean</run>
                                    <run>apt-get autoremove -y</run>
                                    <run>rm -rf /var/lib/apt/lists/*</run>
                                    <run>mkdir -p ${docker.path.home} ${docker.path.install}</run>
                                </runCmds>

                                <env>
                                    <LANG>en_US.UTF-8</LANG>
                                    <MESH_HOME>${docker.path.home}</MESH_HOME>
                                    <MESH_INSTALL_DIR>${docker.path.install}</MESH_INSTALL_DIR>
                                </env>
                                <ports>
                                    <port>7777/tcp</port>
                                </ports>

                                <assemblies>
                                    <assembly>
                                        <name>home</name>
                                        <descriptor>home.xml</descriptor>
                                        <mode>dir</mode>
                                        <targetDir>${docker.path.home}</targetDir>
                                    </assembly>
                                    <assembly>
                                        <name>install</name>
                                        <descriptor>install.xml</descriptor>
                                        <mode>dir</mode>
                                        <targetDir>${docker.path.install}</targetDir>
                                    </assembly>
                                </assemblies>

                                <entryPoint>
                                    <exec>
                                        <arg>tini</arg>
                                        <arg>--</arg>
                                    </exec>
                                </entryPoint>
                                <cmd>
                                    <exec>
                                        <arg>java</arg>
                                        <arg>-Dfile.encoding=UTF-8</arg>
                                        <arg>-Dmesh.home=${docker.path.home}</arg>
                                        <arg>-Dsun.jnu.encoding=UTF-8</arg>
                                        <arg>-jar</arg>
                                        <arg>${docker.path.install}/mesh-app.jar</arg>
                                    </exec>
                                </cmd>
                                <workdir>${docker.path.home}</workdir>

                                <cleanup>true</cleanup>
                                <optimise>true</optimise>
                            </build>
                        </image>
                    </images>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
