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
if defined ANT_ANSI set ANT_ARGS=-logger org.apache.tools.ant.listener.AnsiColorLogger
<project name="RadJava" basedir="." default="build">
<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="libs.dir" location="libs"/>
<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>
<condition property="libs.dir.exists">
<available file="${libs.dir}" type="dir"/>
</condition>
<target name="build.classpath.true" if="libs.dir.exists">
<path id="build.classpath">
<fileset dir="${libs.dir}" includes="*.jar"/>
</path>
</target>
<target name="build.classpath.false" unless="libs.dir.exists">
<path id="build.classpath">
</path>
</target>
<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>
<target name="properties" depends="build.classpath.true,build.classpath.false">
<property name="properties.set" value="true"/>
<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>
<target name="compile" depends="properties" 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" depends="properties">
<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="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="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>
<target name="dep" if="project.dep" description="build dependencies">
<subant target="install">
<property name="install.dir" value="${libs.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="examplesclean" description="clean examples">
<subant target="clean">
<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>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment