<?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</groupId>
        <artifactId>logging</artifactId>
        <version>1.11.9</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>org.ops4j.pax.logging</groupId>
    <artifactId>pax-logging-service</artifactId>
    <packaging>bundle</packaging>

    <name>OPS4J Pax Logging - Log4Jv1 implementation</name>

    <description>
        Pax Logging backend implementation based on Apache Log4J. It provides Log4J specific implementation
        of PaxLoggingService interface and Log4J specific configuration methods.
        Users may customize Log4J behaviour (appenders, layouts) by creating fragment attached to this bundle.
    </description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <!--
                            Unpack original log4j:log4j - we'll pick only some of the packages later
                        -->
                        <id>unpack-log4j</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>log4j</groupId>
                                    <artifactId>log4j</artifactId>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/unpack-deps/log4j</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <!--
                            Unpack pax-logging-api too - to not rely on possibly random order of pax-logging-api and
                            log4j on maven classpath
                        -->
                        <id>unpack-pax-logging-api</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.ops4j.pax.logging</groupId>
                                    <artifactId>pax-logging-api</artifactId>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/unpack-deps/pax-logging-api</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!--
                    Selective copy of log4j:log4j packages that are also Export-Packaged
                    from pax-logging-api. Packages that are not used (exported) by pax-logging-api
                    will be included anyway using Private-Package (see osgi.bnd).
                    If there's a need for pax-logging-service to alter original classes from log4j:log4j,
                    the sources have to be copied (and modified) directly to src/main/java.

                    Be careful here and remember about "-split-package:=merge-first"
                    or "-split-package:=first"!

                    Some packages *have to* contain different classes both in pax-logging-api and
                    pax-logging-service, because in pax-logging-api they're acting like API for log4j and in
                    pax-logging-service they're actual implementation.

                    Because it's not possible in OSGi to have some classes from one package imported from
                    one bundle while some classes of the same package being used from own bundle, We
                    we have to explicitly decide which packages are imported from pax-logging-api and which
                    we want private-packaged in pax-logging-service.

                    Also, because some classes which are exported from pax-logging-api and private packaged
                    from original log4j:log4j have to be changed in pax-logging-service (some PAXLOGGING
                    fixes related to synchronization), we actually can't rely on the order of Private-Packaging
                    with usual "-split-package:=merge-first", because we can't tell which dependency
                    (pax-logging-api or log4j:log4j) will be "first"! All we know is that maven-bundle-plugin
                    ensures that "currentProject.getBuild().getOutputDirectory()" is the first location, so
                    that's the reason why we're directly and explicitly unpacking some packages directly to
                    ${project.build.outputDirectory}.
                    We can then use "-split-package:=first".
                    This strategy ensures that even if pax-logging-api has some additional class in one of
                    the unpacked packages, this class won't be taken by normal Private-Package mechanism.

                    Another thing - if pax-logging-service needs some changes in one of the classes
                    from the package which is also exported by pax-logging-api, it's enough to put this
                    class (and change) to src/main/java. Running maven-resources-plugin:copy-resources
                    goal in "process-resources" phase is done before compilation phase, so changed
                    (in pax-logging-service bundle) classes take precendence over those unpacked
                    from log4j:log4j.

                    What's even more confusing is that some classes that are exported from pax-logging-api
                    and are different than in log4j should be used in pax-logging-service without importing,
                    because pax-logging-service adds own classes to such package.
                    This example is "org.apache.log4j.helpers" package, where "AppenderAttachableImpl" is
                    fixed for PAXLOGGING-182, and also pax-logging-api reimplements "LogLog" class. In
                    this case we have to ensure that "org.apache.log4j.helpers" is Private-Packaged from
                    pax-logging-api with pax-logging-service adjustments to some classes.
                -->
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-selected-packages</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/unpack-deps/log4j</directory>
                                    <includes>
                                        <include>org/apache/log4j/*.class</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>${project.build.directory}/unpack-deps/pax-logging-api</directory>
                                    <includes>
                                        <include>org/apache/log4j/helpers/*.class</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>${project.build.directory}/unpack-deps/log4j/org/apache/log4j/xml</directory>
                                    <includes>
                                        <include>log4j.dtd</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <!-- Logging -->

        <dependency>
            <groupId>org.knopflerfish.kf6</groupId>
            <artifactId>log-API</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.ops4j.pax.logging</groupId>
            <artifactId>pax-logging-api</artifactId>
        </dependency>

        <!-- OSGi -->

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.cmpn</artifactId>
        </dependency>

        <!-- Testing -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>
