<?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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.ops4j.pax.logging</groupId>
        <artifactId>pax-logging-it-karaf</artifactId>
        <version>2.0.17</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>org.ops4j.pax.logging.karaf</groupId>
    <artifactId>karaf-base</artifactId>
<!--    <packaging>kar</packaging>-->
    <packaging>pom</packaging>

    <name>OPS4J Pax Logging - Karaf KAR</name>

    <description>
        In Apache Karaf there's org.apache.karaf.features:base and org.apache.karaf.features:framework
        Here both are combined in single "framework KAR", which contains all fundamental resources.
        These resources include:
        - bin scripts
        - libs (ext, boot, endorsed)
        - basic etc config files
        - fundamental system libraries (e.g., Felix framework)
    </description>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/filtered-resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/appended-resources</directory>
                <filtering>true</filtering>
                <targetPath>${project.build.directory}/appended-resources</targetPath>
            </resource>
            <resource>
                <directory>src/main/feature</directory>
                <filtering>true</filtering>
                <!-- convention for org.apache.karaf.tooling.KarMojo#featuresFile -->
                <targetPath>${project.build.directory}/feature</targetPath>
            </resource>
        </resources>

        <plugins>

            <!-- Core plugins -->

            <plugin>
                <!-- required, because we have "pom" packaging -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <!--
                            selective copy of karaf base resources (remember - maven-dependencies-plugin
                            is invoked earlier - in generate-resources phase
                        -->
                        <id>copy-karaf-base-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/karaf-base/resources</directory>
                                    <includes>
                                        <include>bin/*</include>
                                        <include>data/**</include>
                                        <include>etc/config.properties</include>
                                        <include>etc/jre.properties</include>
                                        <include>etc/system.properties</include>
                                        <include>lib/boot/*</include>
                                        <include>lib/endorsed/*locator*</include>
                                        <include>lib/jdk9plus/*.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <!--
                            Normal processing according to <build>/<resources> configuration. Any resources
                            copied previously from unpacked org.apache.karaf.features:base will be overwritten
                        -->
                        <id>default</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                        <configuration>
                            <useDefaultDelimiters>false</useDefaultDelimiters>
                            <delimiters>
                                <delimiter>${*}</delimiter>
                            </delimiters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Tools -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- final touches -->
                        <id>customize-karaf-base</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="Customizing etc configuration files" />
                                <concat destfile="${project.build.outputDirectory}/resources/etc/config.properties" append="true" encoding="UTF-8">
                                    <filelist dir="${project.build.directory}/appended-resources" files="append-header.part" />
                                </concat>
                                <concat destfile="${project.build.outputDirectory}/resources/etc/config.properties" append="true" encoding="UTF-8">
                                    <filelist dir="${project.build.directory}/appended-resources/etc" files="config.properties" />
                                    <filterchain>
                                        <headfilter lines="-1" skip="18" />
                                    </filterchain>
                                </concat>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <!--
                           unpack original org.apache.karaf.features:base and do not alter anything
                           we'll pick required resources later
                        -->
                        <id>unpack-karaf-base</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.karaf.features</groupId>
                                    <artifactId>base</artifactId>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/karaf-base</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Karaf plugins -->

            <plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>karaf-maven-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>package</id>
                        <goals>
                            <goal>kar</goal>
                        </goals>
                        <configuration>
                            <featuresFile>${project.build.directory}/feature/feature.xml</featuresFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- MojoHaus plugins -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>target/feature/feature.xml</file>
                                    <classifier>features</classifier>
                                    <type>xml</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <dependencies>

        <!-- pax-logging own artifacts -->

        <dependency>
            <groupId>org.ops4j.pax.logging.karaf</groupId>
            <artifactId>karaf-base-logger</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Karaf -->

        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>base</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>
