
<!--

	This is a build script for compiling and deploying blackberry applications to a server environment for download on the device.
	This script is used internally for development and seemed useful enough to pass along to others in the development community.
	This script has two example deployments - one simple one and another more complex with an alternate entry point, in case that's
	necessary.

	REQUIRED: 
	1.  Blackberry Ant Tools project (http://bb-ant-tools.sourceforge.net/) 
	2.  JCraft SSH lib must be in your ANT lib path (http://www.jcraft.com/jsch/index.html) to deploy to the server
	3.  To send the mail notification of releases, JavaMail must be in the ANT lib path (http://java.sun.com/products/javamail/)

	TO USE:
	1.  Edit the "jde.home" property to reference the location of your JDE
	2.  Edit the "bb-ant-tools" property to reference the loation of your Blackberry Ant Jar file
	3.  Search for TODO and make all the necessary changes

	TARGETS:
	- clean: self explanatory, wipes everything out
	- init: sets everything up
	- compile: builds the application jad and cod files are output from this target
	- sign: invokes the signtool to sign your COD file
	- deploy-XXX: will push the application to the server via SCP for you and send an email to the recepient list corresponding to the
	environment in question.  Build comes with dev, qa and prod environments configured, which can obviously be changed if necessary.


	Author:
	Eric Muntz 
	Versite Consulting, LLC
	eric@versiteconsulting.com

	License:
	Do whatever you wish with this build script.  If you have useful changes or input or just want to say hi, please email me at
	eric@versiteconsulting.com.

-->
<project name="TODO - YOUR PROJECT NAME HERE" default="compile">

	<!-- TODO - these two properties are the big ones to set -->
	<property name="jde.home" location="C:/Program Files/Research In Motion/BlackBerry JDE 4.3.0" />
	<property name="bb-ant-tools" location="C:/Development/SDKs/apache-ant-1.7.0/lib/bb-ant-tools.jar"/>

	<!-- TODO - properties for app names, output dirs, etc -->
	<property name="title" value="App Name"/>
	<property name="vendor" value="Vendor Name"/>
	<property name="output.dir" value="bb-build"/>
	<property name="output.file" value="OutputFilePrefix"/>

	<!-- TODO - email settings recipients list -->
	<property name="email.from" value="eric@versiteconsulting.com"/>
	<property name="email.smtpserver" value="smtp.YOURSERVER.com"/>
	<property name="email.port" value="25"/>
	<property name="dev-deploy-recipients" value="eric@versiteconsulting.com, bb_buildgroup@versiteconsulting.com"/>
	<property name="qa-deploy-recipients" value="eric@versiteconsulting.com, bb_buildgroup@versiteconsulting.com"/>
	<property name="prod-deploy-recipients" value="eric@versiteconsulting.com, bb_buildgroup@versiteconsulting.com"/>
	
	<!-- TODO - server settings -->
	<!-- prod -->
	<property name="prod-server-jad" value="http://myserver.com/blackberry/prod/${output.file}.jad"/>
	<property name="prod-server-address" value="scp.myserver.com"/><!-- for the SCP to the deployment server -->
	<property name="prod-server-username" value="USERNAME"/>
	<property name="prod-server-password" value="SECRETS-ARE-FUN"/>
	<property name="prod-server-dir" value="/vol/www/blackberry/foobar"/><!-- the location on the remote file system -->
	<!-- qa -->	
	<property name="qa-server-jad" value="http://myserver.com/blackberry/qa/${output.file}.jad"/>
	<property name="qa-server-address" value="scp.myserver.com"/><!-- for the SCP to the deployment server -->
	<property name="qa-server-username" value="USERNAME"/>
	<property name="qa-server-password" value="SECRETS-ARE-FUN"/>
	<property name="qa-server-dir" value="/vol/www/blackberry/foobar"/><!-- the location on the remote file system -->
	<!-- dev -->
	<property name="dev-server-jad" value="http://myserver.com/blackberry/dev/${output.file}.jad"/>
	<property name="dev-server-address" value="scp.myserver.com"/><!-- for the SCP to the deployment server -->
	<property name="dev-server-username" value="USERNAME"/>
	<property name="dev-server-password" value="SECRETS-ARE-FUN"/>
	<property name="dev-server-dir" value="/vol/www/blackberry/foobar"/><!-- the location on the remote file system -->


	<!-- define the tasks from bb-ant project -->
	<taskdef resource="bb-ant-defs.xml" classpath="${bb-ant-tools-jar}"/>
	
	<!-- clean -->
	<target name="clean">
		<delete failonerror="false">
		  <fileset dir="${output.dir}" includes="**/*"/>
		  <fileset dir="${output.dir}/unzipped" includes="**/*"/>
		</delete>	
	</target>

	<!-- initialize everything for this process -->
	<target name="init">
		<mkdir dir="${output.dir}"/>
		<mkdir dir="${output.dir}/unzipped"/>
	</target>
	
	<!-- compile the application (just compile, no signing or anything else -->
	<target name="compile" depends="init">
		<input message="What version are you building?" addproperty="app.version"/>
	
		<rapc output="${output.file}" destdir="${output.dir}">
			<src>
				<!-- TODO - Define all the sources and resourcs and such -->
				<fileset dir="src">
					<include name="**/*.java"/>
				</fileset>
				<fileset dir="resources"/>
			</src>
			
			<!-- TODO - Pick an example -->
			
			<!-- This is an application with just a single simple CLDC app -->
			<jdp title="${title}" vendor="${vendor}" 
				version="${app.version}" type="cldc" />
			
			<!-- This is an application with two entry-points, one gui and one background startup app -->
			<jdp title="${title} - background" vendor="${vendor}" 
				version="${app.version}" type="cldc"
				systemmodule="true" runonstartup="true" startuptier="7">
				
				<!-- add an alternate entry point for the gui -->
				<entry title="${title}" arguments="gui" icon="../resources/icon.png" ribbonposition="1"/>
			</jdp>
		</rapc>		
	</target>
	
	<!-- sign the COD file -->
	<target name="sign" depends="compile">
		<sigtool codfile="${output.dir}/${output.file}.cod" />
	</target>

	<!-- this is the case when the code file contains a bunch of other cod files.  you don't need to do this if it's not 
		 the case for your app.
		 
		 This task will rename the COD file to be a zip file and then unzip it to produce the other COD files 
	  -->
	<target name="unzip_cod" depends="sign">
		<copy file="${output.dir}/${output.file}.cod" tofile="${output.dir}/${output.file}.zip" overwrite="true"/>
		<unzip src="${output.dir}/${output.file}.zip" dest="${output.dir}/unzipped" overwrite="true"/>
		<delete file="${output.dir}/${output.file}.zip"/>
	</target>

	<!-- uploads the files to production -->
	<target name="deploy-prod" depends="sign, unzip_cod">
		<!-- scp the build files to the server -->
		<antcall target="upload">
			<param name="server.username" value="${prod-server-username}"/>
			<param name="server.password" value="${prod-server-password}"/>
			<param name="server.address" value="${prod-server-address}"/>
			<param name="server.dir" value="${prod-server-dir}"/>
		</antcall>
		<antcall target="sendmail">
			<param name="recipients" value="${prod-deploy-recipients}"/>
			<param name="server" value="${prod-ftp-server}"/>
			<param name="server.jad" value="${prod-server-jad}"/>
		</antcall>
	</target>
	
	<!-- uploads the file to the qa server -->
	<target name="deploy-qa" depends="sign, unzip_cod">
		<!-- scp the build files to the server -->
		<antcall target="upload">
			<param name="server.username" value="${qa-server-username}"/>
			<param name="server.password" value="${qa-server-password}"/>
			<param name="server.address" value="${qa-server-address}"/>
			<param name="server.dir" value="${qa-server-dir}"/>
		</antcall>
		<antcall target="sendmail">
			<param name="recipients" value="${qa-deploy-recipients}"/>
			<param name="server" value="${qa-server}"/>
			<param name="server.jad" value="${qa-server-jad}"/>
		</antcall>

	</target>
	
	<!-- uploads the file to the dev server -->
	<target name="deploy-dev" depends="sign, unzip_cod">
		<antcall target="upload">
			<param name="server.username" value="${dev-server-username}"/>
			<param name="server.password" value="${dev-server-password}"/>
			<param name="server.address" value="${dev-server-address}"/>
			<param name="server.dir" value="${dev-server-dir}"/>
		</antcall>				
		<antcall target="sendmail">
			<param name="recipients" value="${dev-deploy-recipients}"/>
			<param name="server" value="${dev-server}"/>
			<param name="server.jad" value="${dev-server-jad}"/>
		</antcall>
	</target>
	
	<!-- generic upload the files to the server -->
	<target name="upload" depends="sign, unzip_cod">
		<!-- scp the build files to the server -->
		<scp todir="${server.username}:${server.password}@${server.address}:${server.dir}" trust="true">
			<fileset dir="${output.dir}">
				<include name="${output.file}.jad"/>
			</fileset>
			<fileset dir="${output.dir}/unzipped" includes="**/*"/>
		</scp>
	</target>
	
	<!-- generic sendmail task -->
	<target name="sendmail">
		<mail mailhost="${email.smtpserver}" mailport="${email.port}" subject="${output.file} ${app.version} deployed" tolist="${recipients}">
			<from address="${email.from}"/>
			<message>
Build ${app.version} of ${output.file} has been pushed to the server. 
Application downloadable on Blackberry at ${server.jad}
			</message>
		</mail>		
	</target>
</project>
