Wednesday, December 6, 2006

Far Beyond Open Source Flash Development

Nowadays, Open Source Flash Development has become a reality, but things continue to move.

Projects have been evolving quickly in the past year regarding our recently born Open Source Flash World. The Eclipse based environment advocated in the first installment of this series.

Nowadays:

* Flashout has been entirely deprecated in favor of the new ASDT features like The SWFViewer and The AS Logger (so we have lost the 'F' of 'FAMES' to become 'AMES').
* We are using ANT Eclipse feature more and more along with as2ant tasks to build our SWFs.
* In order to embed our art inside our SWF (clips, fonts, images,...) now we have SWFMill.
* A myriad of open source projects have been born, mainly at the osflash.org site, which gives you powerful capabilities in your day by day development (look at ActionStep or Red5, for example...)
* These are only a few of the important things that happened through 2005 but it's too much to cover in detail in a single article, for this reason I'd like to concentrate in a single point: the ANT tool.

ANT

It was six years ago while consulting at Vodafone Spain where I was introduced to a very useful and productive tool to help build our Java projects. It was of great help for me and my co-workers. That tool was Apache ANT. Since the ANT software came to the stage a lot of products in the market were integrating this Apache Open Source project due to its useful features. One of them was the Eclipse Platform that many of us use today to create our SWF experiences.

ANT is a build tool that uses an XML dialect to execute the tasks that you, the developer, are requesting. The nature of the tasks are very diverse, from copying files to some directory, generating files (SWFs, XMLs or even classes) on the fly or transferring files via ftp to your favorite remote server. These are only a few examples of ANT's Power.

Now that you are producing SWFs in a more standard way thanks to open source, maybe you'll want to invest some time looking through the ANT manual to discover some hidden gems that improves your development workflow. By the way, using ANT you'll always have full control over your project and will maintain it free of dependencies from other production environments and IDEs.

The following are some tips and tricks that may be handy in your flash open source development. If you have never used ANT for flash development until now, you can check the following tutorial for a great guide to get started.

I recommend that you open your Eclipse IDE to follow my explanation, and create or open any ANT build.xml. Note that properties and build files must be in the project's root folder

USE PROPERTIES FILES

Instead of declaring all your properties in your build file, make one external file to be able to reuse it in other projects. You could create an "ames.properties" file and reference it from your build XML file, using the following XML tag:



The "ames.properties" file could have the vars you used to write in mostly all your build files like:

#AMES Home
ames=C:/AMES

#SWFMill
swfmill=${ames}/swfmill/swfmill.exe

#MTASC
mtasc=${ames}/mtasc/mtasc.exe

#AS2ANT (see the section USE AS2ANT TASKS below)
as2ant=${ames}/as2ant/src

...

USE ANT PREDEFINED VARS

In Eclipse and inside a build file and "project" tag, if you type the $ symbol you'll get code hinting with the variables that you can use inside your custom build file. The vars list is constructed with ANT's predefined vars and your custom vars.

For example, If you are working in a team, the ${user.home} var could be invaluable to declare where in your local disk your flash 8 classes are, because it will be translated to each user's home directory. The following could also be in your newly created "ames.properties":

#FLASH 8 CLASSES
Flash8Classes=${user.home}/local Configuration/
Program Files/Macromedia/Flash 8/en/Configuration/Classes

Or you could customize some file names automatically according to your project's name in your build file:



USE AS2ANT TASKS

Although you could use MTASC or SWFMill with the default exec ANT task, a recommended best practice is to use as2ant 1.0 (now in beta). This package provides you with three new tasks specially designed to use with your favorite open source swf related software. The tasks are MTASC, SWFMILL and SWF. To install it and use it in your build file:

You could create the "as2ant_taskdefs.xml" file:

description="as2ant_taskdefs"/>




classname="org.as2lib.ant.Mtasc"
classpath="${as2ant}"/>
classname="org.as2lib.ant.Swfmill"
classpath="${as2ant}"/>
classname="org.as2lib.ant.Swf"
classpath="${as2ant}"/>


and import it from your build file:



For example, you can now use the new tasks for SWFMill SWF creation:

src="${swfmill_xml_file}"
dest="${swfmill_in_file}"/>

or with MTASC:

version="8"
frame="5"
classpath="${project.classes};
${animationpackage_classes};${remoting_classes}"
swf="${swfmill_in_file}"
out="${mtasc_out_file}"
main="true" src="${mainclass}"/>

Notice that swfmill and mtasc attributes in each task must be set to the location where you installed the corresponding distribution of those tools.

USE ANT MACRODEF TO BUILD YOUR MACROS

As you refine your build files you'll need to refactor some common tasks and pass some variables. You can accomplish this with macrodef task. In the following example I've created a macrodef called "createSWF":





version="@{version}"
classpath="${project.classes}"
swf="${swfmill_in_file}"
out="${mtasc_out_file}"
main="true"
src="${mainclass}"/>



You use the "createSWF" macrodef with the following sentence inside a target:






As you notice I'm passing a "version" attribute to the macro for configuration purposes and using it with the @ symbol.

CREATE YOUR NEEDED FILES ON THE FLY

Another great use for ANT is the possibility to create files on the fly. You could use this feature to create a config file or even a Class file before MTASC compilation:

For example use the following to create a class file with build information in folder "com/carlosrovira/myProject":








pattern="M/yyyy" unit="month"/>




append="false">
// --- This file is created by an ANT build task
// --- (see the build.xml file for more info)
class com.carlosrovira.myProject.BuildInformation {
public static var versionNumber:String = "${build.version}";
public static var buildTime:String = "${build.time}";
public static var buildNumber:Number = ${build.number};

public static var copyright_str:String = "Copyright(c) 2006 Carlos Rovira.";
}


If you create files with ANT and you're using Eclipse I recommend that you configure the ANT Build dialog checking "Refresh resources Upon Completion" (in this way you could refresh the folder, or the project, etc...). Also check Eclipse-ANT specific task:


CREATE A SHORCUT KEYS IN ECLIPSE

This is an Eclipse-ANT specific trick. This would allow you to have a shortcut for building your project.

Go to the menu bar through Window and select the Preferences dialog window. Inside General > Keys you'll see a View Tab. You could select the Run/Debug Category with the command "Run Ant Build" inside the Datagrid. You could add another key combination inside the "modify" tab (Alt+Shift+X+Q is a little cumbersome in my opinion). Maybe some combo keys like the ones in Flash Authoring Environment (CTRL+ENTER or SHIFT+F12) could be very useful, isn't it?.

CONCLUSION

I hope you learn and enjoy some of the ANT tricks I explained in this article and improve your art in building your SWF projects. In the future, I expect ASDT will have more and more automation and ANT integration to build your SWF related projects, but at the moment is recommended to have a good knowledge about the possibilities you now have in your hands.