FDT and Ant Tutorial

From FDT Documentation

(Difference between revisions)
Jump to: navigation, search
(Created page with " If you're just using the built in Ant tasks ( not FDT's Ant tasks ), you wont need to work about making sure your Ant tasks (file) are operating as the same JRE (Java Run Time ...")
Line 1: Line 1:
 +
Sometimes the build process can get pretty complex, for example, you may create several SWCs and SWFs, some HTML files to be dynamically edited and updated and files copied and uploaded.  In these cases the launcher on it's own won't cut it.
-
If you're just using the built in Ant tasks ( not FDT's Ant tasks ), you wont need to work about making sure your Ant tasks (file)  are operating as the same JRE (Java Run Time Environment) as FDT; however, if you plan on using FDT's Ant tasks, or FDT's Ant tasks along with Ant's built in tasks, you will.
+
FDT ships with built in Ant tasks for you to use and at the same time, Ant provides a lot of useful tasks already. This intro will help get you acquainted with using FDT and Ant.
-
+
 
-
You"BUILD FAILED Could not create task or type ..." your task probably runs in another JRE. This can be corrected. Therefore got to "Run- >External Tools->External Tools..." in the main menu. Choose your run configuration and go to the tab "JRE". Change the option "Runtime JRE" from "Separate JRE" to "Run in the same JRE as the workspace".
+
=Getting Started=
 +
Begin by importing this project into FDT ( learn about importing and exporting via Creating Project Templates). Or create your own.
 +
 
 +
=Create Your Build File=
 +
With the project open [[File:09_001.gif|10px]], let's begin by creating our 'build' Ant file. Right click on the project and choose '''new>other…''' [[File:09_002.gif|10px]]
 +
 
 +
type '''xml''' in the filter field, choose 'XML' from the 'XML' folder and then click 'Next' [[File:09_003.gif|10px]]. Name the file 'build.xml' (it can be anything but build is the standard), save it within the root directory and then click 'finish' [[File:09_004.gif|10px]].
 +
 
 +
As of now, the file is a standard '.xml' file. We can edit the file as is and it will work but it would be immensely helpful if we had the syntax highlighting and auto completion like we have when editing ActionScript files. We want to make sure FDT opens this file up as an Ant build file.
 +
 
 +
For FDT to recognize this .xml file as an Ant file, all we need to do is to  do is add in this syntax [[File:09_005.gif|10px]]:
 +
 
 +
<syntaxhighlight lang="xml">
 +
<project>
 +
<target>
 +
</target>
 +
</project>
 +
</syntaxhighlight>
 +
 
 +
 
 +
Close the file and then open it back up within the editor. We need to do this because the first time FDT opened the file, it opened it as a generic XML file - using the XML Editor. When the file is opened up and the Ant file syntax is recognized, it will open the file with the Ant Editor.
 +
 
 +
When the file is opened up again, you will immediately recognize that FDT has detected an error: '''target element appears without a name attribute''' [[File:09_006.gif|10px]].  This is great, FDT now will tell us about syntax errors and any incomplete arguments (attributes) with our tasks.
 +
 
 +
We also now have code hinting and auto completion.  Move your cursor to the '''target''' task and begin typing '''name''' and then '''Ctrl+Space''' (autocomplete) and the '''name''' attribute will be filled in.  Add a name, let's use '''default''', then save the file to have the errors recalculated in our '''Problems''' view.
 +
 
 +
=Your First Task: Compiling & JRE Error=
 +
Within the '''task''' element, begin typing '''fdt.launch.application''' and use FDT's auto-complete to fill in the remainder of the element [[File:09_007.gif|10px]]. With the element created, add these three attributes to the element: '''projectname''' , '''mainclass''' and '''target'''.
 +
 
 +
<syntaxhighlight lang="xml"
 +
<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf"/>
 +
</syntaxhighlight>
 +
 
 +
 
 +
This is the minimum needed to create a .swf with FDT and Ant. Just like many other tasks in FDT, there are a few different ways to run Ant tasks.  The easiest way is to execute tasks via the '''Ant View'''. If the view isn't open already, open it via '''Window>Show View>Ant''' [[File:09_008.gif|10px]]. With the Ant view open, drag the '''build.xml''' file into the '''Ant''' panel [[File:09_009.gif|10px]] and then expand the elements and you'll see how the Ant View's contents match the build.xml file in the Editor [[File:09_010.gif|10px]].
 +
 
 +
To start the build, just select the target ('''default''' in our case ) within the Ant View [[File:09_011.gif|10px]] and then click the '''Run'' button[[File:09_012.gif|10px]].
 +
 
 +
Immediately we encounter an error:
 +
 
 +
'''Problem: failed to create task or type fdt.launch.application'''
 +
 
 +
