<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.elastisys.scale</groupId>
    <artifactId>cloudpool.root</artifactId>
    <version>4.1.3</version>
  </parent>
  <artifactId>cloudpool.openstack</artifactId>
  <packaging>jar</packaging>
  <name>elastisys:scale :: cloudpool :: openstack</name>
  <description>
    A cloud pool capable of managing a pool of OpenStack servers.
    This build module produces an all-in-one "server and application" executable
    jar file. When executed, it starts an embedded web server that publishes 
    the cloud pool REST API endpoint.
  </description>

  <properties>
    <!-- Default version to use when tagging docker image. -->
    <docker.image.version>${project.version}</docker.image.version>  
  </properties>

  <dependencies>
    <!-- Openstack API access -->
    <dependency>
      <groupId>org.pacesys</groupId>
      <artifactId>openstack4j-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.pacesys.openstack4j.connectors</groupId>
      <artifactId>openstack4j-jersey2</artifactId>
    </dependency>

    <!-- Common cloud pool API/server classes -->
    <dependency>
      <groupId>com.elastisys.scale</groupId>
      <artifactId>cloudpool.api</artifactId>
      <version>${project.version}</version>
    </dependency>
    <!-- Common cloud pool functionality (such as TerminationQueue and ResizePlanner) -->
    <dependency>
      <groupId>com.elastisys.scale</groupId>
      <artifactId>cloudpool.commons</artifactId>
      <version>${project.version}</version>
    </dependency>    
    

    <!-- UTC Time -->
    <dependency>
      <groupId>com.elastisys.scale</groupId>
      <artifactId>commons.util</artifactId>
    </dependency>

    <!-- Logging -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>    
      <!-- Dockerfile needs placeholder substitution for project version 
        to include the (version-qualified) server jar. -->
      <resource>
        <directory>src/main/docker</directory>
        <targetPath>${project.build.directory}</targetPath>
        <filtering>true</filtering>
        <includes>
          <include>Dockerfile</include>
        </includes>        
      </resource>
      <resource>
        <directory>src/main/docker</directory>
        <targetPath>${project.build.directory}</targetPath>
        <filtering>false</filtering>       
        <excludes>
          <exclude>Dockerfile</exclude>
        </excludes>        
      </resource>
    </resources>
  
    <plugins>
      <!-- Build a standalone executable jar file that embeds all classpath 
        dependencies. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <transformers>
            <!-- Make jar file executable (add Main-Class header to jar manifest) -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.elastisys.scale.cloudpool.openstack.server.Main</mainClass>
            </transformer>
          </transformers>
          <!-- Make shaded jar file the main artifact output by the build. -->
          <shadedArtifactAttached>false</shadedArtifactAttached>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- Runs docker build to create a docker image for the server.
           Execute from command-line as follows:
           
             mvn exec:exec -Dexec.executable=docker [-Ddocker.image.version=<version>]
           
           NOTE: to be able to use docker without sudo/root privileges, you need
           to add yourself to the "docker" group. See:
           https://docs.docker.com/installation/ubuntulinux/#giving-non-root-access       
        -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
          <execution>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <executable>docker</executable>
          <workingDirectory>${project.build.directory}</workingDirectory>
          <arguments>
            <argument>build</argument>
            <argument>--tag</argument>
            <argument>elastisys/openstackpool:${docker.image.version}</argument>
            <argument>.</argument>
          </arguments>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>
