<?xml version="1.0" encoding="utf-8"?>
<!-- ** build.xml - main ant file for JOSM
**
** To build run
**    ant clean
**    ant dist
** This will create 'josm-custom.jar'  in directory 'dist'. See also
**   https://josm.openstreetmap.de/wiki/CreateBuild
**
-->
<project xmlns:as="antlib:org.codehaus.mojo.animal_sniffer" name="josm" default="dist" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
    <property name="test.dir" location="test"/>
    <property name="src.dir" location="src"/>
    <property name="build.dir" location="build"/>
    <property name="javacc.home" location="tools"/>
    <property name="mapcss.dir" location="${src.dir}/org/openstreetmap/josm/gui/mappaint/mapcss"/>
    <!-- build parameter: compression level (ant -Dclevel=N)
             N ranges from 0 (no compression) to 9 (maximum compression)
             default: 9 -->
    <condition property="clevel" value="${clevel}" else="9">
        <isset property="clevel"/>
    </condition>
    <!-- Java classpath addition (all jar files to compile tests with this) -->
    <path id="classpath">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <!--
      ** Used by Eclipse ant builder for updating
      ** the REVISION file used by JOSM
    -->
    <target name="create-revision-eclipse">
        <property name="revision.dir" value="bin"/>
        <antcall target="create-revision"/>
    </target>
    <!--
      ** Creates the REVISION file to be included in the distribution
    -->
    <target name="create-revision">
        <property name="revision.dir" value="${build.dir}"/>
        <exec append="false" output="REVISION.XML" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="info"/>
            <arg value="--xml"/>
            <arg value="."/>
        </exec>
        <xmlproperty file="REVISION.XML" prefix="version" keepRoot="false" collapseAttributes="true"/>
        <delete file="REVISION.XML"/>
        <tstamp>
            <format property="build.tstamp" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>
        <property name="version.entry.commit.revision" value="UNKNOWN"/>
        <mkdir dir="${revision.dir}"/>
        <!-- add Build-Name: ... when making special builds, e.g. DEBIAN -->
        <echo file="${revision.dir}/REVISION">
