<?xml version="1.0" encoding="UTF-8"?>
<!--

    ==========================================================================================
    =                   JAHIA'S DUAL LICENSING - IMPORTANT INFORMATION                       =
    ==========================================================================================

                                    http://www.jahia.com

        Copyright (C) 2002-2025 Jahia Solutions Group SA. All rights reserved.

        THIS FILE IS AVAILABLE UNDER TWO DIFFERENT LICENSES:
        1/Apache2 OR 2/JSEL

        1/ Apache2
        ==================================================================================

        Copyright (C) 2002-2025 Jahia Solutions Group SA. All rights reserved.

        Licensed 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.


        2/ JSEL - Commercial and Supported Versions of the program
        ===================================================================================

        IF YOU DECIDE TO CHOOSE THE JSEL LICENSE, YOU MUST COMPLY WITH THE FOLLOWING TERMS:

        Alternatively, commercial and supported versions of the program - also known as
        Enterprise Distributions - must be used in accordance with the terms and conditions
        contained in a separate written agreement between you and Jahia Solutions Group SA.

        If you are unsure which license is appropriate for your use,
        please contact the sales department at sales@jahia.com.

-->
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.jahia.server</groupId>
        <artifactId>jahia-root</artifactId>
        <version>8.2.2.0</version>
    </parent>
    <groupId>org.jahia.packages</groupId>
    <artifactId>jahia-packages-parent</artifactId>
    <name>Jahia Packages Parent</name>
    <packaging>pom</packaging>
    <description>Parent project to package all Factory components</description>
    <properties>
        <jahia.final.package.name>${project.artifactId}</jahia.final.package.name>
        <jahia.manifest.package.id>${project.artifactId}</jahia.manifest.package.id>
        <jahia.required.version>${project.parent.version}</jahia.required.version>
        <jahia.package.assembly.file>src/main/assembly/package.xml</jahia.package.assembly.file>
        <jahia.package.sources.assembly.file>src/main/assembly/sources.xml</jahia.package.sources.assembly.file>

        <package.karaf.skip>true</package.karaf.skip>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>set-skip</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                if (project.artifactId == 'jahia-packages-parent') {
                                    project.properties['package.skip'] = "true"
                                    project.properties['package.sources.skip'] = "true"
                                    project.properties['package.provisioning.skip'] = "true"
                                }
                            </source>
                        </configuration>
                    </execution>
                    <execution>
                        <id>provisioning-script</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source><![CDATA[
                                if (project.properties['package.skip'] != 'true' || project.properties['package.provisioning.skip'] != 'true') {
                                    def file = new File(project.build.directory, '${project.artifactId}.dependencies');
                                    def conditionsFile = new File(project.basedir, 'src/main/conditions.properties');
                                    def startlevelFile = new File(project.basedir, 'src/main/startlevel.properties');
                                    deps = []
                                    artifactIds = [];
                                    file.eachLine { line ->
                                        if ((matcher = line =~ / +(.+):(.+):(.+):(.+)/)) {
                                            artifactIds.push([matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4)])
                                        }
                                    }

                                    if (project.properties['package.skip'] != 'true') {
                                        project.properties['jahia.manifest.description'] = artifactIds.collect({ f -> f[1] }).join(',')
                                    }
                                    def conditions = [:];
                                    if (conditionsFile.exists()) {
                                        conditionsFile.eachLine { line ->
                                            def key = line.split(":")[0]
                                            conditions[key] = line.substring(key.length() + 1)
                                        }
                                    }
                                    def startlevel = [:];
                                    if (startlevelFile.exists()) {
                                        startlevelFile.eachLine { line ->
                                            def key = line.split(":")[0]
                                            startlevel[key] = line.substring(key.length() + 1)
                                        }
                                    }
                                    if (project.properties['package.provisioning.skip'] != 'true') {
                                        new File(project.build.directory, '${project.artifactId}.provisioning.yaml').withWriter('utf-8') { writer ->
                                            writer.writeLine(" - installOrUpgradeBundle: ")
                                            artifactIds.forEach { f ->
                                                if ("tgz".equals(f[2])) {
                                                    writer.writeLine("   - url: 'npm:mvn:${f[0]}/${f[1]}/${f[3]}/${f[2]}'");
                                                } else {
                                                    writer.writeLine("   - url: 'mvn:${f[0]}/${f[1]}/${f[3]}'");
                                                }
                                                if (conditions[f[1]] != null) {
                                                    writer.writeLine("     if: \"${conditions[f[1]].replace('"', '\\"')}\"")
                                                }
                                                if (startlevel[f[1]] != null) {
                                                    writer.writeLine("     startLevel: ${startlevel[f[1]]}")
                                                }
                                            }
                                            writer.writeLine("   ignoreChecks: true ")
                                        }
                                    }
                                }
                                ]]></source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${dependency.plugin.version}</version>
                <executions>
                    <execution>
                        <id>list-dependencies</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>list</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>true</excludeTransitive>
                            <outputScope>false</outputScope>
                            <outputFile>${project.build.directory}/${project.artifactId}.dependencies
                            </outputFile>
                            <skip>${package.skip}</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>true</excludeTransitive>
                            <skip>${package.skip}</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-dependencies-sources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>org.jahia.modules</includeGroupIds>
                            <outputDirectory>${project.build.directory}/sources</outputDirectory>
                            <classifier>sources</classifier>
                            <skip>${package.sources.skip}</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>attach-provisioning</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${project.build.directory}/${project.artifactId}.provisioning.yaml</file>
                                    <type>yaml</type>
                                    <classifier>provisioning</classifier>
                                </artifact>
                            </artifacts>
                            <skipAttach>${package.provisioning.skip}</skipAttach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${assembly.plugin.version}</version>
                <executions>
                    <execution>
                        <id>packaging-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${jahia.package.assembly.file}</descriptor>
                            </descriptors>
                            <attach>true</attach>
                            <finalName>${jahia.final.package.name}-${project.version}</finalName>
                            <archive>
                                <manifestEntries>
                                    <Jahia-Package-ID>${jahia.manifest.package.id}</Jahia-Package-ID>
                                    <Jahia-Package-Name>${project.name}</Jahia-Package-Name>
                                    <Jahia-Package-Version>${project.version}</Jahia-Package-Version>
                                    <Jahia-Required-Version>${jahia.required.version}</Jahia-Required-Version>
                                    <Jahia-Package-License>${jahia.manifest.license}</Jahia-Package-License>
                                    <Jahia-Package-Description>${jahia.manifest.description}</Jahia-Package-Description>
                                </manifestEntries>
                            </archive>
                            <skipAssembly>${package.skip}</skipAssembly>
                        </configuration>
                    </execution>
                    <execution>
                        <id>packaging-dependencies-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${jahia.package.sources.assembly.file}</descriptor>
                            </descriptors>
                            <attach>true</attach>
                            <finalName>${jahia.final.package.name}-${project.version}</finalName>
                            <appendAssemblyId>true</appendAssemblyId>
                            <archive>
                                <manifestEntries>
                                    <Jahia-Package-ID>${jahia.manifest.package.id}</Jahia-Package-ID>
                                    <Jahia-Package-Name>${project.name}</Jahia-Package-Name>
                                    <Jahia-Package-Version>${project.version}</Jahia-Package-Version>
                                    <Jahia-Required-Version>${project.parent.version}</Jahia-Required-Version>
                                    <Jahia-Package-License>${jahia.manifest.license}</Jahia-Package-License>
                                    <Jahia-Package-Description>${jahia.manifest.description}</Jahia-Package-Description>
                                </manifestEntries>
                            </archive>
                            <skipAssembly>${package.sources.skip}</skipAssembly>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
