Skip to content

Instantly share code, notes, and snippets.

@RadAd
Last active April 2, 2023 14:29
Show Gist options
  • Save RadAd/b63a073011f53821647c to your computer and use it in GitHub Desktop.
Save RadAd/b63a073011f53821647c to your computer and use it in GitHub Desktop.
Ant build script for java projects.
set JAVA_HOME=%UTILS_DIR%\Devel\jdk1.7_x64
if not exist "%JAVA_HOME%" echo JAVA_HOME doesnt exist & exit /B 1
if not defined RAD_BUILD_HOME set RAD_BUILD_HOME=%DROPBOX_DIR%\Current\Development\Java\Build
set ANT_ARGS=-logger org.apache.tools.ant.listener.AnsiColorLogger
<project name="RadJava" basedir="." default="build"
xmlns:ivy="antlib:org.apache.ivy.ant" >
<property environment="env"/>
<dirname property="antfile.dir" file="${ant.file}"/>
<property name="project.displayname" value="${ant.project.name}"/>
<property name="project.vendor" value="RadSoft"/>
<condition property="install.dir" value="${env.RAD_INSTALL_DIR}">
<isset property="env.RAD_INSTALL_DIR"/>
</condition>
<property name="compile.lint" value="true"/>
<property name="compile.debug" value="true"/>
<property name="executable.javac" location="${env.JAVA_HOME}\bin\javac"/>
<property name="src.dir" location="src"/>
<property name="gen.dir" location="gen"/>
<property name="classes.dir" location="classes"/>
<property name="bin.dir" location="bin"/>
<property name="lib.dir" location="lib"/>
<property name="assets.dir" location="assets"/>
<property name="res.dir" location="res"/>
<condition property="jar.name" value="${bin.dir}/${ant.project.name}.jar">
<isset property="ant.project.name"/>
</condition>
<path id="build.classpath">
<fileset dir="${lib.dir}" includes="*.jar" erroronmissingdir="false"/>
</path>
<condition property="build.src" value="${src.dir};${gen.dir}" else="${src.dir}">
<available file="${gen.dir}" type="dir"/>
</condition>
<condition property="run.args" value="${run.args}" else="">
<isset property="run.args"/>
</condition>
<path id="manifest.classpath">
<fileset dir="${bin.dir}" includes="*.jar">
<exclude name="${ant.project.name}.jar"/>
<!-- TODO exclude named files -->
</fileset>
</path>
<target name="clean">
<delete dir="${classes.dir}"/>
</target>
<target name="realclean" depends="clean">
<delete dir="${bin.dir}"/>
</target>
<condition property="compile.lint.arg" value="-Xlint" else="">
<istrue value="${compile.lint}"/>
</condition>
<condition property="compile.processor.arg" value="-processor ${compile.processor}" else="">
<isset property="compile.processor"/>
</condition>
<target name="compile" description="compile the source">
<mkdir dir="${classes.dir}"/>
<tstamp/>
<javac
srcdir="${build.src}"
destdir="${classes.dir}"
includeantruntime="false"
classpathref="build.classpath"
executable="${executable.javac}"
debug="${compile.debug}"
fork="true">
<compilerarg value="${compile.lint.arg}"/>
<compilerarg line="${compile.processor.arg}"/>
</javac>
</target>
<target name="jar-pre">
<fail unless="jar.name">Property jar.name not set.</fail>
<fail unless="project.displayname">Property project.displayname not set.</fail>
<fail unless="project.version">Property project.version not set.</fail>
<fail unless="project.vendor">Property project.vendor not set.</fail>
<mkdir dir="${bin.dir}"/>
<copy todir="${bin.dir}" flatten="true">
<path refid="build.classpath"/>
</copy>
<manifestclasspath property="jar.classpath" jarfile="${jar.name}">
<classpath refid="manifest.classpath"/>
</manifestclasspath>
</target>
<target name="jar-main" if="project.mainclass">
<jar destfile="${jar.name}" basedir="${classes.dir}">
<fileset dir="${assets.dir}" erroronmissingdir="false"/>
<fileset dir="${res.dir}" erroronmissingdir="false"/>
<manifest>
<attribute name="Main-Class" value="${project.mainclass}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
<attribute name="Specification-Title" value="${project.displayname}"/>
<attribute name="Specification-Version" value="${project.version}"/>
<attribute name="Specification-Vendor" value="${project.vendor}"/>
<attribute name="Implementation-Title" value="${project.displayname}"/>
<attribute name="Implementation-Version" value="${project.version} ${TODAY}"/>
<attribute name="Implementation-Vendor" value="${project.vendor}"/>
<attribute name="Package-Title" value="${project.displayname}"/>
<attribute name="Package-Version" value="${project.version}"/>
<attribute name="Package-Vendor" value="${project.vendor}"/>
</manifest>
</jar>
</target>
<target name="jar-nomain" unless="project.mainclass">
<jar destfile="${jar.name}" basedir="${classes.dir}">
<fileset dir="${assets.dir}" erroronmissingdir="false"/>
<manifest>
<attribute name="Class-Path" value="${jar.classpath}"/>
<attribute name="Specification-Title" value="${project.displayname}"/>
<attribute name="Specification-Version" value="${project.version}"/>
<attribute name="Specification-Vendor" value="${project.vendor}"/>
<attribute name="Implementation-Title" value="${project.displayname}"/>
<attribute name="Implementation-Version" value="${project.version} ${TODAY}"/>
<attribute name="Implementation-Vendor" value="${project.vendor}"/>
<attribute name="Package-Title" value="${project.displayname}"/>
<attribute name="Package-Version" value="${project.version}"/>
<attribute name="Package-Vendor" value="${project.vendor}"/>
</manifest>
</jar>
</target>
<target name="jar" depends="compile,jar-pre,jar-main,jar-nomain" description="create the jar file">
</target>
<target name="build" depends="jar" description="build all targets">
</target>
<target name="buildall" depends="resolve,dep,build" description="build all targets and dependencies">
</target>
<target name="show" depends="jar-pre">
<echo message="Java home: ${env.JAVA_HOME}"/>
<echo message="Ant: ${ant.file}"/>
<echo message="Jar: ${jar.name}"/>
<echo message="Src: ${build.src}"/>
<echo message="Lint: ${compile.lint.arg}"/>
<echo message="Processor: ${compile.processor.arg}"/>
<echo message="Classpath1: ${toString:build.classpath}"/>
<echo message="Classpath2: ${toString:manifest.classpath}"/>
<echo message="Classpath4: ${jar.classpath}"/>
</target>
<target name="run" depends="jar" description="execute the jar file">
<java jar="${jar.name}" fork="true">
<arg line="${run.args}"/>
</java>
</target>
<target name="rebuild" depends="clean,build"/>
<target name="install" depends="jar" description="copies jar files to target directory">
<fail unless="install.dir">Property install.dir not set.</fail>
<copy todir="${install.dir}">
<fileset dir="${bin.dir}"/>
</copy>
</target>
<condition property="ivy.xml.exists">
<available file="ivy.xml" type="file"/>
</condition>
<target name="resolve" if="ivy.xml.exists" description="retrieve dependencies with ivy" depends="install-ivy">
<ivy:retrieve />
<ivy:checkdepsupdate showTransitive="false" revisionToCheck="latest.release"/>
</target>
<target name="dep" if="project.dep" description="build dependencies">
<subant target="install">
<property name="install.dir" value="${lib.dir}"/>
<filelist dir="${antfile.dir}" files="${project.dep}"/>
</subant>
</target>
<target name="depclean" if="project.dep" description="clean dependencies">
<subant target="clean">
<filelist dir="${antfile.dir}" files="${project.dep}"/>
</subant>
</target>
<target name="examples" description="build examples">
<subant>
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<target name="examplesall" description="build examples all">
<subant target="buildall">
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<target name="examplesclean" description="clean examples">
<subant target="clean">
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<target name="examplesresolve" description="build examples resolve">
<subant target="resolve">
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<target name="examplesdep" description="build examples dependencies">
<subant target="dep">
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<target name="examplesdepclean" description="clean examples dependencies">
<subant target="depclean">
<fileset dir="examples" includes="*/build.xml"/>
</subant>
</target>
<property name="ivy.install.version" value="2.4.0"/>
<property name="ivy.jar.dir" value="${basedir}/ivy"/>
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy-${ivy.install.version}.jar"/>
<target name="download-ivy" unless="skip.download">
<mkdir dir="${ivy.jar.dir}"/>
<!-- download Ivy from web site so that it can be used even without any special installation -->
<echo message="installing ivy..."/>
<get usetimestamp="true" dest="${ivy.jar.file}" src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"/>
</target>
<condition property="ivy.jar.exists">
<available file="${ivy.jar.file}" type="file"/>
</condition>
<target name="install-ivy" description="--> install ivy" if="ivy.jar.exists">
<!-- try to load ivy here from local ivy dir, in case the user has not already dropped it into ant's lib dir (note that the latter copy will always take precedence). We will not fail as long as local lib dir exists (it may be empty) and ivy is in at least one of ant's lib dir or the local lib dir. -->
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef classpathref="ivy.lib.path" uri="antlib:org.apache.ivy.ant" resource="org/apache/ivy/ant/antlib.xml"/>
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment