<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.openforis.collect</groupId>
		<artifactId>collect-installation</artifactId>
		<version>4.0.49</version>
	</parent>
	<artifactId>collect-installer</artifactId>
	<packaging>pom</packaging>
	<name>Open Foris Collect Installer</name>
	<description>Module to create the installers for Open Foris Collect</description>

	<properties>
		<!-- to avoid replacing relative path to images and other resources used 
			by the installer -->
		<installer-build-path>${project.build.directory}/installer</installer-build-path>
		<installbuilder-file-name>installbuilder.xml</installbuilder-file-name>
		<installbuilder.project.name>OpenForisCollect</installbuilder.project.name>
	</properties>

	<profiles>
		<profile>
			<id>installer</id>
			<build>
				<plugins>
					<!-- 1. copy dependencies -->
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-dependency-plugin</artifactId>
						<executions>
							<execution>
								<id>copy-other-artifacts</id>
								<phase>process-sources</phase>
								<goals>
									<goal>copy</goal>
								</goals>
								<configuration>
									<artifactItems>
										<!-- Collect WebApp -->
										<artifactItem>
											<groupId>org.openforis.collect</groupId>
											<artifactId>collect-webapp</artifactId>
											<version>${project.version}</version>
											<type>war</type>
											<overWrite>false</overWrite>
											<outputDirectory>${installer-build-path}/webapps</outputDirectory>
											<destFileName>collect.war</destFileName>
										</artifactItem>
										<!-- Saiku WebApp -->
										<artifactItem>
											<groupId>meteorite.bi</groupId>
											<artifactId>saiku-webapp-full</artifactId>
											<version>2.6_b</version>
											<type>war</type>
											<overWrite>false</overWrite>
											<outputDirectory>${installer-build-path}/webapps</outputDirectory>
											<destFileName>saiku.war</destFileName>
										</artifactItem>
										<!-- JDBC Libraries -->
										<artifactItem>
											<groupId>com.h2database</groupId>
											<artifactId>h2</artifactId>
											<type>jar</type>
											<overWrite>false</overWrite>
											<outputDirectory>${installer-build-path}/lib</outputDirectory>
										</artifactItem>
										<artifactItem>
											<groupId>org.xerial</groupId>
											<artifactId>sqlite-jdbc</artifactId>
											<type>jar</type>
											<overWrite>false</overWrite>
											<outputDirectory>${installer-build-path}/lib</outputDirectory>
										</artifactItem>
										<artifactItem>
											<groupId>org.postgresql</groupId>
											<artifactId>postgresql</artifactId>
											<type>jar</type>
											<overWrite>false</overWrite>
											<outputDirectory>${installer-build-path}/lib</outputDirectory>
										</artifactItem>
									</artifactItems>
								</configuration>
							</execution>
							<execution>
								<id>copy-control-panel-dependencies</id>
								<phase>process-sources</phase>
								<goals>
									<goal>copy-dependencies</goal>
								</goals>
								<configuration>
									<outputDirectory>${installer-build-path}/lib</outputDirectory>
									<includeScope>compile</includeScope>
								</configuration>
							</execution>
						</executions>
					</plugin>

					<!-- 2. copy resource files -->
					<plugin>
						<artifactId>maven-resources-plugin</artifactId>
						<executions>
							<execution>
								<id>copy-installbuilder-source-file</id>
								<phase>prepare-package</phase>
								<goals>
									<goal>copy-resources</goal>
								</goals>
								<configuration>
									<outputDirectory>${installer-build-path}</outputDirectory>
									<resources>
										<resource>
											<directory>src/main/resources</directory>
											<includes>
												<include>**</include>
											</includes>
										</resource>
									</resources>
									<overwrite>true</overwrite>
								</configuration>
							</execution>
						</executions>
					</plugin>

					<!-- 3. replace application version into the InstallBuilder XML source 
						file replace Java version into the InstallBuilder java.xml file -->
					<plugin>
						<groupId>com.google.code.maven-replacer-plugin</groupId>
						<artifactId>replacer</artifactId>
						<executions>
							<execution>
								<phase>prepare-package</phase>
								<goals>
									<goal>replace</goal>
								</goals>
							</execution>
						</executions>
						<configuration>
							<basedir>${installer-build-path}</basedir>
							<includes>
								<include>${installbuilder-file-name}</include>
								<include>java.xml</include>
							</includes>
							<regex>false</regex>
							<replacements>
								<replacement>
									<token><![CDATA[%JRE_VERSION_FULL%]]></token>
									<value>${installer.jre.version.full}</value>
								</replacement>
								<replacement>
									<token><![CDATA[%JRE_VERSION_MAJOR%]]></token>
									<value>${installer.jre.version.major}</value>
								</replacement>
								<replacement>
									<token><![CDATA[%JRE_VERSION%]]></token>
									<value>${installer.jre.version}</value>
								</replacement>
								<replacement>
									<token>PROJECT_VERSION</token>
									<value>${project.version}</value>
								</replacement>
								<replacement>
									<token>VERSION_ID</token>
									<value>${installer-version-id}</value>
								</replacement>
								<replacement>
									<token>VERSION</token>
									<value>${project.version}</value>
								</replacement>
								<replacement>
									<token>UPDATE_XML_URL</token>
									<value>${collect.update_3.url}</value> <!-- always use latest update URL -->
								</replacement>
							</replacements>
						</configuration>
					</plugin>

					<!-- 4. iterate over platforms and prepare updaters -->
					<plugin>
						<groupId>com.soebes.maven.plugins</groupId>
						<artifactId>iterator-maven-plugin</artifactId>
						<executions>
							<execution>
								<id>download-jre-autoupdater</id>
								<phase>process-sources</phase>
								<goals>
									<goal>iterator</goal>
								</goals>
								<configuration>
									<pluginExecutors>
										<!-- download and extract JRE -->
										<pluginExecutor>
											<plugin>
												<groupId>org.apache.maven.plugins</groupId>
												<artifactId>maven-dependency-plugin</artifactId>
											</plugin>
											<goal>unpack</goal>
											<configuration>
												<artifactItems>
													<artifactItem>
														<groupId>net.java.openjdk</groupId>
														<artifactId>jre</artifactId>
														<version>${installer.jre.version.full}</version>
														<classifier>${jre.classifier}</classifier>
														<type>zip</type>
														<overWrite>false</overWrite>
														<outputDirectory>${installer-build-path}/jre_files</outputDirectory>
													</artifactItem>
												</artifactItems>
											</configuration>
										</pluginExecutor>
										<!-- copy autoupdater artifacts -->
										<pluginExecutor>
											<plugin>
												<groupId>org.apache.maven.plugins</groupId>
												<artifactId>maven-dependency-plugin</artifactId>
											</plugin>
											<goal>copy</goal>
											<configuration>
												<skip>${installer.skip}</skip>
												<artifactItems>
													<artifactItem>
														<groupId>org.openforis.collect</groupId>
														<artifactId>collect-autoupdater</artifactId>
														<version>${project.version}</version>
														<classifier>${installer.classifier}</classifier>
														<type>${installer.artifact.type}</type>
														<overWrite>false</overWrite>
														<outputDirectory>${installer-build-path}</outputDirectory>
														<destFileName>autoupdate-@item@.${installer.autoupdater.extension}</destFileName>
													</artifactItem>
												</artifactItems>
											</configuration>
										</pluginExecutor>
									</pluginExecutors>
								</configuration>
							</execution>
							<execution>
								<id>execute-installer</id>
								<phase>package</phase>
								<goals>
									<goal>iterator</goal>
								</goals>
								<configuration>
									<pluginExecutors>
										<!-- 4. execute InstallBuilder to produce the installers -->
										<pluginExecutor>
											<plugin>
												<groupId>org.codehaus.mojo</groupId>
												<artifactId>exec-maven-plugin</artifactId>
											</plugin>
											<goal>exec</goal>
											<configuration>
												<skip>${installer.skip}</skip>
												<executable>${installbuilder.builder.executable}</executable>
												<arguments>
													<argument>build</argument>
													<argument>${installer-build-path}/${installbuilder-file-name}</argument>
													<argument>@item@</argument>
												</arguments>
											</configuration>
										</pluginExecutor>
										<!-- 5. move generated installers into the target folder -->
										<pluginExecutor>
											<plugin>
												<groupId>org.apache.maven.plugins</groupId>
												<artifactId>maven-antrun-plugin</artifactId>
											</plugin>
											<goal>run</goal>
											<configuration>
												<skip>${installer.skip}</skip>
												<target>
													<move file="${installbuilder.builder.output}/${installbuilder.project.name}-${installer-version-id}-${installer.classifier}-installer.${installer.extension}" tofile="${project.build.directory}/${installer.classifier}-installer.${installer.extension}" failonerror="true" />
												</target>
											</configuration>
										</pluginExecutor>
										<!-- 6. attach installers as artifact -->
										<pluginExecutor>
											<plugin>
												<groupId>org.codehaus.mojo</groupId>
												<artifactId>build-helper-maven-plugin</artifactId>
											</plugin>
											<goal>attach-artifact</goal>
											<configuration>
												<skipAttach>${installer.skip}</skipAttach>
												<artifacts>
													<artifact>
														<file>${project.build.directory}/${installer.classifier}-installer.${installer.extension}</file>
														<classifier>${installer.classifier}</classifier>
														<type>${installer.extension}</type>
													</artifact>
												</artifacts>
											</configuration>
										</pluginExecutor>
									</pluginExecutors>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>
	<dependencies>
		<dependency>
			<groupId>org.openforis.collect</groupId>
			<artifactId>collect-control-panel</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>
</project>