<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<!-- 
	This is a special POM to be used as a (intermediate) parent for ImageJ plugins projects.
	It is assumed that the project has the following structure:
	
	project-root
	    pom.xml
	    ImageJ/
	    	jars/
	    	plugins/
	    	ij.jar				(copied as a Maven dependency during build)
	    src/
            main/
                java/
                resources/
                ...
        target/
        
	What this POM does:
	1) Defines 'project-root/ImageJ/plugins/' as the output-directory (where .class files are compiled to).
	2) Copies the POM-specified version of 'ij.jar' to 'project-root/ImageJ/' during build.
	3) Copies all other dependencies (JARs) to 'project-root/ImageJ/jars' during build.
	4) Empties 'project-root/ImageJ/plugins/' and 'project-root/ImageJ/jars/' during clean phase.
	5) Runs a Java program (contained in imagingbook-common) to build the 'plugins.config' file.
	6) Includes the 'plugins.config' in the target JAR.
	-->
	
	<parent>
		<groupId>com.imagingbook</groupId>
		<artifactId>imagingbook-parent-pom</artifactId>
		<version>4.0.0</version>
		<relativePath />
	</parent>

	<artifactId>imagingbook-plugins-pom</artifactId>
	<packaging>pom</packaging>

	<properties>
		<!-- <imagingbook.rootdir>${directory-up-1}</imagingbook.rootdir> -->
		<pluginConfigFile>plugins.config</pluginConfigFile>
		<!-- future use to specify general plugins path: -->
		<pluginPrefix>"Plugins&gt;B&amp;B "</pluginPrefix>	
	</properties>

	<dependencies>

		<dependency>
			<groupId>net.imagej</groupId>
			<artifactId>ij</artifactId>
		</dependency>

		<dependency>
			<groupId>com.imagingbook</groupId>
			<artifactId>imagingbook-common</artifactId>
			<version>4.0.0</version>
		</dependency>

	</dependencies>

	<!-- ******************* build profiles ************************** -->

	<profiles>
		<!-- 
			This is a special build profile for ImageJ plugin projects. If the project
			base directory contains an 'ImageJ' sub-folder it is assumed that the project
			defines ImageJ plugins. This sets the output directory to 'ImageJ/plugins',
			i.e., all project-related .class files are stored there, whereas all 
			dependencies are copied to the 'ImageJ/jars' folder.
			File 'ImageJ/ij.jar' is also updated by maven.
			Directories ImageJ/plugins and jars are cleared during the maven 'clean' phase.
			This should probably be in a sub-POM.
		-->
		<profile>
			<id>ij-plugins-build</id>
			<!-- activates this profile if directory ${project.basedir}/ImageJ exists -->
			<activation>
				<file>
                	<exists>ImageJ</exists>
            	</file>
			</activation>
			<properties>
				<imagingbook.buildprofile>IjPluginsProfile</imagingbook.buildprofile>
				<outputDirectory>${project.basedir}/ImageJ/plugins</outputDirectory>
			</properties>

			<build>
				<plugins>
					<!-- empty the ImageJ/plugins and ImageJ/jars directories at clean -->
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-clean-plugin</artifactId>
						<configuration>
							<!-- avoid removing the cleaned directories themselves, delete content only: -->
							<excludeDefaultDirectories>true</excludeDefaultDirectories>
							<filesets>
								<!-- clean ImageJ/jars/  -->
								<fileset>
									<directory>${project.basedir}/ImageJ/jars</directory>
									<includes>
										<include>**/*</include>
									</includes>
								</fileset>
								<!-- also clean the output directory (needed?) -->
								<fileset>
									<directory>${outputDirectory}</directory>
									 <!-- <includes> -->
										<!-- <include>**/*</include> -->
										<!-- <include>**/*.jar</include> -->
									<!-- </includes> -->
									<excludes>
										<exclude>.gitignore</exclude>
									</excludes>
								</fileset>
								<!-- because excludeDefaultDirectories=true, target doesn't get cleaned by default -->
								<fileset>
									<directory>${project.basedir}/target</directory>
									<includes>
										<include>**/*</include>
									</includes>
								</fileset>
							</filesets>
						</configuration>
					</plugin>


					<!-- Copy dependencies into different places of ImageJ project: 
						see https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html -->
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-dependency-plugin</artifactId>
						<executions>
							<execution>
								<!-- copy ij.jar into ImageJ/ -->
								<id>copy-ij-jar</id>
								<phase>package</phase>
								<goals>
									<goal>copy</goal>
								</goals>
								<configuration>
									<artifactItems>
										<artifactItem>
											<groupId>net.imagej</groupId>
											<artifactId>ij</artifactId>
											<outputDirectory>${project.basedir}/ImageJ</outputDirectory>
											<destFileName>ij.jar</destFileName>
										</artifactItem>
									</artifactItems>
									<overWriteIfNewer>true</overWriteIfNewer>
									<overWriteReleases>false</overWriteReleases>
									<overWriteSnapshots>true</overWriteSnapshots>
								</configuration>
							</execution>
							
							<execution>
								<!-- copy all imagingbook plugins into ImageJ/plugins/ -->
								<id>copy-imagingbook-dependencies</id>
								<phase>package</phase>
								<goals>
									<goal>copy-dependencies</goal>
								</goals>
								<configuration>
									<includeGroupIds>com.imagingbook</includeGroupIds>
									<excludeArtifactIds>imagingbook-common,imagingbook-core</excludeArtifactIds>
									<outputDirectory>${project.basedir}/ImageJ/plugins</outputDirectory>
									<overWriteIfNewer>true</overWriteIfNewer>
									<overWriteReleases>false</overWriteReleases>
									<overWriteSnapshots>true</overWriteSnapshots>
								</configuration>
							</execution>
							
							<execution>
								<!-- copy only imagingbook-common into ImageJ/jars/ -->
								<id>copy-imagingbook-common</id>
								<phase>package</phase>
								<goals>
									<goal>copy-dependencies</goal>
								</goals>
								<configuration>
									<includeGroupIds>com.imagingbook</includeGroupIds>
									<includeArtifactIds>imagingbook-common,imagingbook-core</includeArtifactIds>
									<outputDirectory>${project.basedir}/ImageJ/jars</outputDirectory>
									<overWriteIfNewer>true</overWriteIfNewer>
									<overWriteReleases>false</overWriteReleases>
									<overWriteSnapshots>true</overWriteSnapshots>
								</configuration>
							</execution>

							<execution>
								<!-- copy all other dependencies into ImageJ/jars/ -->
								<id>copy-other-dependencies</id>
								<phase>package</phase>
								<goals>
									<goal>copy-dependencies</goal>
								</goals>
								<configuration>
									<excludeGroupIds>com.imagingbook,net.imagej</excludeGroupIds>
									<excludeArtifactIds>ij</excludeArtifactIds>
									<outputDirectory>${project.basedir}/ImageJ/jars</outputDirectory>
									<overWriteIfNewer>true</overWriteIfNewer>
									<overWriteReleases>false</overWriteReleases>
									<overWriteSnapshots>true</overWriteSnapshots>
								</configuration>
							</execution>
							
							<!-- copy the newly installed target jar to ImageJ/jars (could this
								be a problem?) -->
							<!-- <execution> <id>copy-installed</id> <phase>install</phase> <goals> 
								<goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> 
								<groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> 
								<version>${project.version}</version> <type>${project.packaging}</type> </artifactItem> 
								</artifactItems> <outputDirectory>${project.basedir}/ImageJ/jars</outputDirectory> 
								</configuration> </execution> -->

						</executions>
					</plugin>
					<!-- end of copy -->
					
					<!-- Run Java program to build plugins.config file in output directory (to be included in output JAR) -->
					<plugin>
						<groupId>org.codehaus.mojo</groupId>
						<artifactId>exec-maven-plugin</artifactId>
						<version>3.0.0</version>
						<executions>
							<execution>
								<id>codegeneration</id>
								<!-- <phase>generate-resources</phase>  -->
								<!-- <phase>process-classes</phase>  -->
								<phase>prepare-package</phase>
								<goals>
									<goal>java</goal>
								</goals>
								<configuration>
									<mainClass>imagingbook.core.plugin.PluginsConfigBuilder</mainClass>
									<arguments>
										<argument>${project.build.outputDirectory}</argument>
										<argument>${project.name}</argument>
										<argument>${pluginPrefix}</argument> <!-- reserved for future use -->
									</arguments>
								</configuration>
							</execution>
						</executions>
					</plugin>

				</plugins>
				
				<resources>
					<!-- add plugins.config to the project's JAR -->
					<resource>
						<directory>src/main/java</directory>
						<includes>
							<include>**/${pluginConfigFile}</include>
						</includes>
					</resource>
				</resources>
			</build>
		</profile>
	</profiles>

</project>