This may seem frustrating at first, but when you understand how Ant, Java, FDT and your operating system work together it makes sense why this happens. By default, development with Eclipse (the platform FDT is built upon) recommends using separate Java Runtime Environments (JRE) when developing and when testing applications. That's great for Java developers, but we don't need that type of sand boxing when creating .SWFs.
 +
 
 +
We need to have the Ant build execute within the same JRE as FDT in order to have access to FDT's custom Ant tasks. Otherwise, it will run in a sandboxed environment that doesn't know anything about FDT.
 +
 
 +
Fortunately for us, this is a simple fix - from the '''External Tools''' menu choose '''External Tools Configurations''' [[File:09_013.gif|10px]]. With the '''External Tools Configurations''' open [[File:09_014.gif|10px]] navigate to the '''JRE''' tab [[File:09_015.gif|10px]] and check the radio button next to '''Run in the same JRE as the workspace''' [[File:09_016.gif|10px]]. From here we can simply click '''Run''' to have the build execute. [[File:09_017.gif|10px]]
 +
 
 +
The build will run and from the output [[File:09_018.gif|10px]] we see that our .swf has been created. The important bit to read is this:
 +
 
 +
'''[fdt.launch.application] 644 bytes written to file /Users/OSX/_dev/fdt/AS3Project/bin/Main.swf in 2429 ms
 +
BUILD SUCCESSFUL'''
 +
 
 +
=Customizing The Build and Using Multiple Targets=
 +
 
 +
With the basics down, lets extend our Ant build. It won't be very realistic, but it will demonstrate  ways of extending Ant.
 +
 
 +
