<?xml version="1.0" encoding="utf-8"?>
<!--
** Template for the build targets common to all plugins
** ====================================================
**
** To override a property, add it to the plugin build.xml _before_
** this template has been imported.
** To override a target, add it _after_ this template has been imported.
**
** Paths are relative to the build.xml that imports this template.
**
-->
<project name="plugin_common" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:if="ant:if" xmlns:unless="ant:unless" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:mvn="antlib:org.apache.maven.resolver.ant" >

    <property name="josm"                   location="../../core/dist/josm-custom.jar"/>
    <property name="josm.ivy"               location="../../core/ivy.xml"/>
    <property name="josm.ivysettings"       location="../../core/ivysettings.xml"/>
    <property name="josm.test.build.dir"    location="../../core/test/build"/>
    <property name="core.tools.ivy"         location="../00_core_tools/ivy.xml"/>
    <property name="plugin.tools.dir"       location="../00_tools"/>
    <property name="plugin.build.dir"       location="build"/>
    <property name="plugin.test.dir"        location="test"/>
    <property name="plugin.src.dir"         location="src"/>
    <property name="plugin.resources.dir"   location="resources"/>
    <property name="plugin.doc.dir"         location="javadoc"/>
    <property name="plugin.lib.dir"         location="lib"/>
    <!-- this is the directory where the plugin jar is copied to -->
    <property name="plugin.dist.dir"        location="../../dist"/>
    <property name="java.lang.version"      value="11" />
    <property name="javadoc.executable"     value="javadoc" />
    <property name="manifest"               value="MANIFEST"/>
    <property name="manifest.unixoid"       value="MANIFEST-unixoid"/>
    <property name="manifest.windows"       value="MANIFEST-windows"/>
    <property name="manifest.osx"           value="MANIFEST-osx"/>
    <property name="plugin.jar"             location="${plugin.dist.dir}/${ant.project.name}.jar"/>
    <property name="plugin.unixoid.jar"     location="${plugin.dist.dir}/${ant.project.name}-unixoid.jar"/>
    <property name="plugin.windows.jar"     location="${plugin.dist.dir}/${ant.project.name}-windows.jar"/>
    <property name="plugin.osx.jar"         location="${plugin.dist.dir}/${ant.project.name}-osx.jar"/>
    <property name="plugin.sources.jar"     location="${plugin.dist.dir}/${ant.project.name}-sources.jar"/>
    <property name="plugin.javadoc.jar"     location="${plugin.dist.dir}/${ant.project.name}-javadoc.jar"/>
    <property name="ivy.home"               location="${user.home}/.ant"/>
    <property name="ivy.jar.dir"            location="${ivy.home}/lib"/>
    <property name="ivy.jar.file"           location="${ivy.jar.dir}/ivy.jar"/>
    <property name="ivy.version"            value="2.5.3"/>
    <property name="maven.artifact.resolver.version" value="1.5.1"/>
    <property name="maven.artifact.resolver.parent.dir" value="${user.home}/.m2/repository/org/apache/maven/resolver/maven-resolver-ant-tasks/${maven.artifact.resolver.version}/"/>
    <property name="maven.artifact.resolver.jar.file" value="${maven.artifact.resolver.parent.dir}/maven-resolver-ant-tasks-${maven.artifact.resolver.version}-uber.jar"/>
    <property name="jacoco.inclbootstrapclasses"  value="false" />
    <property name="jacoco.inclnolocationclasses" value="false" />
    <property name="junit.printsummary"     value="on" />

    <!-- For platform-specific stuff -->
    <condition property="isWindows"><os family="Windows"/></condition>
    <condition property="isUnix"><os family="Unix"/></condition>
    <condition property="isMac"><os family="Mac"/></condition>
    <!-- For Java specific stuff by version -->
    <condition property="isJava14"><matches string="${ant.java.version}" pattern="1[4-9]|[2-9][0-9]" /></condition>
    <condition property="isJava15"><matches string="${ant.java.version}" pattern="1[5-9]|[2-9][0-9]" /></condition>
    <condition property="isJava17"><matches string="${ant.java.version}" pattern="1[7-9]|[2-9][0-9]" /></condition>
    <condition property="isJava21"><matches string="${ant.java.version}" pattern="2[1-9]|[3-9][0-9]" /></condition>

    <!-- set the javadoc location -->
    <property name="javadoc.link"           value="https://docs.oracle.com/en/java/javase/11/docs/api/" unless:set="isJava17"/>
    <property name="javadoc.link"           value="https://docs.oracle.com/en/java/javase/17/docs/api/" if:set="isJava17" unless:set="isJava21"/>
    <property name="javadoc.link"           value="https://docs.oracle.com/en/java/javase/21/docs/api/" if:set="isJava21"/>

    <!-- Disable jacoco on Java 21+ (Jacoco does not yet support Java 21+) -->
    <condition property="coverageByDefault">
        <not>
            <isset property="isJava21"/>
        </not>
    </condition>
    <target name="-jaxb_windows" if="isWindows">
        <property name="xjc" value="${plugin.tools.dir}${file.separator}jaxb-ri${file.separator}bin${file.separator}xjc.bat" />
    </target>
    <target name="-jaxb_linux" unless="isWindows">
        <property name="xjc" value="${plugin.tools.dir}${file.separator}jaxb-ri${file.separator}bin${file.separator}xjc.sh" />
    </target>

    <!-- To be overriden in plugin build file before inclusion if other plugins are required -->
    <fileset id="plugin.requires.jars" dir="${plugin.dist.dir}" includes="nothing"/>

    <fileset id="jaxb.jars" dir="${plugin.tools.dir}/jaxb-ri/lib" includes="**/*.jar"/>

    <path id="plugin.classpath">
        <pathelement location="${josm}"/>
        <fileset dir="${plugin.lib.dir}" erroronmissingdir="no">
            <include name="**/*.jar"/>
            <exclude name="**/*-sources.jar"/>
            <exclude name="**/*-javadoc.jar"/>
            <exclude name="**/*-unixoid.jar" unless="isUnix"/>
            <exclude name="**/*-windows.jar" unless="isWindows"/>
            <exclude name="**/*-osx.jar" unless="isMac"/>
        </fileset>
        <fileset refid="plugin.requires.jars"/>
        <fileset refid="jaxb.jars"/>
    </path>

    <!--
    **********************************************************
    ** init - initializes the build
    **********************************************************
    -->
    <target name="init">
        <mkdir dir="${plugin.build.dir}"/>
    </target>
    <!--
    **********************************************************
    ** compile - compiles the source tree
    **********************************************************
    -->
    <target name="pre-compile">
        <!-- to be overridden by plugins that need to perform additional tasks before compiling -->
    </target>
    <target name="plugin-classpath-actual">
        <condition property="plugin.classpath.actual.defined">
            <isreference refid="plugin.classpath.actual" type="path"/>
        </condition>
        <path id="plugin.classpath.actual" unless:set="plugin.classpath.actual.defined">
            <path refid="plugin.classpath"/>
        </path>
    </target>
    <target name="compile" depends="init, pre-compile, resolve-tools, plugin-classpath-actual" unless="skip-compile">
        <echo message="compiling sources for ${plugin.jar} ..."/>
        <path id="jdk.boot.classpath">
            <path refid="errorprone_javac.classpath"/>
            <fileset refid="jaxb.jars"/>
        </path>
        <javac srcdir="${plugin.src.dir}" debug="true" destdir="${plugin.build.dir}" includeantruntime="false"
            encoding="UTF-8" release="${java.lang.version}" fork="yes">
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED" unless:set="noErrorProne"/>
            <compilerarg value="-XDaddTypeAnnotationsToSymbol=true" if:set="isJava21" unless:set="noErrorProne"/>
            <compilerarg value="-XDcompilePolicy=simple"/>
            <compilerarg value="--should-stop=ifError=FLOW" />
            <compilerarg value="-processorpath"/>
            <compilerarg pathref="errorprone.classpath"/>
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xplugin:ErrorProne -Xep:StringSplitter:OFF -Xep:ReferenceEquality:OFF -Xep:InsecureCryptoUsage:OFF -Xep:FutureReturnValueIgnored:OFF -Xep:JdkObsolete:OFF -Xep:EqualsHashCode:OFF -Xep:JavaUtilDate:OFF -Xep:DoNotCallSuggester:OFF -Xep:BanSerializableRead:OFF" />
            <compilerarg line="-Xmaxwarns 1000"/>
            <classpath refid="plugin.classpath.actual"/>
        </javac>
    </target>
    <!--
    **********************************************************
    ** setup-dist - copies files for distribution
    **********************************************************
    -->
    <target name="setup-dist-default">
        <copy todir="${plugin.build.dir}/resources" failonerror="no" includeemptydirs="no">
            <fileset dir="${plugin.resources.dir}"/>
        </copy>
        <copy todir="${plugin.build.dir}/images" failonerror="no" includeemptydirs="no">
            <fileset dir="images"/>
        </copy>
        <copy todir="${plugin.build.dir}/data" failonerror="no" includeemptydirs="no">
            <fileset dir="data"/>
        </copy>
        <copy todir="${plugin.build.dir}">
            <fileset dir=".">
                <include name="README"/>
                <include name="LICENSE*"/>
                <include name="*GPL*"/>
                <exclude name="*.md"/>
            </fileset>
        </copy>
    </target>
    <target name="setup-dist">
        <antcall target="setup-dist-default" />
    </target>
    <!--
    **********************************************************
    ** dist - creates the plugin jars
    **********************************************************
    -->
    <target name="dist" depends="compile,javadoc,revision" unless="skip-dist">
        <echo message="creating ${ant.project.name}.jar ... "/>
        <antcall target="setup-dist" />
        <delete failonerror="no">
            <fileset dir="." includes="${manifest}*" />
        </delete>
        <manifest file="${manifest}" mode="update">
            <attribute name="Plugin-Mainversion" value="${plugin.main.version}"/>
            <attribute name="Plugin-Version" value="${version.entry.commit.revision}" unless:set="plugin.version"/>
            <attribute name="Plugin-Version" value="${plugin.version}" if:set="plugin.version"/>
            <attribute name="Plugin-Class" value="${plugin.class}" />
            <attribute name="Plugin-Description" value="${plugin.description}" />
            <attribute name="Plugin-Date" value="${version.entry.commit.date}" />
            <attribute name="Author" value="${plugin.author}"/>
        </manifest>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Link"/>
            <param name="property.name" value="plugin.link"/>
            <param name="property.value" value="${plugin.link}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Icon"/>
            <param name="property.name" value="plugin.icon"/>
            <param name="property.value" value="${plugin.icon}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Early"/>
            <param name="property.name" value="plugin.early"/>
            <param name="property.value" value="${plugin.early}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Provides"/>
            <param name="property.name" value="plugin.provides"/>
            <param name="property.value" value="${plugin.provides}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Requires"/>
            <param name="property.name" value="plugin.requires"/>
            <param name="property.value" value="${plugin.requires}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Stage"/>
            <param name="property.name" value="plugin.stage"/>
            <param name="property.value" value="${plugin.stage}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Canloadatruntime"/>
            <param name="property.name" value="plugin.canloadatruntime"/>
            <param name="property.value" value="${plugin.canloadatruntime}"/>
        </antcall>
        <antcall target="add-manifest-attribute">
            <param name="manifest.attribute" value="Plugin-Minimum-Java-Version"/>
            <param name="property.name" value="plugin.minimum.java.version"/>
            <param name="property.value" value="${plugin.minimum.java.version}"/>
        </antcall>
        <antcall target="additional-manifest" />
        <antcall target="build-jar" />
        <jar destfile="${plugin.sources.jar}" basedir="${plugin.src.dir}" level="9"/>
        <jar destfile="${plugin.javadoc.jar}" basedir="${plugin.doc.dir}" level="9"/>
        <delete failonerror="no">
            <fileset dir="." includes="${manifest}*" />
        </delete>
        <antcall target="post-dist" />
    </target>
    <target name="build-jar">
        <jar destfile="${plugin.jar}" basedir="${plugin.build.dir}" manifest="${manifest}" manifestencoding="UTF-8" duplicate="preserve" level="9">
            <restrict>
                <not><or>
                    <name name="META-INF/maven/*"/>
                    <name name="META-INF/DEPENDENCIES"/>
                    <name name="META-INF/LICENSE"/>
                    <name name="META-INF/NOTICE"/>
                    <name name="META-INF/*.RSA"/>
                    <name name="META-INF/*.SF"/>
                    <name name="module-info.class"/>
                </or></not>
                <archives>
                    <zips>
                        <fileset dir="${plugin.lib.dir}" includes="*.jar" excludes="*-sources.jar, *-javadoc.jar" erroronmissingdir="no"/>
                    </zips>
                </archives>
            </restrict>
        </jar>
    </target>
    <target name="post-dist">
        <!-- to be overridden by plugins that need to perform additional tasks on resulting jar -->
    </target>
    <target name="add-manifest-attribute" depends="check-manifest-attribute" if="have-${property.name}">
        <manifest file="${manifest}" mode="update">
            <attribute name="${manifest.attribute}" value="${property.value}" />
        </manifest>
    </target>
    <!-- target to add additional entries, empty in commons -->
    <target name="additional-manifest">
    </target>
    <target name="check-manifest-attribute">
        <condition property="have-${property.name}">
            <and>
                <isset property="${property.name}"/>
                <not>
                    <equals arg1="${property.value}" arg2=""/>
                </not>
                <not>
                    <equals arg1="${property.value}" arg2="..."/>
                </not>
            </and>
        </condition>
    </target>
    <target name="pre-javadoc">
        <!-- to be overridden by plugins that need to perform additional tasks before generating javadoc -->
    </target>
    <target name="javadoc" depends="pre-javadoc,pre-compile,plugin-classpath-actual" unless="skip-javadoc">
        <mkdir dir="${plugin.doc.dir}"/>
        <javadoc destdir="${plugin.doc.dir}"
                executable="${javadoc.executable}"
                encoding="UTF-8"
                windowtitle="JOSM-${ant.project.name}"
                use="true"
                private="true"
                linksource="true"
                author="false">
            <classpath refid="plugin.classpath" unless:set="plugin.classpath.dependencies"/>
            <classpath refid="plugin.classpath.dependencies" if:set="plugin.classpath.dependencies"/>
            <classpath refid="plugin.classpath.actual"/>
            <sourcepath>
                <pathelement path="${plugin.src.dir}" />
                <pathelement path="gen" />
                <pathelement path="includes" />
            </sourcepath>
            <link href="${javadoc.link}"/>
            <link href="https://josm.openstreetmap.de/doc"/>
            <doctitle><![CDATA[<h2>JOSM-${ant.project.name} - Javadoc</h2>]]></doctitle>
            <bottom><![CDATA[<a href="https://josm.openstreetmap.de/wiki/Plugins">JOSM Plugins</a>]]></bottom>
            <arg line="-tag license:X" />
            <arg value="-html5" />
            <arg value="--add-exports" unless:set="noJavaFX" />
            <arg value="javafx.graphics/com.sun.javafx.application=ALL-UNNAMED" unless:set="noJavaFX" />
        </javadoc>
    </target>
    <!--
    **********************************************************
    ** revision - extracts the current revision number for the
    **    file build.number and stores it in the XML property
    **    version.*
    **********************************************************
    -->
    <!--
      ** Initializes the REVISION.XML file from SVN information
    -->
    <target name="init-svn-revision-xml" unless="skip-revision">
        <exec append="false" output="REVISION.XML" executable="svn" failifexecutionfails="false" resultproperty="svn.info.result">
            <env key="LANG" value="C"/>
            <arg value="info"/>
            <arg value="--xml"/>
            <arg value="."/>
        </exec>
        <condition property="svn.info.fail">
            <not>
                <and>
                    <equals arg1="${svn.info.result}" arg2="0" />
                    <length file="REVISION.XML" when="greater" length="1" />
                </and>
            </not>
        </condition>
    </target>
    <!--
      ** Initializes the REVISION.XML file from git-svn information.
         Obtains the revision from the git-svn-id field.
    -->
    <target name="init-git-svn-revision-xml" if="svn.info.fail" unless="skip-revision">
        <exec append="false" output="REVISION.XML" executable="git" failifexecutionfails="false" resultproperty="git.svn.info.result">
            <arg value="log"/>
            <arg value="-1"/>
            <arg value="--grep=git-svn-id"/>
            <!--
            %B:  raw body (unwrapped subject and body)
            %n:  new line
            %ai: author date, ISO 8601 format
            -->
            <arg value="--pretty=format:%B%n%ai"/>
            <arg value="."/>
        </exec>
        <replaceregexp file="REVISION.XML" flags="s"
                       match=".*git-svn-id: [^@]*@([0-9]+).*(\d{4}-\d{2}-\d{2}.\d{2}\:\d{2}\:\d{2}\s*[+-]\d{2}:?\d{2})\s*$"
                       replace="&lt;info&gt;&lt;entry&gt;&lt;commit revision=&quot;\1&quot;&gt;&lt;date&gt;\2&lt;/date&gt;&lt;/commit&gt;&lt;/entry&gt;&lt;/info&gt;"/>
       <condition property="git.svn.fail">
           <not>
               <and>
                   <equals arg1="${git.svn.info.result}" arg2="0" />
                   <length file="REVISION.XML" when="greater" length="1" />
               </and>
           </not>
       </condition>
    </target>
    <!--
      ** Initializes the REVISION.XML file from git (w/o svn) information.
         Uses Unix date as revision number.
    -->
    <target name="init-git-revision-xml" if="git.svn.fail" unless="skip-revision">
        <exec append="false" output="REVISION.XML" executable="git" failifexecutionfails="false" resultproperty="git.info.result">
            <arg value="log"/>
            <arg value="-1"/>
            <arg value="--pretty=format:%at%n%ai"/>
            <arg value="."/>
        </exec>
        <replaceregexp file="REVISION.XML" flags="s"
                       match="\s*(\d*)\s+(\d{4}-\d{2}-\d{2}.\d{2}\:\d{2}\:\d{2}\s*[+-]\d{2}:?\d{2})\s*$"
                       replace="&lt;info&gt;&lt;entry&gt;&lt;commit revision=&quot;\1&quot;&gt;&lt;date&gt;\2&lt;/date&gt;&lt;/commit&gt;&lt;/entry&gt;&lt;/info&gt;"/>
       <condition property="git.fail">
           <not>
               <and>
                   <equals arg1="${git.info.result}" arg2="0" />
                   <length file="REVISION.XML" when="greater" length="1" />
               </and>
           </not>
       </condition>
    </target>
    <target name="init-revision-fallback" if="git.fail" unless="skip-revision">
        <tstamp>
            <format property="current.time" pattern="yyyy-MM-dd'T'HH:mm:ss.SSS" />
        </tstamp>
        <echo file="REVISION.XML"><![CDATA[<info><entry><commit revision="UNKNOWN"><date>${current.time}</date></commit></entry></info>]]></echo>
    </target>
    <target name="revision" depends="init-svn-revision-xml, init-git-svn-revision-xml, init-git-revision-xml, init-revision-fallback" unless="skip-revision">
      <xmlproperty file="REVISION.XML" prefix="version" keepRoot="false" collapseAttributes="true"/>
      <delete file="REVISION.XML"/>
    </target>
    <!--
    **********************************************************
    ** clean - clean up the build environment
    **********************************************************
    -->
    <target name="clean">
        <delete dir="${plugin.build.dir}"/>
        <delete dir="${plugin.doc.dir}"/>
        <delete file="${plugin.jar}"/>
        <delete file="${plugin.sources.jar}"/>
        <delete file="${plugin.javadoc.jar}"/>
    </target>
    <!--
    **********************************************************
    ** install - install the plugin in your local JOSM installation
    **********************************************************
    -->
    <target name="install" depends="dist">
        <property environment="env"/>
        <condition property="josm.plugins.dir" value="${env.APPDATA}/JOSM/plugins">
            <and>
                <os family="windows"/>
            </and>
        </condition>
        <condition property="josm.plugins.dir" value="${user.home}/Library/JOSM/plugins">
            <and>
                <os family="mac"/>
            </and>
        </condition>
        <condition property="josm.plugins.dir" value="${user.home}/.local/share/JOSM/plugins">
            <and>
                <not><os family="windows"/></not>
                <not><os family="mac"/></not>
            </and>
        </condition>
        <copy file="${plugin.jar}" todir="${josm.plugins.dir}"/>
    </target>
    <!--
    ************************** Publishing the plugin ***********************************
    -->
    <!--
    ** extracts the JOSM release for the JOSM version in ../core and saves it in the
    ** property ${coreversion.info.entry.revision}
    **
    -->
    <target name="core-info">
        <exec append="false" output="core.info.xml" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="info"/>
            <arg value="--xml"/>
            <arg value="../../core"/>
        </exec>
        <xmlproperty file="core.info.xml" prefix="coreversion" keepRoot="true" collapseAttributes="true"/>
        <echo>Building against core revision ${coreversion.info.entry.revision}.</echo>
        <echo>Plugin-Mainversion is set to ${plugin.main.version}.</echo>
        <delete file="core.info.xml"/>
    </target>
    <!--
    ** commits the source tree for this plugin
    -->
    <target name="commit-current">
        <echo>Commiting the plugin source with message '${commit.message}' ...</echo>
        <exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="commit"/>
            <arg value="-m"/>
            <arg value="${commit.message}"/>
            <arg value="."/>
        </exec>
    </target>
    <!--
    ** updates (svn up) the source tree for this plugin
    -->
    <target name="update-current">
        <echo>Updating plugin source ...</echo>
        <exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="up"/>
            <arg value="."/>
        </exec>
        <echo>Updating ${plugin.jar} ...</echo>
        <exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="up"/>
            <arg value="${plugin.jar}"/>
        </exec>
    </target>
    <!--
    ** commits the plugin.jar
    -->
    <target name="commit-dist">
        <echo>
    ***** Properties of published ${plugin.jar} *****
    Commit message    : '${commit.message}'
    Plugin-Mainversion: ${plugin.main.version}
    JOSM build version: ${coreversion.info.entry.revision}
    Plugin-Version    : ${version.entry.commit.revision}
    ***** / Properties of published ${plugin.jar} *****

    Now committing ${plugin.jar} ...
    </echo>
        <exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="-m"/>
            <arg value="${commit.message}"/>
            <arg value="commit"/>
            <arg value="${plugin.jar}"/>
        </exec>
    </target>
    <!-- ** make sure svn is present as a command line tool ** -->
    <target name="ensure-svn-present">
        <exec append="true" output="svn.log" executable="svn" failifexecutionfails="false" failonerror="false" resultproperty="svn.exit.code">
            <env key="LANG" value="C"/>
            <arg value="--version"/>
        </exec>
        <fail message="Fatal: command 'svn --version' failed. Please make sure svn is installed on your system.">
            <!-- return code not set at all? Most likely svn isn't installed -->
            <condition>
                <not>
                    <isset property="svn.exit.code"/>
                </not>
            </condition>
        </fail>
        <fail message="Fatal: command 'svn --version' failed. Please make sure a working copy of svn is installed on your system.">
            <!-- error code from SVN? Most likely svn is not what we are looking on this system -->
            <condition>
                <isfailure code="${svn.exit.code}"/>
            </condition>
        </fail>
    </target>

    <target name="publish" depends="ensure-svn-present,core-info,commit-current,update-current,clean,dist,commit-dist">
    </target>

    <macrodef name="init-test-preferences">
        <sequential>
            <copy file="../00_core_test_config/preferences.template.xml" tofile="../00_core_test_config/unit-josm.home/preferences.xml"/>
            <replace file="../00_core_test_config/unit-josm.home/preferences.xml" encoding="UTF-8" token="@OSM_USERNAME@" value="${osm.username}"/>
            <replace file="../00_core_test_config/unit-josm.home/preferences.xml" encoding="UTF-8" token="@OSM_PASSWORD@" value="${osm.password}"/>
        </sequential>
    </macrodef>
    <target name="check-test">
        <available file="${plugin.test.dir}" type="dir" property="test.present"/>
    </target>
    <target name="test-init" depends="check-test" if="test.present" unless="skip-test">
        <mkdir dir="${plugin.test.dir}/build"/>
        <mkdir dir="${plugin.test.dir}/build/unit"/>
        <mkdir dir="${plugin.test.dir}/report"/>
        <init-test-preferences/>
        <ivy:settings file="${josm.ivysettings}" id="ivy.core.settings"/>
        <ivy:resolve settingsRef="ivy.core.settings" file="${josm.ivy}" conf="test,jacocoant"/>
        <!-- The following ivy:retrieve calls are for IDEs without Ivy support; since we don't have sync=true, these may not have the "latest" versions -->
        <!--
        <ivy:retrieve settingsRef="ivy.core.settings" pattern="../00_core_test_lib/[artifact].[ext]" conf="test"/>
        <ivy:retrieve settingsRef="ivy.core.settings" pattern="../00_core_tools/[conf].[ext]" conf="jacocoant"/>
        -->
        <ivy:cachepath settingsRef="ivy.core.settings" pathid=".local.test.classpath" conf="test" />
        <ivy:cachepath settingsRef="ivy.core.settings" pathid="jacocotest.classpath" conf="test,jacocoant"/>
        <ivy:cachepath settingsRef="ivy.core.settings" pathid="jmockit.classpath" file="${josm.ivy}" conf="jmockit" transitive="false"/>
        <ivy:cachepath settingsRef="ivy.core.settings" pathid="jacocoant.classpath" file="${josm.ivy}" conf="jacocoant"/>
        <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpath="${toString:jacocoant.classpath}"/>
        <path id="test.classpath">
            <!-- JMockit must be included before JUnit in the classpath -->
            <path refid="jmockit.classpath"/>
            <path refid="jacocotest.classpath"/>
            <path refid=".local.test.classpath"/>
            <fileset dir="${plugin.test.dir}/lib" erroronmissingdir="no">
                <include name="**/*.jar"/>
                <exclude name="**/*-sources.jar"/>
                <exclude name="**/*-javadoc.jar"/>
            </fileset>
            <fileset dir="lib" erroronmissingdir="no">
                <include name="**/*.jar"/>
                <exclude name="**/*-sources.jar"/>
                <exclude name="**/*-javadoc.jar"/>
            </fileset>
            <pathelement path="${plugin.resources.dir}"/>
            <pathelement path="${plugin.test.dir}/data"/>
            <pathelement path="${josm.test.build.dir}/unit"/>
            <pathelement path="${josm}"/>
            <pathelement path="${plugin.jar}"/>
        </path>
    </target>
    <target name="test-clean">
        <delete dir="${plugin.test.dir}/build"/>
        <delete dir="${plugin.test.dir}/report"/>
        <delete file="${plugin.test.dir}/jacoco.exec" />
        <delete file="../00_core_test_config/unit-josm.home/preferences.xml" />
        <delete dir="../00_core_test_config/unit-josm.home/cache" failonerror="false"/>
    </target>
    <target name="test-compile" depends="test-init,dist,plugin-classpath-actual" if="test.present" unless="skip-test">
        <sequential>
            <javac debug="on" includeantruntime="false" srcdir="${plugin.test.dir}/unit" destdir="${plugin.test.dir}/build/unit" encoding="UTF-8"
                release="${java.lang.version}">
                <classpath>
                    <fileset refid="plugin.requires.jars"/>
                    <path refid="test.classpath"/>
                    <path refid="plugin.classpath.actual"/>
                </classpath>
                <compilerarg value="-Xlint:all"/>
                <compilerarg value="-Xlint:-serial"/>
            </javac>
        </sequential>
    </target>
    <target name="test" depends="dist, test-clean, test-compile" if="test.present" unless="skip-test"
        description="Run unit tests. OSM API (TEST) account shall be set with -Dosm.username and -Dosm.password">
        <sequential>
            <echo message="Running unit tests with JUnit"/>
            <jacoco:agent destfile="${plugin.test.dir}/jacoco.exec" enabled="${coverageByDefault}"
                          inclbootstrapclasses="${jacoco.inclbootstrapclasses}" inclnolocationclasses="${jacoco.inclnolocationclasses}"
                          property="jacocoagent" if:true="${coverageByDefault}"/>
            <junitlauncher printsummary="${junit.printsummary}">
                <classpath>
                    <fileset refid="plugin.requires.jars"/>
                    <path refid="test.classpath"/>
                    <pathelement path="${plugin.test.dir}/build/unit"/>
                    <path refid="plugin.classpath.actual"/>
                </classpath>
                <testclasses outputDir="${plugin.test.dir}/report">
                    <fileset dir="${plugin.test.dir}/build/unit" includes="**/*Test.class"/>
	                  <fork>
                        <jvmarg value="${jacocoagent}" if:set="jacocoagent" />
                        <jvmarg value="-Dfile.encoding=UTF-8"/>
                        <jvmarg value="-javaagent:${toString:jmockit.classpath}"/>
                        <jvmarg value="-Djunit.jupiter.extensions.autodetection.enabled=true"/>
                        <jvmarg value="-Djunit.jupiter.execution.parallel.enabled=true"/>
                        <jvmarg value="--add-opens" />
                        <jvmarg value="java.base/java.lang.reflect=ALL-UNNAMED" />
                        <jvmarg value="--add-opens" />
                        <jvmarg value="java.desktop/javax.imageio.spi=ALL-UNNAMED" />
                        <jvmarg value="--add-exports" />
                        <jvmarg value="java.desktop/com.sun.imageio.spi=ALL-UNNAMED"  />
                        <jvmarg value="-XX:+ShowCodeDetailsInExceptionMessages" if:set="isJava14" unless:set="isJava15" />
                        <jvmarg value="--enable-native-access=ALL-UNNAMED" if:set="isJava17" />
                        <sysproperty key="josm.home" value="../00_core_test_config/unit-josm.home"/>
                        <sysproperty key="josm.test.data" value="${plugin.test.dir}/data"/>
                        <sysproperty key="java.awt.headless" value="true"/>
                        <sysproperty key="suppressPermanentFailure" value="${suppressPermanentFailure}"/>
                        <sysproperty key="junit.jupiter.execution.parallel.enabled" value="${junit.jupiter.execution.parallel.enabled}" if:set="junit.jupiter.execution.parallel.enabled"/>
                        <sysproperty key="junit.jupiter.execution.parallel.mode.default" value="${junit.jupiter.execution.parallel.mode.default}" if:set="junit.jupiter.execution.parallel.mode.default"/>
                        <sysproperty key="junit.jupiter.execution.parallel.mode.classes.default" value="${junit.jupiter.execution.parallel.mode.classes.default}" if:set="junit.jupiter.execution.parallel.mode.classes.default"/>
                    </fork>
                    <listener type="legacy-plain" />
                    <listener type="legacy-xml" />
                </testclasses>
            </junitlauncher>
        </sequential>
    </target>

    <target name="checkstyle" depends="resolve-tools">
        <taskdef resource="com/puppycrawl/tools/checkstyle/ant/checkstyle-ant-task.properties">
            <classpath refid="checkstyle.classpath"/>
        </taskdef>
        <checkstyle config="${basedir}/../checkstyle-config.xml">
            <fileset dir="${basedir}/src" includes="**/*.java" excludes="boofcv/**/*.java,
                                                                         com/google/**/*.java,
                                                                         crosby/**/*.java,
                                                                         edu/princeton/**/*.java,
                                                                         net/boplicity/**/*.java,
                                                                         org/apache/**/*.java,
                                                                         org/dinopolis/**/*.java,
                                                                         org/kaintoch/**/*.java,
                                                                         org/marvinproject/**/*.java,
                                                                         org/netbeans/**/*.java,
                                                                         org/openstreetmap/josm/plugins/dataimport/io/tcx/**/*.java,
                                                                         org/openstreetmap/josm/plugins/ohe/parser/**/*.java,
                                                                         org/openstreetmap/josm/plugins/pdfimport/pdfbox/operators/**/*.java
                                                                         org/openstreetmap/josm/plugins/roadsigns/javacc/**/*.java,
                                                                         org/osgeo/**/*.java,
                                                                         "/>
            <fileset dir="${basedir}/test" includes="**/*.java" erroronmissingdir="false"/>
            <formatter type="plain"/>
            <formatter type="xml" toFile="checkstyle-josm-${ant.project.name}.xml"/>
        </checkstyle>
    </target>

    <target name="spotbugs" depends="compile,resolve-tools">
        <taskdef name="spotbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="${toString:spotbugs.classpath}"/>
        <spotbugs output="xml"
                outputFile="spotbugs-josm-${ant.project.name}.xml"
                classpath="${toString:spotbugs.classpath}"
                pluginList=""
                excludeFilter="../spotbugs-filter.xml"
                effort="less"
                reportLevel="low"
                nested="false"
                jvmargs="-Xmx1024m"
                >
            <auxClasspath refid="plugin.classpath" unless:set="plugin.classpath.dependencies"/>
            <auxClasspath refid="plugin.classpath.dependencies" if:set="plugin.classpath.dependencies"/>
            <sourcePath path="${basedir}/src" />
            <class location="${plugin.build.dir}" />
        </spotbugs>
    </target>

    <target name="runjosm" depends="install">
        <java jar="${josm}" fork="true"/>
    </target>

    <target name="profilejosm" depends="install">
        <nbprofiledirect>
        </nbprofiledirect>
        <java jar="${josm}" fork="true">
            <jvmarg value="${profiler.info.jvmargs.agent}"/>
        </java>
    </target>
    <!--
    ** shows a help text
    -->
    <target name="help">
        <echo>
    You can use following targets:
    * dist        This default target builds the plugin jar file
    * clean       Cleanup automatically created files
    * test        Run unit tests (if any)
    * publish     Checkin source code, build jar and checkin plugin jar
                  (requires proper entry for SVN commit message!)
    * install     Install the plugin in current system
    * runjosm     Install plugin and start josm
    * profilejosm Install plugin and start josm in profiling mode

    There are other targets, which usually should not be called manually.
        </echo>
    </target>
    <!--
    ** Ivy tasks
    -->
    <target name="is-ivy">
        <condition property="isIvy">
            <available file="ivy.xml"/>
        </condition>
    </target>
    <target name="download-ivy">
        <mkdir dir="${ivy.jar.dir}"/>
        <get src="https://josm.openstreetmap.de/repository/public/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"
             dest="${ivy.jar.file}"
             usetimestamp="true"/>
    </target>
    <target name="init-ivy" depends="download-ivy">
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    </target>
    <target name="resolve-tools" depends="init-ivy" description="Resolves tools using Apache Ivy">
        <ivy:settings file="${josm.ivysettings}"/>
        <ivy:resolve file="${core.tools.ivy}"/>
        <ivy:cachepath file="${core.tools.ivy}" pathid="errorprone.classpath" conf="errorprone"/>
        <ivy:cachepath file="${core.tools.ivy}" pathid="errorprone_javac.classpath" conf="errorprone_javac"/>
        <ivy:cachepath file="${core.tools.ivy}" pathid="checkstyle.classpath" conf="checkstyle"/>
        <ivy:cachepath file="${core.tools.ivy}" pathid="spotbugs.classpath" conf="spotbugs"/>
    </target>
    <target name="clean_ivy">
        <delete failonerror="false">
            <fileset dir="${plugin.lib.dir}">
                <include name="**/*.jar"/>
                <exclude name="**/*-custom.jar" />
            </fileset>
        </delete>
    </target>
    <target name="fetch-dependencies-ivy" depends="clean_ivy, init-ivy, is-ivy" if="isIvy">
        <echo>fetching dependencies with ivy</echo>
        <available property="plugin.ivysettings.exists" file="ivy_settings.xml"/>
        <ivy:settings file="ivy_settings.xml" if:set="plugin.ivysettings.exists"/>
        <ivy:settings file="${josm.ivysettings}" unless:set="plugin.ivysettings.exists"/>
        <ivy:resolve />
        <ivy:retrieve pattern="${plugin.lib.dir}/[artifact]-[revision](-[classifier]).[ext]" conf="default" />
    </target>
    <target name="ivy-checkdepsupdate" description="Display dependency updates on the console" depends="fetch-dependencies-ivy">
        <ivy:checkdepsupdate/>
    </target>
    <!--
    ** Maven tasks
    -->
    <target name="is-maven" depends="is-ivy">
        <condition property="isMaven">
            <and>
                <available file="pom.xml"/>
                <not>
                    <isset property="isIvy"/>
                </not>
            </and>
        </condition>
    </target>
    <target name="download-maven" depends="is-maven" if="isMaven">
        <mkdir dir="${maven.artifact.resolver.parent.dir}"/>
        <get src="https://josm.openstreetmap.de/repository/public/org/apache/maven/resolver/maven-resolver-ant-tasks/${maven.artifact.resolver.version}/maven-resolver-ant-tasks-${maven.artifact.resolver.version}-uber.jar"
             dest="${maven.artifact.resolver.jar.file}"
             usetimestamp="true"/>
    </target>
    <target name="init-maven" depends="is-maven,download-maven" if="isMaven">
        <path id="maven.lib.path">
            <fileset dir="${maven.artifact.resolver.parent.dir}" includes="*.jar"/>
        </path>
        <taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml" classpathref="maven.lib.path"/>
    </target>
    <target name="fetch-dependencies-maven" depends="is-maven,init-maven,clean_ivy" if="isMaven">
        <mvn:pom file="pom.xml"/>
        <mvn:pom file="pom.xml" id="pom"/>
        <!-- This stanza is necessary since the maven resolver doesn't read repo settings from the pom.xml file -->
        <!-- resolver.repositories makes it global -->
        <mvn:remoterepos id="resolver.repositories">
            <mvn:remoterepo id="JOSM" url="https://josm.openstreetmap.de/repository/public/" />
            <mvn:remoterepo id="JOSM-snapshots" url="https://josm.openstreetmap.de/repository/snapshots/" snapshots="true" />
        </mvn:remoterepos>
        <mkdir dir="${plugin.lib.dir}"/>
        <mvn:resolve>
            <!-- For some reason, compile time josm-unittest dependencies get included -->
            <mvn:dependencies pomRef="pom">
                <mvn:exclusion groupId="org.openstreetmap.josm" artifactId="josm-unittest"/>
                <mvn:exclusion groupId="org.junit.platform"/>
                <mvn:exclusion groupId="org.junit.vintage"/>
                <mvn:exclusion groupId="org.junit.jupiter"/>
            </mvn:dependencies>
            <mvn:files refid="lib.files" dir="${plugin.lib.dir}" layout="{artifactId}-{version}-{classifier}.{extension}" scopes="compile,!test"/>
        </mvn:resolve>
        <mvn:resolve>
            <mvn:path refid="classpath.provided" scopes="provided"/>
        </mvn:resolve>
        <mvn:resolve>
            <mvn:path refid="testlib.classpath" classpath="test"/>
        </mvn:resolve>
        <mvn:resolve>
            <mvn:dependencies>
                <mvn:dependency groupId="org.jacoco" artifactId="org.jacoco.ant" version="${pom.properties.jacoco.version}" classifier="nodeps" type="jar" scope="test"/>
            </mvn:dependencies>
            <mvn:path refid="jacocotest.classpath" classpath="test"/>
        </mvn:resolve>
        <mvn:resolve>
            <mvn:dependencies>
                <mvn:dependency groupId="com.puppycrawl.tools" artifactId="checkstyle" version="${pom.properties.checkstyle.version}" scope="compile"/>
            </mvn:dependencies>
            <mvn:path refid="checkstyle.classpath" classpath="compile"/>
        </mvn:resolve>
        <mvn:resolve>
            <mvn:dependencies>
                <mvn:dependency groupId="com.github.spotbugs" artifactId="spotbugs" version="${pom.properties.spotbugs.version}" scope="compile"/>
                <mvn:dependency groupId="com.github.spotbugs" artifactId="spotbugs-ant" version="${pom.properties.spotbugs.version}" scope="compile"/>
            </mvn:dependencies>
            <mvn:path refid="spotbugs.classpath" classpath="compile"/>
        </mvn:resolve>
        <condition property="classpath.provided.present">
            <isreference refid="classpath.provided" type="path"/>
        </condition>
        <path id="plugin.classpath.actual" unless:set="classpath.provided.present">
            <path refid="plugin.classpath"/>
        </path>
        <path id="plugin.classpath.actual" if:set="classpath.provided.present">
            <path refid="plugin.classpath"/>
            <path refid="classpath.provided"/>
        </path>
    </target>

    <target name="fetch_dependencies" depends="fetch-dependencies-ivy,fetch-dependencies-maven">
      <!-- We can depend upon ivy and/or maven right now. -->
    </target>
</project>
