<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.bamboo.distribution</groupId>
        <artifactId>bamboo-distribution</artifactId>
        <version>6.7.0-m405</version>
    </parent>

    <artifactId>bamboo-tomcat-distribution</artifactId>
    <packaging>pom</packaging>

    <name>Atlassian Bamboo Server Tomcat Distribution</name>

    <dependencies>
        <dependency>
            <groupId>com.atlassian.bamboo.distribution</groupId>
            <artifactId>bamboo-common-distribution</artifactId>
            <version>${project.version}</version>
        </dependency>
       <dependency>
            <groupId>com.atlassian.bamboo.distribution</groupId>
            <artifactId>bamboo-jars-tomcat-distribution</artifactId>
            <version>${project.version}</version>
            <classifier>tomcat-additional-jars</classifier>
            <type>zip</type>
            <scope>provided</scope>
        </dependency>

      <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>apache-maven</artifactId>
        <version>${maven.version}</version>
        <classifier>bin</classifier>
        <type>tar.gz</type>
        <scope>runtime</scope>
        <exclusions>
          <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
          </exclusion>
        </exclusions>
      </dependency>

      <dependency>
        <groupId>org.jboss.byteman</groupId>
        <artifactId>byteman</artifactId>
        <scope>runtime</scope>
      </dependency>
      <dependency>
        <groupId>org.jboss.byteman</groupId>
        <artifactId>byteman-submit</artifactId>
        <scope>runtime</scope>
      </dependency>

        <dependency>
            <groupId>com.atlassian.bamboo</groupId>
            <artifactId>atlassian-bamboo-web-app</artifactId>
            <type>war</type>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>        
    </dependencies>

    <profiles>
      <profile>
        <id>set-extra-java-properties</id>
        <activation>
          <property>
            <name>installer.extra.java.properties</name>
          </property>
        </activation>
        <build>
          <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <concat destfile="${installed.tomcat.dir}/bin/setenv.bat" append="yes" fixlastline="yes">set JAVA_OPTS=%JAVA_OPTS% ${installer.extra.java.properties}</concat>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
          </plugins>
        </build>
      </profile>
    </profiles>    



    <build>
        <finalName>atlassian-bamboo</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>${tomcat8x.zip.version}</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>${tomcat8x.zip.version}</version>
                                    <type>zip</type>
                                    <classifier>windows-x86</classifier>
                                </artifactItem>									
                            </artifactItems>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-bundled-jdbc-drivers</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <artifactItems>
                              <!-- Postgresql JDBC Driver -->
                              <artifactItem>
                                <groupId>org.postgresql</groupId>
                                <artifactId>postgresql</artifactId>
                                <version>${postgresql.driver.version}</version>
                              </artifactItem>

                              <!-- MS SQL JDBC Driver -->
                              <artifactItem>
                                  <groupId>com.microsoft.sqlserver</groupId>
                                  <artifactId>mssql-jdbc</artifactId>
                                  <version>${sqlServer.driver.version}</version>
                              </artifactItem>

                                <!--Oracle JDBC driver-->
                                <artifactItem>
                                    <groupId>com.oracle.jdbc</groupId>
                                    <artifactId>ojdbc8</artifactId>
                                    <version>${oracle.driver.version}</version>
                                </artifactItem>

                            </artifactItems>
                            <outputDirectory>${installed.tomcat.dir}/lib</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unpack-webapp-dist</id>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <includeArtifactIds>atlassian-bamboo-web-app</includeArtifactIds>
                            <outputDirectory>${installed.tomcat.dir}/atlassian-bamboo</outputDirectory>
                            <!--
                                Tomcat 6 requires that SL4J logging and log4j NOT be in the BAMBOO WEB-INF/lib directory but rather
                                only in <tomcat>lib so they are copied there earlier and removed from WEB-INF/lib here
                                We can't do exclusions at the webapp level because we need the IDE to pick up the required
                                dependency on log4j

                                The jta implementation libraries are also excluded, even though we have a runtime dependency on them.
                                Instead they live in <tomcat>lib
                            -->
                            <!--
                                Because of NPE in application unloading Bamboo modifies

                            <excludes>META-INF/*, WEB-INF/lib/*slf4j*.jar, WEB-INF/lib/commons-logging*.jar, WEB-INF/classes/hash-registry.properties</excludes>
                            -->
                            <excludes>META-INF/*</excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>modz-detector-maven-plugin</artifactId>
                <version>0.8</version>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>generate-registry</goal>
                    </goals>
                    <configuration>
                      <registry>${installed.tomcat.dir}/atlassian-bamboo/WEB-INF/classes/hash-registry.properties</registry>
                      <filesystem>
                        <directory>${installed.tomcat.dir}/atlassian-bamboo</directory>
                        <excludes>WEB-INF/classes/**/*, images/raw/**/*</excludes>
                      </filesystem>
                      <classpath>
                          <directory>${installed.tomcat.dir}/atlassian-bamboo/WEB-INF/classes</directory>
                          <includes>**/*</includes>
                      </classpath>
                    </configuration>
                  </execution>
                </executions>
            </plugin>            
            
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>modify-tomcat-files</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <move file="${project.build.directory}/tomcat-x64/apache-tomcat-${tomcat8x.version}" toFile="${project.build.directory}/tomcat-x64/apache-tomcat"/>
                    <move file="${project.build.directory}/apache-tomcat-${tomcat8x.version}" toFile="${project.build.directory}/apache-tomcat"/>
                    <replaceregexp file="${installed.tomcat.dir}/bin/service.bat" match="--JvmMs \d+" replace="--JvmMs 512" />
                    <replaceregexp file="${installed.tomcat.dir}/bin/service.bat" match="--JvmMx \d+" replace="--JvmMx 1024 --Startup auto" />
                    <replaceregexp file="${installed.tomcat.dir}/bin/service.bat" match="SERVICE_NAME=Tomcat\d+" replace="SERVICE_NAME=BAMBOO" />
                    <replaceregexp file="${installed.tomcat.dir}/bin/service.bat" match="Apache Tomcat [\d.]+" replace="Atlassian Bamboo" flags="g"/>
                    <replace file="${installed.tomcat.dir}/bin/service.bat" token="http://tomcat.apache.org" value="http://localhost:8085" />
                    <replace file="${installed.tomcat.dir}/bin/service.bat" token="++JvmOptions &quot;-Djava.io.tmpdir=%CATALINA_BASE%\temp" value="++JvmOptions &quot;-Djava.io.tmpdir=%CATALINA_BASE%\temp" />
                    <copy file="${project.build.directory}/tomcat-x64/apache-tomcat/bin/tcnative-1.dll" tofile="${installed.tomcat.dir}/bin/tcnative-1.dll.x64" />
                    <copy file="${project.build.directory}/tomcat-x64/apache-tomcat/bin/tomcat8.exe" tofile="${installed.tomcat.dir}/bin/tomcat8.exe.x64" />
                    <concat destfile="${installed.tomcat.dir}/conf/logging.properties" append="yes" fixlastline="yes">
                      # Suppress clearReferencesThreads and clearThreadLocalMap notifications on shutdown
                      org.apache.catalina.loader.WebappClassLoader.level = OFF

                      # Supress some useless REST messages. See JRADEV-12212.
                      com.sun.jersey.server.impl.application.WebApplicationImpl.level = WARNING
                      com.atlassian.plugins.rest.doclet.level = WARNING
                      com.sun.jersey.api.wadl.config.WadlGeneratorLoader.level = WARNING
                    </concat>
                  </target>
                </configuration>
              </execution>
              <execution>
                <id>run-install4j</id>
                <phase>install</phase>
                <configuration>
                  <target>
                    <taskdef name="install4j" classname="com.install4j.Install4JTask" classpath="${install4j.home}/bin/ant.jar"/>
                    <install4j projectfile="src/main/install4j/bamboo.install4j" verbose="true" destination="target/install4j" release="${project.version}" mediatypes="${bamboo.install4j.mediaTypes}"/>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

          <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <executions>
                  <execution>
                      <id>prepare-distro-resources</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                      <configuration>
                          <descriptors>
                              <descriptor>src/main/assembly/standalone.xml</descriptor>
                              <descriptor>src/main/assembly/additional-distro-resources.xml</descriptor>
                          </descriptors>
                      </configuration>
                  </execution>
              </executions>
          </plugin>
        </plugins>

        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                <finalName>${project.build.finalName}-${project.version}</finalName>
                <tarLongFileMode>gnu</tarLongFileMode>
                <archive>
                  <compress>true</compress>
                </archive>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
    </build>


    <properties>
        <TOMCAT_VERSION>${tomcat8x.zip.version}</TOMCAT_VERSION>
        <installed.tomcat.dir>${project.build.directory}/apache-tomcat</installed.tomcat.dir>
        <VERSION>${project.version}</VERSION>
        <BAMBOOVERSION>${project.version}</BAMBOOVERSION>
        <BUILD_NUMBER>${buildNumber}</BUILD_NUMBER>
        <BUILD_DATE>${maven.build.timestamp}</BUILD_DATE>
        <DOCS_VERSION>${project.version}</DOCS_VERSION>
        <bamboo.install4j.mediaTypes>unixArchive,windowsArchive,windows</bamboo.install4j.mediaTypes>
        <maven.version>3.5.0</maven.version>
    </properties>
</project>
