<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<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">

  <parent>
    <groupId>org.apache.jspwiki</groupId>
    <artifactId>jspwiki-builder</artifactId>
    <version>2.12.4</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>jspwiki-portable</artifactId>
  <name>Apache JSPWiki portable</name>

  <properties>
    <jspwiki.woas.language>en</jspwiki.woas.language>
    <jspwiki.native-launchers.macos.download>woas:download-appbundler-for-mac</jspwiki.native-launchers.macos.download>
    <jspwiki.native-launchers.macos.generate>woas:mac-app-oracle-jdk</jspwiki.native-launchers.macos.generate>
    <jspwiki.native-launchers.windows.download>woas:download-launch4j-for-mac</jspwiki.native-launchers.windows.download>
    <jspwiki.native-launchers.windows.generate>woas:create-windows-app</jspwiki.native-launchers.windows.generate>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <configuration>
          <filesets>
            <fileset>
              <directory>${basedir}/logs</directory>
            </fileset>
          </filesets>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>regex-property</id>
            <goals>
              <goal>regex-property</goal>
            </goals>
            <configuration>
              <name>woas.version</name>
              <regex>-SNAPSHOT</regex>
              <value>${project.version}</value>
              <failIfNoMatch>false</failIfNoMatch>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-tomcat-launcher</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <appendAssemblyId>false</appendAssemblyId>
              <archive>
                <manifest>
                  <mainClass>org.apache.catalina.startup.Bootstrap</mainClass>
                </manifest>
              </archive>
              <descriptors>
                <descriptor>${basedir}/src/main/assembly/tomcat-launcher.xml</descriptor>
              </descriptors>
              <finalName>tomcat-launcher-${tomcat.version}</finalName>
              <outputDirectory>./target/launchers</outputDirectory>
              <!-- 
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters> -->
            </configuration>
          </execution>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <tarLongFileMode>gnu</tarLongFileMode>
              <descriptors>
                <descriptor>${basedir}/src/main/assembly/woas-dist.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>woas:copy-woas</id>
            <phase>process-resources</phase>
            <configuration>
              <target>
                <property name="jspwiki.woas.version" value="${woas.version}" />
                <property name="jspwiki.tomcat.version" value="${tomcat.version}" />
                <ant antfile="${basedir}/build.xml" target="woas:info" />
                <ant antfile="${basedir}/build.xml" target="woas:copy-woas" />
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>          
          <execution>
            <id>woas:copy-tomcat</id>
            <phase>prepare-package</phase>
            <configuration>
              <target>
                <property name="jspwiki.woas.version" value="${woas.version}" />
                <property name="jspwiki.tomcat.version" value="${tomcat.version}" />
                <ant antfile="${basedir}/build.xml" target="woas:copy-tomcat" />
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
          <execution>
            <id>woas:copy-launchers</id>
            <phase>prepare-package</phase>
            <configuration>
              <target>
                <property name="jspwiki.woas.version" value="${woas.version}" />
                <property name="jspwiki.tomcat.version" value="${tomcat.version}" />
                <ant antfile="${basedir}/build.xml" target="woas:info" />
                <ant antfile="${basedir}/build.xml" target="woas:copy-tomcat-launchers" />
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <!-- Unpack the JSP Wiki web archive -->
          <execution>
            <id>unpack-jspwiki-war</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.jspwiki</groupId>
                  <artifactId>jspwiki-war</artifactId>
                  <version>${project.version}</version>
                  <type>war</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}/unpack/jspwiki-war</outputDirectory>
            </configuration>
          </execution>
          <!-- Unpack the JSP Wiki Pages for the "personal" wiki -->
          <execution>
            <id>unpack-wikipages-personal</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.jspwiki.wikipages</groupId>
                  <artifactId>jspwiki-wikipages-${jspwiki.woas.language}</artifactId>
                  <version>${project.version}</version>
                </artifactItem>
              </artifactItems>
              <excludes>META-INF/**</excludes>
              <outputDirectory>${project.build.directory}/woas/data/personal</outputDirectory>
            </configuration>
          </execution>
          <!-- Unpack the JSP Wiki Pages for the "department" wiki -->
          <execution>
            <id>unpack-wikipages-department</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.jspwiki.wikipages</groupId>
                  <artifactId>jspwiki-wikipages-${jspwiki.woas.language}</artifactId>
                  <version>${project.version}</version>
                </artifactItem>
              </artifactItems>
              <excludes>META-INF/**</excludes>
              <outputDirectory>${project.build.directory}/woas/data/department</outputDirectory>
            </configuration>
          </execution>
          <!-- Unpack the tomcat distribution -->
          <execution>
            <id>unpack-tomcat-zip</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat</artifactId>
                  <version>${tomcat.version}</version>
                  <type>zip</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}/unpack/tomcat</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
    <dependency><!-- added so we can issue safe, parallel builds -->
      <groupId>${project.groupId}</groupId>
      <artifactId>jspwiki-war</artifactId>
      <version>${project.version}</version>
      <type>war</type>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>${tomcat.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jsp-api</artifactId>
      <version>${tomcat.version}</version>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>generate-launchers</id>
      <!-- Native launchers generation need a previous build to be run, so the ant script is able to locate all      -->
      <!-- needed files. In order to do that, we launch another module build, as early as possible, and generate the -->
      <!-- launchers over it. That way, the main build will be able to pick up the launchers in one run, without     -->
      <!-- needing to manually issue another build again. As generating the native launchers takes some time, this   -->
      <!-- feature must be explicitly activated.                                                                     -->
      <activation>
        <activeByDefault>false</activeByDefault>
        <property>
          <name>generate-native-launchers</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-invoker-plugin</artifactId>
            <configuration>
              <logDirectory>${project.build.directory}</logDirectory>
              <pom>./pom.xml</pom>
              <properties><build-native-launchers>true</build-native-launchers></properties>
              <streamLogs>true</streamLogs>
            </configuration>
            <executions>
              <execution>
                <id>woas:generate-native-launchers</id>
                <phase>validate</phase>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>build-native-launchers</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <property>
          <name>build-native-launchers</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>woas:build-native-launchers</id>
                <phase>package</phase>
                <configuration>
                  <target>
                    <property name="jspwiki.woas.version" value="${woas.version}" />
                    <property name="jspwiki.tomcat.version" value="${tomcat.version}" />
                    <ant antfile="${basedir}/build.xml" target="woas:info" />
                    <ant antfile="${basedir}/build.xml" target="${jspwiki.native-launchers.macos.download}" />
                    <ant antfile="${basedir}/build.xml" target="${jspwiki.native-launchers.macos.generate}" />
                    <ant antfile="${basedir}/build.xml" target="${jspwiki.native-launchers.windows.download}" />
                    <ant antfile="${basedir}/build.xml" target="${jspwiki.native-launchers.windows.generate}" />
                    <ant antfile="${basedir}/build.xml" target="woas:update-tomcat-launchers" />
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
            <dependencies>
              <dependency><!-- enable support with JDK >= 9, see https://sourceforge.net/p/launch4j/bugs/172/ and https://sourceforge.net/p/launch4j/bugs/206/ -->
                <groupId>com.thoughtworks.xstream</groupId>
                <artifactId>xstream</artifactId>
                <version>${xstream.version}</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>native-launchers-on-windows-build</id>
      <activation>
        <os><family>windows</family></os>
      </activation>
      <properties>
        <jspwiki.native-launchers.macos.download>woas:download-appbundler-for-mac-on-windows</jspwiki.native-launchers.macos.download>
        <jspwiki.native-launchers.macos.generate>woas:create-mac-app</jspwiki.native-launchers.macos.generate>
        <jspwiki.native-launchers.windows.download>woas:download-launch4j-for-win</jspwiki.native-launchers.windows.download>
        <jspwiki.native-launchers.windows.generate>woas:create-windows-app</jspwiki.native-launchers.windows.generate>
      </properties>
    </profile>
  </profiles>
</project>
