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

  <artifactId>packaging</artifactId>
  <name>OpenRefine - packaging</name>
  <description>Creates packages for all supported operating systems</description>
  <url>http://openrefine.org/</url>
  <packaging>pom</packaging>

  <parent>
    <groupId>org.openrefine</groupId>
    <artifactId>openrefine</artifactId>
    <version>3.9.5</version>
  </parent>
  <properties>
    <rootdir>${basedir}/..</rootdir>
    <webapp.dir>../main/webapp</webapp.dir>
    <webapp.target>../main/target</webapp.target>
    <embedded.jre.version>11</embedded.jre.version>

    <mac.jre.path>${project.build.directory}/mac_jre</mac.jre.path>
    <mac.jre.url>https://api.adoptium.net/v3/binary/latest/${embedded.jre.version}/ga/mac/x64/jre/hotspot/normal/eclipse?project=jdk</mac.jre.url>
    <mac.build.directory>OpenRefine ${project.version}</mac.build.directory>
    <mac.webapp.dir>./target/OpenRefine.app/Contents/Resources/webapp/</mac.webapp.dir>
    <mac.icon.path>../graphics/icon/openrefine.icns</mac.icon.path>
    <mac.dmg.path>${project.build.directory}/${project.build.finalName}-mac-${project.version}.dmg</mac.dmg.path>
    <mac.signing.identity>Code for Science and Society, Inc.</mac.signing.identity>
    <!-- Apple Notarization settings -->
    <apple.notarization.timeout>15m</apple.notarization.timeout>

    <windows.jre.path>${project.build.directory}/windows_jre</windows.jre.path>
    <windows.jre.url>https://api.adoptium.net/v3/binary/latest/${embedded.jre.version}/ga/windows/x64/jre/hotspot/normal/eclipse?project=jdk</windows.jre.url>
    <windows.copyright.line>Copyright (c) 2018 OpenRefine contributors, 2010 Google, Inc.</windows.copyright.line>
    <windows.fullversion>0.0.0.0</windows.fullversion>
  </properties>

  <profiles>
    <profile>
      <id>default</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>sh.tak.appbundler</groupId>
            <artifactId>appbundle-maven-plugin</artifactId>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <id>package-distributions</id>
                <configuration>
                  <descriptors>
                    <descriptor>linux.xml</descriptor>
                    <descriptor>windows-with-jre.xml</descriptor>
                  </descriptors>
                  <attach>false</attach>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>linux</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <id>package-distributions</id>
                <configuration>
                  <descriptors>
                    <descriptor>linux.xml</descriptor>
                  </descriptors>
                  <attach>false</attach>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>windows</id>
      <build>
        <plugins>
          <plugin>
            <groupId>sh.tak.appbundler</groupId>
            <artifactId>appbundle-maven-plugin</artifactId>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <id>package-distributions</id>
                <configuration>
                  <descriptors>
                    <descriptor>windows-without-jre.xml</descriptor>
                    <descriptor>windows-with-jre.xml</descriptor>
                  </descriptors>
                  <attach>false</attach>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <build>
    <plugins>
      <plugin> <!-- gathers license files for all dependencies at packaging time -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>license-maven-plugin</artifactId>
        <version>${license-plugin.version}</version>
        <configuration>
          <socketTimeout>10000</socketTimeout>
          <connectTimeout>10000</connectTimeout>
          <cleanLicensesOutputDirectory>true</cleanLicensesOutputDirectory>
          <licensesConfigFile>${basedir}/manual_licenses.xml</licensesConfigFile>
          <excludedArtifacts>oro</excludedArtifacts>  
        </configuration>
        <executions>
          <execution>
            <phase>generate-test-resources</phase>
            <goals>
              <goal>download-licenses</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin> <!-- we attach a dummy main artifact for the packaging project, 
                because Appbundle requires one -->
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>groovy-maven-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
          <execution>
          <id>set-main-artifact</id>
          <phase>prepare-package</phase>
          <goals> 
            <goal>execute</goal>
          </goals>
          <configuration>
            <source>
            project.artifact.setFile(new File("./packaging/target/dummy_artifact.jar"))
            </source>
          </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin> <!-- necessary for Appbundle -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>prepare-package</phase>
            <id>prepare-mac-bundle</id>
            <configuration>

              <target>
                <mkdir dir="./target/"/>
                <touch file="./target/dummy_artifact.jar" />

                <mkdir dir="${mac.webapp.dir}" />
                    
                    <copy todir="${mac.webapp.dir}">
                        <fileset dir="${webapp.dir}">
                            <include name="**/*"/>
                            <exclude name="node_modules/**"/>
                            <exclude name="package.json"/>
                            <exclude name="package-lock.json"/>
                            <exclude name="dependencies.json"/>
                            <exclude name="copy-dependencies.js"/>
                            <exclude name="README.md"/>
                            <exclude name="**/test-output/**"/>
                            <exclude name="WEB-INF/classes/**"/>
                            <exclude name="WEB-INF/lib-local/**"/>
                            <exclude name="WEB-INF/lib-local-src/**"/>
                            <exclude name="WEB-INF/lib/icu4j*.jar"/>
                        </fileset>
                    </copy>
                    <copy todir="${mac.webapp.dir}">
                        <fileset dir="${webapp.dir}">
                            <include name="**/*.properties"/>
                        </fileset>
                    </copy>
                    <copy todir="${mac.webapp.dir}/WEB-INF/lib/">
                        <fileset dir="${webapp.target}">
                            <include name="openrefine-main.jar" />
                        </fileset>
                    </copy>
                    
                    <copy todir="${mac.webapp.dir}/licenses/"> <!-- licenses for the backend -->
                        <fileset dir="${project.build.directory}/generated-resources/">
                            <include name="licenses.xml" />
                            <include name="licenses/**/*" />
                        </fileset>
                    </copy>

                    <copy todir="${mac.webapp.dir}/licenses/licenses/"> <!-- licenses for the frontend -->
                        <fileset dir="${webapp.dir}/licenses/">
                            <include name="**/*" />
                        </fileset>
                    </copy>

                    <copy todir="${mac.webapp.dir}/extensions">
                        <fileset dir="../extensions">
                            <include name="**/*"/>
                            <exclude name="**/pom.xml"/>
                            <exclude name="**/src/**"/>
                            <exclude name="**/lib-local/**"/>
                            <exclude name="**/lib-local-src/**"/>
                            <exclude name="**/target/**" />
                            <exclude name="**/tests/**" />
                            <exclude name="**/*.java"/>
                            <exclude name="**/.settings/**" />
                            <exclude name="**/.project" />
                        </fileset>
                    </copy>
                    
                    <replace file="${mac.webapp.dir}/WEB-INF/web.xml">
                        <replacefilter token="$VERSION" value="${project.version}"/>
                    </replace>

                    <replace file="${mac.webapp.dir}/WEB-INF/butterfly.properties">
                        <replacefilter token="../../extensions" value="extensions"/>
                    </replace>

              </target>

            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
          <execution>
            <id>download-jre</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target name="downloadJRE" description="Download JRE">
                <!-- Download JRE (Mac OS X) -->
                <exec dir="${project.build.directory}" executable="curl">
                  <arg line="-L"/>
                  <arg line="-C"/>
                  <arg line="-"/>
                  <arg line="-o"/>
                  <arg line="mac_jre.tar.gz"/>
                  <arg line="${mac.jre.url}"/>
                </exec>

                <!-- Download JRE (Windows) -->
                <exec dir="${project.build.directory}" executable="curl">
                  <arg line="-L"/>
                  <arg line="-C"/>
                  <arg line="-"/>
                  <arg line="-o"/>
                  <arg line="windows_jre.zip"/>
                  <arg line="${windows.jre.url}"/>
                </exec>

                <!-- Unzip Mac JRE archive -->
                <mkdir dir="${project.build.directory}/mac_jre"/>
                <exec dir="${project.build.directory}" executable="tar">
                  <arg line="-zxf"/>
                  <arg line="mac_jre.tar.gz"/>
                  <arg line="-C &quot;mac_jre&quot; --strip-components=1"/>
                </exec>

                <!-- Unzip Windows JRE archive -->
                <!-- The Windows JRE is downloaded as a Zip file so we cannot untar it. -->
                <!-- We use the mapper to get rid of the root directory whose name is unpredictable -->
                <mkdir dir="${project.build.directory}/windows_jre"/>
                <unzip src="${project.build.directory}/windows_jre.zip" dest="${project.build.directory}/windows_jre">
                  <mapper type="regexp" from="^([^/]*)/(.*)$$" to="\2"/>
                </unzip>
              </target>
            </configuration>
          </execution>
          <execution>
            <id>generate-dmg</id>
            <phase>package</phase> 
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target name="generateDMG" description="Generate DMG">

                <!-- Remove manpages from JRE as they are not accessible anyway, and prevent us from codesigning -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="rm">
                    <arg line="-r"/>
                    <arg line="&quot;${mac.build.directory}/OpenRefine.app/Contents/PlugIns/JRE/Contents/Home/jre/man&quot;"/>
                </exec>
                <!-- remove JRE licenses folder, cannot be signed -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="rm">
                    <arg line="-r"/>
                    <arg line="&quot;${mac.build.directory}/OpenRefine.app/Contents/PlugIns/JRE/Contents/Home/jre/legal&quot;"/>
                </exec>
                <!-- adapt file permissions, for https://github.com/OpenRefine/OpenRefine/issues/5160 -->
                <exec dir="${project.build.directory}" executable="chmod">
                    <arg line="-R" />
                    <arg line="go+r" />
                    <arg line="&quot;${mac.build.directory}/OpenRefine.app/Contents/Resources/webapp/WEB-INF/&quot;" />
                </exec>

                <!-- remove any existing DMG (hdiutil fails otherwise) -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="rm">
                    <arg value="-f"/>
                    <arg value="${mac.dmg.path}"/>
		</exec>

                <!-- Remove quarantine bit set recursively on JRE -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="xattr">
                    <arg line="-rd"/>
                    <arg line="com.apple.quarantine"/>
                    <arg line="&quot;${mac.build.directory}/OpenRefine.app/Contents/PlugIns&quot;"/>
                </exec>

                <!-- Codesign JRE -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="codesign">
                    <arg value="--entitlements" />
                    <arg value="../Entitlements.plist" />
                    <arg value="--sign"/>
                    <arg value="${mac.signing.identity}"/>
                    <arg value="--deep"/>
                    <arg value="--force"/>
                    <arg value="--timestamp"/>
                    <arg value="--options"/>
                    <arg value="runtime"/>
                    <arg value="${mac.build.directory}/OpenRefine.app/Contents/PlugIns/JRE/Contents/Home/jre/lib/jli/libjli.dylib" />
                </exec>

                <!-- Codesign jars with native code in them -->
                <exec executable="/bin/bash" os="Mac OS X" dir="${project.build.directory}/${mac.build.directory}">
                    <arg value="${basedir}/apple_certs/codesign.sh" />
                    <arg value="OpenRefine.app" />
                    <arg value="${mac.signing.identity}" />
                    <arg value="../../Entitlements.plist" />
                </exec>

                <!-- Codesign app -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="codesign">
                    <arg value="--entitlements" />
                    <arg value="../Entitlements.plist" />
                    <arg value="--sign"/>
                    <arg value="${mac.signing.identity}"/>
                    <arg value="--deep"/>
                    <arg value="--force"/>
                    <arg value="--timestamp"/>
                    <arg value="--options"/>
                    <arg value="runtime"/>
                    <arg value="${mac.build.directory}/OpenRefine.app"/>
                </exec>

                <!-- Create DMG (Mac OS X) -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="python3">
                    <arg value="-m"/>
                    <arg value="dmgbuild"/>
                    <arg value="-s"/>
                    <arg value="../dmg_settings.py"/>
                    <arg value="-D"/>
                    <arg value="app=${mac.build.directory}/OpenRefine.app"/>
                    <arg value="OpenRefine"/>
                    <arg value="${mac.dmg.path}"/>
                </exec>

                <!-- Create DMG (Linux), only for testing -->
                <exec dir="${project.build.directory}" os="Linux" executable="genisoimage">
                    <arg value="-V"/>
                    <arg value="OpenRefine"/>
                    <arg value="-U"/>
                    <arg value="-D"/>
                    <arg value="-l"/>
                    <arg value="-allow-multidot"/>
                    <arg value="-max-iso9660-filenames"/>
                    <arg value="-relaxed-filenames"/>
                    <arg value="-no-iso-translate"/>
                    <arg value="-r"/>
                    <arg value="-o"/>
                    <arg value="${mac.dmg.path}"/>
                    <arg value="-root"/>
                    <arg value="${mac.build.directory}"/>
                    <arg value="${mac.build.directory}"/>
                </exec>

                <!-- Send DMG file to Apple's notarization service -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="xcrun">
                    <arg value="notarytool"/>
                    <arg value="submit"/>
                    <arg value="${mac.dmg.path}"/>
                    <arg value="--apple-id"/>
                    <arg value="${apple.notarization.username}"/>
                    <arg value="--password"/>
                    <arg value="${apple.notarization.password}"/>
                    <arg value="--team-id"/>
                    <arg value="${apple.notarization.team.id}"/>
                    <arg value="--wait"/>
                    <arg value="--timeout"/>
                    <arg value="${apple.notarization.timeout}"/>
                </exec>

                <!-- Staple the notarization ticket to the DMG bundle -->
                <exec dir="${project.build.directory}" os="Mac OS X" executable="xcrun">
                    <arg value="stapler"/>
                    <arg value="staple"/>
                    <arg value="${mac.dmg.path}"/>
                </exec>
              </target>
            </configuration>
          </execution>

        </executions>
      </plugin>
      <plugin>
        <groupId>com.akathist.maven.plugins.launch4j</groupId>
        <artifactId>launch4j-maven-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
          <execution>
            <id>l4j-clui-with-jre</id>
            <phase>package</phase>
            <goals><goal>launch4j</goal></goals>
            <configuration>
              <headerType>console</headerType>
              <outfile>target/openrefine_with_jre.exe</outfile>
              <jar>server/target/lib/openrefine-${project.version}-server.jar</jar>
              <dontWrapJar>true</dontWrapJar>
              <saveConfig>true</saveConfig>
              <icon>openrefine.ico</icon>
              <classPath>
                <mainClass>com.google.refine.Refine</mainClass>
                <jarLocation>server/target/lib/</jarLocation>
                <postCp>server/target/lib/*.jar</postCp>
              </classPath>
              <jre>
                <path>server/target/jre</path>
                <minVersion>${java.minversion}</minVersion>
                <maxVersion>${java.maxversion}</maxVersion>
                <initialHeapSize>512</initialHeapSize>
                <maxHeapSize>2048</maxHeapSize>
                <opts>
                  <opt>-Djava.library.path=server/target/lib/native/windows</opt>
                </opts>
              </jre>
              <versionInfo>
                <fileVersion>${windows.fullversion}</fileVersion>
                <txtFileVersion>${project.version}</txtFileVersion>
                <fileDescription>openrefine</fileDescription>
                <copyright>${windows.copyright.line}</copyright>
                <productVersion>${windows.fullversion}</productVersion>
                <txtProductVersion>${project.version}</txtProductVersion>
                <productName>OpenRefine</productName>
                <internalName>openrefine</internalName>
                <originalFilename>openrefine.exe</originalFilename>
              </versionInfo>
            </configuration>
          </execution>

        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.5.2</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
    	<plugins>
    		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
    		<plugin>
    			<groupId>org.eclipse.m2e</groupId>
    			<artifactId>lifecycle-mapping</artifactId>
    			<version>1.0.0</version>
    			<configuration>
    				<lifecycleMappingMetadata>
    					<pluginExecutions>
    						<pluginExecution>
    							<pluginExecutionFilter>
    								<groupId>
    									org.apache.maven.plugins
    								</groupId>
    								<artifactId>
    									maven-antrun-plugin
    								</artifactId>
    								<versionRange>[1.4,)</versionRange>
    								<goals>
    									<goal>run</goal>
    								</goals>
    							</pluginExecutionFilter>
    							<action>
    								<ignore></ignore>
    							</action>
    						</pluginExecution>
    					</pluginExecutions>
    				</lifecycleMappingMetadata>
    			</configuration>
    		</plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.7.1</version>
          <executions>
            <execution>
              <id>package-distributions</id>
              <goals>
                <goal>single</goal>
              </goals>
              <phase>package</phase>
              <configuration>
                <descriptors>
                <!-- TBD -->
                </descriptors>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>sh.tak.appbundler</groupId>
          <artifactId>appbundle-maven-plugin</artifactId>
          <version>1.2.0</version>
          <dependencies>
            <dependency>
              <!-- dependency upgraded to >4.0 to fix runtime issues with some JRE versions:
                   https://stackoverflow.com/questions/53696439/appbundle-maven-plugin-fails-on-mac-osx-with-java-10 -->
              <groupId>org.codehaus.plexus</groupId>
              <artifactId>plexus-archiver</artifactId>
              <version>4.10.0</version>
            </dependency>
            <dependency>
              <groupId>org.apache.velocity</groupId>
              <artifactId>velocity-tools</artifactId>
              <version>2.0</version>
            </dependency>
          </dependencies>
          <configuration>
            <mainClass>com.google.refine.Refine</mainClass>
            <bundleName>OpenRefine</bundleName>
            <buildDirectory>${project.build.directory}/${mac.build.directory}</buildDirectory>
            <iconFile>${mac.icon.path}</iconFile>
            <generateDiskImageFile>false</generateDiskImageFile>
            <jvmVersion>1.8</jvmVersion>
            <jrePath>${mac.jre.path}</jrePath>
            <jvmOptions>
              <param>-Xms512M</param>
              <param>-Xmx2048M</param>
              <param>-Drefine.version=${project.version}</param>
              <param>-Drefine.webapp=$APP_ROOT/Contents/Resources/webapp</param>
            </jvmOptions>
            <additionalResources>
              <fileSet>
                <directory>target</directory>
                <includes>
                  <include>OpenRefine.app/Contents/Resources/webapp/**/**</include>
                </includes>
              </fileSet>
            </additionalResources>
            <additionalBundledClasspathResources>
              <fileSet>
                <directory>${rootdir}/server/target/lib/</directory>
                <includes>
                  <include>*.jar</include>
                </includes>
              </fileSet>
              <fileSet>
                <directory>${rootdir}/server/target</directory>
                <includes>
                  <include>openrefine-${project.version}-server.jar</include>
                </includes>
                <excludes>
                  <exclude>**/lib/**</exclude>
                  <exclude>**/org/**</exclude>
                </excludes>
              </fileSet>
            </additionalBundledClasspathResources>
          </configuration>
          <executions>
            <execution>
              <phase>prepare-package</phase>
              <goals>
                <goal>bundle</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
         <plugin>
	   <groupId>org.apache.maven.plugins</groupId>
	   <artifactId>maven-deploy-plugin</artifactId>
	   <version>${maven-deploy-plugin.version}</version>
	   <configuration>
	    <skip>false</skip>
	  </configuration>
         </plugin>
    	</plugins>
    </pluginManagement>
  </build>
  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>server</artifactId>
      <version>${project.version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>
