<?xml version="1.0" encoding="UTF-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public 
	License, v. 2.0. If a copy of the MPL - was not distributed with this file, 
	You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!-- Portions created by ART Group, University of Rome Tor Vergata are Copyright 
	(C) 2014 -->
<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>it.uniroma2.art.maple</groupId>
		<artifactId>maple</artifactId>
		<version>0.0.10</version>
		<relativePath>..</relativePath>
	</parent>
	<artifactId>maple-karaf-distribution</artifactId>
	<name>MAPLE Karaf Custom Distribution</name>

	<packaging>pom</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.karaf</groupId>
			<artifactId>apache-karaf</artifactId>
			<version>${karaf.version}</version>
			<type>zip</type>
		</dependency>
	</dependencies>

	<build>
		<!-- Enables filtered resources (1 of 2) -->
		<resources>
			<resource>
				<directory>src/main/filtered-resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
		</resources>

		<plugins>
			<!-- Enables filtered resources (2 of 2) -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>filter</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>resources</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.3.1</version>
				<executions>
					<execution>
						<id>force-install-configuration</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>install-file</goal>
						</goals>
						<configuration>
							<file>${project.build.directory}/classes/etc/${project.groupId}.cfg</file>
							<groupId>${project.groupId}</groupId>
							<artifactId>${project.artifactId}</artifactId>
							<version>${project.version}</version>
							<packaging>cfg</packaging>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.karaf.tooling</groupId>
				<artifactId>karaf-maven-plugin</artifactId>
				<version>${karaf.version}</version>
				<executions>
					<execution>
						<id>add-features-to-repo</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>features-add-to-repository</goal>
						</goals>
						<configuration>
							<descriptors>
								<descriptor>mvn:org.apache.karaf.features/standard/${karaf.version}/xml/features</descriptor>
								<descriptor>mvn:org.apache.karaf.features/enterprise/${karaf.version}/xml/features</descriptor>
								<descriptor>mvn:org.apache.karaf.features/spring/${karaf.version}/xml/features</descriptor>
								<descriptor>file:${project.basedir}/target/classes/features.xml</descriptor>
							</descriptors>
							<features>
								<feature>maple</feature>
							</features>
							<addTransitiveFeatures>true</addTransitiveFeatures>

							<!-- In 2.3.3 this property defaults to ${project.build.directory}/features-repo -->
							<!-- <repository>${project.build.directory}/features-repo</repository> -->
						</configuration>
					</execution>
					<!-- Builds the Karaf Archive (self-contained distribution) -->
					<execution>
						<id>kar</id>
						<goals>
							<goal>kar</goal>
						</goals>
						<configuration>
							<featuresFile>${project.build.directory}/classes/features.xml</featuresFile>
							<featureName>maple</featureName>

							<!-- Indicates a non-existing directory, to avoid that resources in 
								target/classes are automatically copied into the KAR archive -->
							<resourcesDir>${project.basedir}/target/some-fancy-directory</resourcesDir>
						</configuration>
					</execution>

				</executions>
			</plugin>

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>1.9.1</version>
				<executions>
					<execution>
						<id>attach-artifact</id>
						<phase>process-resources</phase>
						<goals>
							<goal>attach-artifact</goal>
						</goals>
						<configuration>
							<artifacts>
								<artifact>
									<file>${project.basedir}/target/classes/features.xml</file>
									<type>xml</type>
									<classifier>features</classifier>
								</artifact>
							</artifacts>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!-- Unpacks the Karaf distribution -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.5</version>
				<executions>
					<execution>
						<id>unpack</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>unpack</goal>
						</goals>
						<configuration>
							<artifactItems>
								<artifactItem>
									<groupId>org.apache.karaf</groupId>
									<artifactId>apache-karaf</artifactId>
									<version>${karaf.version}</version>
									<type>zip</type>
									<outputDirectory>${project.basedir}/target/dependencies</outputDirectory>
								</artifactItem>
							</artifactItems>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!-- Builds the assembly -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>bin</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<finalName>${project.parent.artifactId}-${project.parent.version}</finalName>
							<outputDirectory>
								${project.parent.build.directory}
							</outputDirectory>
							<descriptors>
								<descriptor>src/main/descriptors/bin.xml</descriptor>
							</descriptors>
							<appendAssemblyId>false</appendAssemblyId>
							<tarLongFileMode>gnu</tarLongFileMode>
							<!-- In the descriptor, the base Karaf distribution is copied mostly 
								at the end, so skipping duplicates leaves the copy introduced by the build 
								of ST (see: https://github.com/codehaus-plexus/plexus-archiver/blob/master/src/main/java/org/codehaus/plexus/archiver/Archiver.java#L335) -->
							<archiverConfig>
								<duplicateBehavior>skip</duplicateBehavior>
							</archiverConfig>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.7</version>
				<executions>
					<execution>
						<!-- Copy artifacts to the parent project target folder -->
						<id>copy-artifacts-to-parent</id>
						<phase>package</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<target>
								<copy todir="../target/dist" overwrite="true">
									<fileset dir="${project.basedir}/target">
										<include name="*.zip" />
										<include name="*.kar" />
									</fileset>
									<fileset dir="${project.basedir}/target/classes">
										<include name="features.xml" />
									</fileset>
								</copy>
							</target>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>