<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.atlassian.bitbucket.server</groupId>
        <artifactId>bitbucket-distribution-parent</artifactId>
        <version>4.6.2-rc2</version>
    </parent>

    <artifactId>bitbucket-distribution</artifactId>
    <packaging>pom</packaging>
    <name>Bitbucket Server - Distribution</name>

    <properties>
        <!-- See http://jira.codehaus.org/browse/MRESOURCES-104 (which says it's fixed but actually isn't) -->
        <at.hack>@</at.hack>
        <db.driver.dir>${project.build.directory}/db-drivers</db.driver.dir>
        <installed.tomcat.dir>${project.build.directory}/apache-tomcat-${tomcat.version}</installed.tomcat.dir>
        <bitbucket.ajp.port>8006</bitbucket.ajp.port>
        <bitbucket.context />
        <bitbucket.configfile>scripts.cfg</bitbucket.configfile>
        <bitbucket.http.port>7990</bitbucket.http.port>
        <bitbucket.ssl.port>8443</bitbucket.ssl.port>
        <elasticsearch.dir>${project.build.directory}/elasticsearch</elasticsearch.dir>
        <tomcat.bootstrap.dir>${project.build.directory}/tomcat-bootstrap</tomcat.bootstrap.dir>
        <!-- See the replacements, and their comments, when changing the Tomcat version. Be careful to ensure the
             replacement strings still match in the new version used. -->
        <tomcat.version>${tomcat.libversion}</tomcat.version>
        <tomcat.binversion>${tomcat.libversion}-atlassian-hosted</tomcat.binversion>
    </properties>

    <build>
        <finalName>bitbucket-distribution-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-tomcat-x64</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.tomcat</groupId>
                                    <artifactId>apache-tomcat</artifactId>
                                    <version>${tomcat.binversion}</version>
                                    <type>zip</type>
                                    <classifier>windows-x64</classifier>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/tomcat-x64</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-tomcat</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.tomcat</groupId>
                                    <artifactId>apache-tomcat</artifactId>
                                    <version>${tomcat.binversion}</version>
                                    <type>zip</type>
                                    <classifier>windows-x86</classifier>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-db-drivers</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.microsoft.sqlserver</groupId>
                                    <artifactId>sqljdbc</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.oracle</groupId>
                                    <artifactId>ojdbc7</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>org.postgresql</groupId>
                                    <artifactId>postgresql</artifactId>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${db.driver.dir}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-tomcat-bootstrap</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.atlassian.bitbucket.server</groupId>
                                    <artifactId>bitbucket-tomcat-bootstrap</artifactId>
                                    <type>jar</type>
                                    <outputDirectory>${tomcat.bootstrap.dir}</outputDirectory>
                                    <destFileName>bitbucket-bootstrap.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-db-native</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.microsoft.sqlserver</groupId>
                                    <artifactId>sqljdbc_auth</artifactId>
                                    <classifier>x64</classifier>
                                    <type>dll</type>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.microsoft.sqlserver</groupId>
                                    <artifactId>sqljdbc_auth</artifactId>
                                    <classifier>x86</classifier>
                                    <type>dll</type>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${db.driver.dir}</outputDirectory>
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unpack-webapp-dist</id>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <includeArtifactIds>bitbucket-webapp</includeArtifactIds>
                            <outputDirectory>${installed.tomcat.dir}/atlassian-bitbucket</outputDirectory>
                            <excludes>META-INF/*</excludes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-elasticsearch</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.elasticsearch.distribution.zip</groupId>
                                    <artifactId>elasticsearch</artifactId>
                                    <type>zip</type>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-buckler</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.atlassian.elasticsearch</groupId>
                                    <artifactId>buckler-plugin</artifactId>
                                    <type>zip</type>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${elasticsearch.dir}/plugins/buckler</outputDirectory>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <!-- The following catalina.bat replacements modify the Tomcat catalina.bat script to
                                     allow us to use "exit /b 1" in setenv.bat and prevent Tomcat from starting. This
                                     is necessary to allow us to omit "start /b /wait cmd /c" when start|stop-bitbucket.bat
                                     execute Tomcat scripts without having the entire command prompt close because we
                                     call "exit 1" (without the /b). However, if we use "start /b /wait cmd /c" when we
                                     execute the other scripts, Ctrl+C can't stop Tomcat.

                                     Note: The match tokens may change between Tomcat versions, so if the Tomcat version
                                     is updated these tokens may need to be changed too. -->
                                <replace file="${installed.tomcat.dir}/bin/catalina.bat" token="goto setenvDone" failOnNoReplacements="true">
                                    <replacevalue>if errorlevel 1 goto end
goto setenvDone</replacevalue>
                                </replace>
                                <replace file="${installed.tomcat.dir}/bin/catalina.bat" token=":setenvDone" failOnNoReplacements="true">
                                    <replacevalue>if errorlevel 1 goto end
:setenvDone</replacevalue>
                                </replace>
                                <!-- Patch the various tool classpaths to make com.atlassian.stash.internal.catalina.startup.Bootstrap available -->
                                <replace file="${installed.tomcat.dir}/bin/catalina.bat" token="set &quot;CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar&quot;" value="set &quot;CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bitbucket-bootstrap.jar;%CATALINA_HOME%\bin\bootstrap.jar&quot;" failOnNoReplacements="true" />
                                <replace file="${installed.tomcat.dir}/bin/catalina.sh" token="CLASSPATH=&quot;$CLASSPATH&quot;&quot;$CATALINA_HOME&quot;/bin/bootstrap.jar" value="CLASSPATH=&quot;$CLASSPATH&quot;&quot;$CATALINA_HOME&quot;/bin/bitbucket-bootstrap.jar:&quot;$CATALINA_HOME&quot;/bin/bootstrap.jar" failOnNoReplacements="true" />
                                <replace file="${installed.tomcat.dir}/bin/daemon.sh" token="CLASSPATH=&quot;$CLASSPATH$CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar&quot;" value="CLASSPATH=$CLASSPATH&quot;$CATALINA_HOME/bin/bitbucket-bootstrap.jar:&quot;$CATALINA_HOME&quot;/bin/bootstrap.jar:&quot;$CATALINA_HOME&quot;/bin/commons-daemon.jar" failOnNoReplacements="true" />
                                <replace file="${installed.tomcat.dir}/conf/catalina.properties" token="bootstrap.jar,commons-daemon.jar,tomcat-juli.jar" value="bootstrap.jar,bitbucket-bootstrap.jar,commons-daemon.jar,tomcat-juli.jar" failOnNoReplacements="true" />
                                <!-- Switch out the Tomcat bootstrap class to com.atlassian.stash.internal.catalina.startup.Bootstrap -->
                                <replace file="${installed.tomcat.dir}/bin/catalina.bat" token="org.apache.catalina.startup.Bootstrap" value="com.atlassian.stash.internal.catalina.startup.Bootstrap" failOnNoReplacements="true" />
                                <replace file="${installed.tomcat.dir}/bin/catalina.sh" token="org.apache.catalina.startup.Bootstrap" value="com.atlassian.stash.internal.catalina.startup.Bootstrap" failOnNoReplacements="true" />
                                <replace file="${installed.tomcat.dir}/bin/daemon.sh" token="org.apache.catalina.startup.Bootstrap" value="com.atlassian.stash.internal.catalina.startup.Bootstrap" failOnNoReplacements="true" />
                                <!-- This change makes $STASH_HOME/lib jars available in Tomcat's common classloader,
                                     which allows things like JDBC drivers to be put there, safe across upgrades. -->
                                <replace file="${installed.tomcat.dir}/conf/catalina.properties" token="common.loader=" value="common.loader=$${bitbucket.home}/lib,$${bitbucket.home}/lib/*.jar," failOnNoReplacements="true" />
                                <copy file="${project.build.directory}/tomcat-x64/apache-tomcat-${tomcat.version}/bin/tcnative-1.dll" tofile="${installed.tomcat.dir}/bin/tcnative-1.dll.x64" />
                                <copy file="${project.build.directory}/tomcat-x64/apache-tomcat-${tomcat.version}/bin/tomcat8.exe" tofile="${installed.tomcat.dir}/bin/tomcat8.exe.x64" />
                                <!-- Turn off WebappClassLoader logging, which dumps a stream of (completely useless)
                                     "SEVERE" warnings about ThreadLocals every time the application is stopped -->
                                <concat destfile="${installed.tomcat.dir}/conf/logging.properties" append="true">
org.apache.catalina.loader.WebappClassLoaderBase.level = OFF
org.apache.catalina.loader.WebappClassLoader.level = OFF</concat>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>strip-version-from-elasticsearch-directory</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <target>
                                <path id="elasticsearch_versioned_dir_path">
                                    <dirset dir="${project.build.directory}">
                                        <include name="elasticsearch-*" />
                                    </dirset>
                                </path>
                                <property name="elasticsearch_versioned_dir" refid="elasticsearch_versioned_dir_path" />
                                <move file="${elasticsearch_versioned_dir}" tofile="${elasticsearch.dir}" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>standalone</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/main/assembly/bin.xml</descriptor>
                            </descriptors>
                            <tarLongFileMode>gnu</tarLongFileMode>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <!-- Have to keep this dependency here rather than in parent pom: otherwise it affects
            some of func-tests by pulling a more recent version of com.fasterxml.jackson -->
            <dependency>
                <groupId>com.atlassian.bitbucket.search</groupId>
                <artifactId>bitbucket-search-parent</artifactId>
                <version>${bitbucket.search.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.distribution.zip</groupId>
            <artifactId>elasticsearch</artifactId>
            <type>zip</type>
        </dependency>
        <dependency>
            <groupId>com.atlassian.elasticsearch</groupId>
            <artifactId>buckler-plugin</artifactId>
            <type>zip</type>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-distribution-common</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-distribution-licenses</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-tomcat-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atlassian.bitbucket.server</groupId>
            <artifactId>bitbucket-webapp</artifactId>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>