First thing I want to do is give my build a name so it's not referred to as '''project'''  and change the name of my target to '''compile'''[[File:09_019.gif|10px]].
 +
 
 +
<syntaxhighlight lang="xml">
 +
<project name="AS3Project Build">
 +
<target name="compile">
 +
<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
 +
</target>
 +
</project>
 +
</syntaxhighlight>
 +
 
 +
 
 +
Next, I'll add a task to create a .SWC [[File:09_020.gif|10px]]:
 +
 
 +
 
 +
<syntaxhighlight lang="xml">
 +
<project name="AS3Project Build">
 +
<target name="compile">
 +
<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
 +
<fdt.launch.library projectname="AS3Project" target="bin/Main.swc"/>
 +
</target>
 +
</project>
 +
</syntaxhighlight>
 +
 
 +
 
 +
Then add some more tasks [[File:09_021.gif|10px]]:
 +
 
 +
<syntaxhighlight lang="xml">
 +
<project name="AS3Project Build">
 +
<target name="compile">
 +
<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
 +
<fdt.launch.library projectname="AS3Project" target="bin/Main.swc"/>
 +
</target>
 +
 +
<target name="browse">
 +
<fdt.browse location="${basedir}/bin/Main.swf" />
 +
</target>
 +
 
 +
<target name="zip">
 +
<zip destfile="${basedir}/deploy.zip" basedir="${basedir}/bin"/>
 +
<move file="${basedir}/deploy.zip" todir="${basedir}/deploy" />
 +
</target>
 +
 +
</project>
 +
</syntaxhighlight>
 +
 
 +
 
 +
This is all pretty straight forward.  The new '''browse''' target will launch my browser navigate it to the '''Main.swf''' and the '''zip''' and '''move''' tasks are built into Ant.  For more info on that, check out an [http://ant.apache.org/manual/tasksoverview.htm overview of Ant tasks].
 +
 
 +
Last, I'm going to include a '''run.all''' target [[File:09_022.gif|10px]] and then set it as my default [[File:09_023.gif|10px]]:
 +
 
 +
<syntaxhighlight lang="xml">
 +
<target name="run.all" depends="compile,browse,zip">
 +
</target>
 +
</syntaxhighlight>
 +
 
 +
Setting the default task is handy. Running the file from the command line won't need a target argument, double clicking on the build file within the Ant view will run it, and the task in the Ant view is now blue - making it easier to read [[File:09_024.gif lang="xml"].
 +
 
 +
To run everything, just double click on the Ant build file icon within the Ant view, and all the magic will start [[File:09_025.gif|10px]].

Revision as of 22:06, 22 September 2010

Sometimes the build process can get pretty complex, for example, you may create several SWCs and SWFs, some HTML files to be dynamically edited and updated and files copied and uploaded. In these cases the launcher on it's own won't cut it.

FDT ships with built in Ant tasks for you to use and at the same time, Ant provides a lot of useful tasks already. This intro will help get you acquainted with using FDT and Ant.

Contents

Getting Started

Begin by importing this project into FDT ( learn about importing and exporting via Creating Project Templates). Or create your own.

Create Your Build File

With the project open 09 001.gif, let's begin by creating our 'build' Ant file. Right click on the project and choose new>other… 09 002.gif

type xml in the filter field, choose 'XML' from the 'XML' folder and then click 'Next' 09 003.gif. Name the file 'build.xml' (it can be anything but build is the standard), save it within the root directory and then click 'finish' 09 004.gif.

As of now, the file is a standard '.xml' file. We can edit the file as is and it will work but it would be immensely helpful if we had the syntax highlighting and auto completion like we have when editing ActionScript files. We want to make sure FDT opens this file up as an Ant build file.

For FDT to recognize this .xml file as an Ant file, all we need to do is to do is add in this syntax 09 005.gif:

<project>
	<target>
	</target>
</project>


Close the file and then open it back up within the editor. We need to do this because the first time FDT opened the file, it opened it as a generic XML file - using the XML Editor. When the file is opened up and the Ant file syntax is recognized, it will open the file with the Ant Editor.

When the file is opened up again, you will immediately recognize that FDT has detected an error: target element appears without a name attribute 09 006.gif. This is great, FDT now will tell us about syntax errors and any incomplete arguments (attributes) with our tasks.

We also now have code hinting and auto completion. Move your cursor to the target task and begin typing name and then Ctrl+Space (autocomplete) and the name attribute will be filled in. Add a name, let's use default, then save the file to have the errors recalculated in our Problems view.

Your First Task: Compiling & JRE Error

Within the task element, begin typing fdt.launch.application and use FDT's auto-complete to fill in the remainder of the element 09 007.gif. With the element created, add these three attributes to the element: projectname , mainclass and target.
 

</syntaxhighlight>


This is the minimum needed to create a .swf with FDT and Ant. Just like many other tasks in FDT, there are a few different ways to run Ant tasks. The easiest way is to execute tasks via the Ant View. If the view isn't open already, open it via Window>Show View>Ant 09 008.gif. With the Ant view open, drag the build.xml file into the Ant panel 09 009.gif and then expand the elements and you'll see how the Ant View's contents match the build.xml file in the Editor 09 010.gif.

To start the build, just select the target ('default in our case ) within the Ant View 09 011.gif and then click the Run button09 012.gif.

Immediately we encounter an error:

Problem: failed to create task or type fdt.launch.application

This may seem frustrating at first, but when you understand how Ant, Java, FDT and your operating system work together it makes sense why this happens. By default, development with Eclipse (the platform FDT is built upon) recommends using separate Java Runtime Environments (JRE) when developing and when testing applications. That's great for Java developers, but we don't need that type of sand boxing when creating .SWFs.

We need to have the Ant build execute within the same JRE as FDT in order to have access to FDT's custom Ant tasks. Otherwise, it will run in a sandboxed environment that doesn't know anything about FDT.

Fortunately for us, this is a simple fix - from the External Tools menu choose External Tools Configurations 09 013.gif. With the External Tools Configurations open 09 014.gif navigate to the JRE tab 09 015.gif and check the radio button next to Run in the same JRE as the workspace 09 016.gif. From here we can simply click Run to have the build execute. 09 017.gif

The build will run and from the output 09 018.gif we see that our .swf has been created. The important bit to read is this:

[fdt.launch.application] 644 bytes written to file /Users/OSX/_dev/fdt/AS3Project/bin/Main.swf in 2429 ms BUILD SUCCESSFUL

Customizing The Build and Using Multiple Targets

With the basics down, lets extend our Ant build. It won't be very realistic, but it will demonstrate ways of extending Ant.

First thing I want to do is give my build a name so it's not referred to as project and change the name of my target to compile09 019.gif.

<project name="AS3Project Build">
	<target name="compile">
		<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
	</target>
</project>


Next, I'll add a task to create a .SWC 09 020.gif:


<project name="AS3Project Build">
	<target name="compile">
		<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
		<fdt.launch.library projectname="AS3Project" target="bin/Main.swc"/>
	</target>
</project>


Then add some more tasks 09 021.gif:

<project name="AS3Project Build">
	<target name="compile">
		<fdt.launch.application projectname="AS3Project" mainclass="src/Main.as" target="bin/Main.swf" />
		<fdt.launch.library projectname="AS3Project" target="bin/Main.swc"/>
	</target>
 
	<target name="browse">
		<fdt.browse location="${basedir}/bin/Main.swf" />
	</target>
 
	<target name="zip">
		<zip destfile="${basedir}/deploy.zip" basedir="${basedir}/bin"/>
		<move file="${basedir}/deploy.zip" todir="${basedir}/deploy" />
	</target>
 
</project>


This is all pretty straight forward. The new browse target will launch my browser navigate it to the Main.swf and the zip and move tasks are built into Ant. For more info on that, check out an overview of Ant tasks.

Last, I'm going to include a run.all target 09 022.gif and then set it as my default 09 023.gif:

<target name="run.all" depends="compile,browse,zip">
</target>

Setting the default task is handy. Running the file from the command line won't need a target argument, double clicking on the build file within the Ant view will run it, and the task in the Ant view is now blue - making it easier to read [[File:09_024.gif lang="xml"].

To run everything, just double click on the Ant build file icon within the Ant view, and all the magic will start 09 025.gif.

Get FDT5