# automatically generated by JOSM build.xml - do not edit
Revision: ${version.entry.commit.revision}
Is-Local-Build: true
Build-Date: ${build.tstamp}
</echo>
    </target>
    <target name="check-schemas">
        <schemavalidate file="data/defaultpresets.xml" >
            <schema namespace="http://josm.openstreetmap.de/tagging-preset-1.0" file="data/tagging-preset.xsd" />
        </schemavalidate>
        <schemavalidate file="styles/standard/elemstyles.xml" >
            <schema namespace="http://josm.openstreetmap.de/mappaint-style-1.0" file="data/mappaint-style.xsd" />
        </schemavalidate>
    </target>
    <target name="dist" depends="compile,create-revision,check-schemas">
        <exec append="false" output="REVISION" executable="svn" failifexecutionfails="false">
            <env key="LANG" value="C"/>
            <arg value="info"/>
            <arg value="--xml"/>
            <arg value="."/>
        </exec>
        <xmlproperty file="REVISION" prefix="version" keepRoot="false" collapseAttributes="true"/>
        <delete file="REVISION"/>
        <property name="version.entry.commit.revision" value="UNKNOWN"/>
        <property name="version.entry.commit.date" value="UNKNOWN"/>
        <echo>Revision ${version.entry.commit.revision}</echo>
        <copy file="CONTRIBUTION" todir="build"/>
        <copy file="README" todir="build"/>
        <copy file="LICENSE" todir="build"/>
        <!-- create josm-custom.jar -->
        <delete file="dist/josm-custom.jar"/>
        <jar destfile="dist/josm-custom.jar" basedir="build" level="${clevel}">
            <!-- add attribute excludes="**/*BZip2*,**/*Bzip2*" to create a non-bzip2 supporting jar -->
            <manifest>
                <attribute name="Main-class" value="JOSM"/>
                <attribute name="Main-Version" value="${version.entry.commit.revision} SVN"/>
                <attribute name="Main-Date" value="${version.entry.commit.date}"/>
                <attribute name="Permissions" value="all-permissions"/>
                <attribute name="Codebase" value="josm.openstreetmap.de"/>
                <attribute name="Application-Name" value="JOSM - Java OpenStreetMap Editor"/>
            </manifest>
            <zipfileset dir="images" prefix="images"/>
            <zipfileset dir="data" prefix="data"/>
            <zipfileset dir="styles" prefix="styles"/>
            <zipfileset dir="src/org/openstreetmap/gui/jmapviewer/images" prefix="org/openstreetmap/gui/jmapviewer/images"/>
            <!-- All jar files necessary to run only JOSM (no tests) -->
            <!-- <zipfileset src="lib/metadata-extractor-2.3.1-nosun.jar"/>  -->
            <!-- <zipfileset src="lib/signpost-core-1.2.1.1.jar"/> -->
        </jar>
    </target>
    <!-- Compatibility Mac OS X target for Java 6 (incompatible with new on for Java 7, see #8654, #9035) -->
    <target name="distmac" depends="dist">
        <!-- modify MacOS X Info.plist file to hold the SVN version number -->
        <copy file="macosx/JOSM.app/Contents/Info.plist" todir="build"/>
        <replace file="build/Info.plist" token="@SVNVersion@" value="${version.entry.commit.revision}"/>
        <!-- create ZIP file with MacOS X application bundle -->
        <zip destfile="dist/josm-custom-macosx.zip" update="true">
            <zipfileset dir="build" includes="CONTRIBUTION README LICENSE"/>
            <zipfileset dir="macosx" includes="JOSM.app/Contents JOSM.app/Contents/MacOS JOSM.app/Contents/Resources JOSM.app/Contents/Resources/Java JOSM.app/Contents/PkgInfo JOSM.app/Contents/Resources/JOSM.icns"/>
            <zipfileset dir="build" includes="Info.plist" prefix="JOSM.app/Contents"/>
            <zipfileset dir="dist" includes="josm-custom.jar" prefix="JOSM.app/Contents/Resources/Java"/>
            <zipfileset dir="macosx" includes="JOSM.app/Contents/MacOS/JOSM" filemode="755"/>
        </zip>
    </target>
    <!-- New Mac OS X target for Java 7 -->
    <target name="distmac7" depends="dist">
        <!-- Using https://bitbucket.org/infinitekind/appbundler to create mac application bundle -->
        <taskdef name="bundleapp" classname="com.oracle.appbundler.AppBundlerTask" classpath="tools/appbundler-1.0ea.jar"/>
        <!-- create MacOS X application bundle -->
        <bundleapp outputdirectory="dist" name="JOSM" displayname="JOSM" executablename="JOSM" identifier="org.openstreetmap.josm"
                   mainclassname="org.openstreetmap.josm.gui.MainApplication"
                   copyright="JOSM, and all its integral parts, are released under the GNU General Public License v2 or later"
                   applicationCategory="public.app-category.utilities"
                   shortversion="${version.entry.commit.revision} SVN"
                   version="${version.entry.commit.revision} SVN"
                   icon="macosx/JOSM.app/Contents/Resources/JOSM.icns"
                   highResolutionCapable="true">

            <arch name="x86_64"/>
            <arch name="i386"/>

            <classpath file="dist/josm-custom.jar"/>

            <option value="-Xmx512m"/>

            <option value="-Xdock:icon=Contents/Resources/JOSM.icns"/>
            <option value="-Xdock:name=JOSM"/>

            <!-- OSX specific options, optional -->
            <option value="-Dapple.laf.useScreenMenuBar=true"/>
            <option value="-Dcom.apple.macos.use-file-dialog-packages=true"/>
            <option value="-Dcom.apple.macos.useScreenMenuBar=true"/>
            <option value="-Dcom.apple.mrj.application.apple.menu.about.name=JOSM"/>
            <option value="-Dcom.apple.smallTabs=true"/>
        </bundleapp>
        
        <!-- appbundler lacks the possibility of defining our own keys or using a template, so update the .plist manually -->
        <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="tools/xmltask.jar"/>
        
        <xmltask source="dist/JOSM.app/Contents/Info.plist" dest="dist/JOSM.app/Contents/Info.plist" indent="false">
            <insert position="before" path="/plist/dict/key[1]"><![CDATA[<key>CFBundleAllowMixedLocalizations</key>
<string>true</string>
]]></insert>
        </xmltask>
        
        <!-- create ZIP file with MacOS X application bundle -->
        <zip destfile="dist/josm-custom-macosx-java7.zip" update="true">
            <zipfileset dir="build" includes="CONTRIBUTION README LICENSE"/>
            <zipfileset dir="dist" includes="JOSM.app/**/*" filemode="755" />
        </zip>
    </target>
    <target name="javacc" depends="init" unless="javacc.notRequired">
        <mkdir dir="${mapcss.dir}/parsergen"/>
        <exec append="false" executable="java" failifexecutionfails="true">
            <arg value="-cp"/>
            <arg value="${javacc.home}/javacc.jar"/>
            <arg value="javacc"/>
            <arg value="-OUTPUT_DIRECTORY=${mapcss.dir}/parsergen"/>
            <arg value="${mapcss.dir}/MapCSSParser.jj"/>
        </exec>
<!--        <javacc target="${mapcss.dir}/MapCSSParser.jj" javacchome="${javacc.home}" outputdirectory="${mapcss.dir}/parsergen"/>-->
    </target>
    <target name="compile" depends="init,javacc">
        <javac srcdir="src" includes="com/**,oauth/**,org/apache/commons/codec/**" destdir="build" target="1.6" source="1.6" debug="on" includeantruntime="false" encoding="iso-8859-1"/>
        <javac srcdir="src" excludes="com/**,oauth/**,org/apache/commons/codec/**" destdir="build" target="1.6" source="1.6" debug="on" includeantruntime="false" encoding="UTF-8">
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-Xlint:unchecked"/>
        </javac>
    </target>
    <target name="init">
        <uptodate property="javacc.notRequired" targetfile="${mapcss.dir}/parsergen/MapCSSParser.java" >
            <srcfiles dir="${mapcss.dir}" includes="MapCSSParser.jj"/>
        </uptodate>
        <mkdir dir="build"/>
        <mkdir dir="dist"/>
    </target>
    <target name="javadoc">
        <javadoc destdir="javadoc" 
                sourcepath="src"
                packagenames="org.openstreetmap.josm.*,org.openstreetmap.gui.jmapviewer.*"
                windowtitle="JOSM"
                use="true"
                private="true"
                linksource="true"
                author="false">
            <link href="http://docs.oracle.com/javase/6/docs/api"/>
            <doctitle><![CDATA[<h2>JOSM - Javadoc</h2>]]></doctitle>
            <bottom><![CDATA[<a href="http://josm.openstreetmap.de/">JOSM</a>]]></bottom>
        </javadoc>
    </target>
    <target name="clean">
        <delete dir="build"/>
        <delete dir="dist"/>
        <delete dir="${mapcss.dir}/parsergen"/>
    </target>
    <path id="test.classpath">
        <fileset dir="${test.dir}/lib">
            <include name="**/*.jar"/>
        </fileset>
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="dist/josm-custom.jar"/>
    </path>
    <target name="test-init">
        <mkdir dir="${test.dir}/build"/>
        <mkdir dir="${test.dir}/report"/>
    </target>
    <target name="test-clean">
        <delete dir="${test.dir}/build"/>
        <delete dir="${test.dir}/report"/>
        <delete file="${test.dir}/jacoco.exec" />
    </target>
    <target name="test-compile" depends="test-init,dist">
        <javac srcdir="${test.dir}/unit" classpathref="test.classpath" destdir="${test.dir}/build" target="1.6" source="1.6" debug="on" includeantruntime="false" encoding="UTF-8">
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-Xlint:unchecked"/>
        </javac>
        <javac srcdir="${test.dir}/functional" classpathref="test.classpath" destdir="${test.dir}/build" target="1.6" source="1.6" debug="on" includeantruntime="false" encoding="UTF-8">
            <compilerarg value="-Xlint:deprecation"/>
            <compilerarg value="-Xlint:unchecked"/>
        </javac>
    </target>
    <target name="test" depends="test-compile">
        <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpath="tools/jacocoant.jar" />
        <jacoco:coverage destfile="${test.dir}/jacoco.exec">
            <junit printsummary="yes" fork="true" forkmode="once">
                <sysproperty key="josm.home" value="${test.dir}/config/unit-josm.home"/>
                <sysproperty key="josm.test.data" value="${test.dir}/data"/>
                <sysproperty key="java.awt.headless" value="true"/>
                <sysproperty key="suppressPermanentFailure" value="${suppressPermanentFailure}"/>
                <classpath>
                    <path refid="test.classpath"/>
                    <pathelement path="${test.dir}/build"/>
                    <pathelement path="${test.dir}/config"/>
                </classpath>
                <formatter type="plain"/>
                <formatter type="xml"/>
                <batchtest fork="yes" todir="${test.dir}/report">
                    <fileset dir="${test.dir}/unit" includes="**/*.java"/>
                </batchtest>
            </junit>
        </jacoco:coverage>
    </target>
    <target name="test-html" depends="test" description="Generate HTML test reports">
        <!-- May require additional ant dependencies like ant-trax package -->
        <junitreport todir="${test.dir}/report">
            <fileset dir="${test.dir}/report">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${test.dir}/report/html"/>
        </junitreport>
        <jacoco:report>
            <executiondata>
                <file file="${test.dir}/jacoco.exec"/>
            </executiondata>
            <structure name="JOSM Test Coverage">
                <classfiles>
                    <fileset dir="${build.dir}" includes="org/openstreetmap/"/>
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${src.dir}" includes="org/openstreetmap/"/>
                </sourcefiles>
            </structure>
            <html destdir="${test.dir}/report/jacoco"/>
        </jacoco:report>
    </target>
    <target name="dist-optimized" depends="dist">
        <taskdef resource="proguard/ant/task.properties" classpath="tools/proguard.jar"/>
        <proguard>
	 	-injars dist/josm-custom.jar
	 	-outjars dist/josm-custom-optimized.jar

	 	-libraryjars ${java.home}/lib/rt.jar
	 	-libraryjars ${java.home}/lib/jce.jar

	 	-dontoptimize
	 	-dontobfuscate

	 	# These options probably are not necessary (and make processing a bit slower)
	 	-dontskipnonpubliclibraryclasses
		-dontskipnonpubliclibraryclassmembers

	 	-keepclasseswithmembers public class org.openstreetmap.josm.gui.MainApplication {
	 	    public static void main(java.lang.String[]);
	 	}
		-keepclasseswithmembers public class org.openstreetmap.josm.gui.MainApplet

		-keep class JOSM
	 	-keep class * extends org.openstreetmap.josm.io.FileImporter
	 	-keep class * extends org.openstreetmap.josm.io.FileExporter
		-keep class org.openstreetmap.josm.data.imagery.types.Adapter1
		-keep class org.openstreetmap.josm.actions.search.SearchCompiler$Never

	 	-keepclassmembers enum  * {
	 	    public static **[] values();
	 	    public static ** valueOf(java.lang.String);
	 	}

	 	# Keep unused public methods (can be useful for plugins)
	 	-keepclassmembers class * {
	 	    public protected *;
	 	}

		# Disable annoying [proguard] Note: the configuration keeps the entry point '...', but not the descriptor class '...'. This notes should not be a problem as we don't use obfuscation
		-dontnote
        </proguard>
    </target>
    <target name="check-plugins" depends="dist-optimized">
        <echo message="Check of plugins binary compatibility (needs ant 1.8)"/>
        <local name="dir"/>
        <local name="plugins"/>
        <property name="dir" value="plugin-check"/>
        <typedef uri="antlib:org.codehaus.mojo.animal_sniffer">
            <classpath path="tools/animal-sniffer-ant-tasks-1.8.jar"/>
        </typedef>
        <mkdir dir="${dir}"/>
        <!-- List of deprecated plugins -->
        <loadfile property="deprecated-plugins" srcFile="src/org/openstreetmap/josm/plugins/PluginHandler.java">
            <filterchain>
                <linecontains>
                    <contains value="new DeprecatedPlugin("/>
                </linecontains>
                <tokenfilter>
                    <replaceregex pattern=".*new DeprecatedPlugin\(&quot;(.+?)&quot;.*" replace="\1|" flags="gi"/>
                </tokenfilter>
                <striplinebreaks/>
                <tokenfilter>
                    <replaceregex pattern="\|$" replace="" flags="gi"/>
                </tokenfilter>
            </filterchain>
        </loadfile>
        <!-- Download list of plugins -->
        <loadresource property="plugins">
            <url url="http://josm.openstreetmap.de/plugin"/>
            <filterchain>
                <linecontainsregexp negate="true">
                    <regexp pattern="^\t.*"/>
                </linecontainsregexp>
                <linecontainsregexp negate="true">
                    <regexp pattern="${deprecated-plugins}"/>
                </linecontainsregexp>
                <tokenfilter>
                    <replaceregex pattern="^.*;" replace="" flags="gi"/>
                </tokenfilter>
            </filterchain>
        </loadresource>
        <!-- Delete files that are not in plugin list (like old plugins) -->
        <loadresource property="file-list">
            <propertyresource name="plugins"/>
            <filterchain>
                <tokenfilter>
                    <replaceregex pattern="^.*/(.*)$" replace="\1\|" flags=""/>
                </tokenfilter>
                <striplinebreaks/>
                <tokenfilter>
                    <replaceregex pattern="\|$" replace="" flags="gi"/>
                </tokenfilter>    
            </filterchain>
        </loadresource>
        <delete>
            <restrict>
                <fileset dir="${dir}"/>
                <not>
                    <name regex="${file-list}"/>
                </not>
            </restrict>
        </delete>
        <!-- Download plugins -->
        <copy todir="${dir}" flatten="true">
            <resourcelist>
                <string value="${plugins}"/>
            </resourcelist>
        </copy>
        <!-- Check plugins -->
        <as:build-signatures destfile="${dir}/api.sig">
            <path>
                <fileset file="dist/josm-custom-optimized.jar"/>
                <fileset file="${java.home}/lib/rt.jar"/>
                <fileset file="${java.home}/lib/jce.jar"/>
            </path>
        </as:build-signatures>
        <as:check-signature signature="${dir}/api.sig">
            <ignore classname="org.jgraph.*"/>
            <ignore classname="com.touchgraph.*"/>
            <ignore classname="com.sun.xml.fastinfoset.*"/>
            <ignore classname="javax.jms.*"/>
            <ignore classname="org.jvnet.staxex.*"/>
            <ignore classname="javax.mail.*"/>
            <ignore classname="com.sun.jdmk.*"/>
            <ignore classname="org.apache.avalon.framework.logger.Logger"/>
            <ignore classname="org.apache.log.*"/>
            <ignore classname="junit.*"/>
            <path path="${dir}"/>
        </as:check-signature>
    </target>

    <target name="findbugs" depends="dist">
        <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="tools/findbugs/findbugs-ant.jar"/>
        <path id="findbugs-classpath">
            <fileset dir="tools/findbugs/">
                <include name="*.jar"/>
            </fileset>
        </path>
        <property name="findbugs-classpath" refid="findbugs-classpath"/>
        <findbugs output="xml"
                outputFile="findbugs-josm.xml"
                classpath="${findbugs-classpath}"
                pluginList=""
                excludeFilter="tools/findbugs/josm-filter.xml"
                effort="max"
                >
            <sourcePath path="${basedir}/src" />
            <class location="${basedir}/dist/josm-custom.jar" />
        </findbugs>
    </target>
    <target name="run" depends="dist">
        <java jar="dist/josm-custom.jar" fork="true">
            <arg value="--set=expert=true"/>
            <arg value="--set=remotecontrol.enabled=true"/>
            <arg value="--set=debug.edt-checker.enable=false"/>
            <jvmarg value="-Djosm.home=/tmp/.josm/"/>
        </java>
    </target>

</project>
