Flex 3 sdk command line development with example on Linux

This post shows how to do Flex 3 development using command line compiler mxmlc under Linux
The example uses a basic actionscript 3 class and an mxml file using the class defined in the .as file.

Step 1:
Download and extract flex 3 sdk to /opt/flex_sdk_3

Hence my flex_sdk_home is /opt/flex_sdk_3

Add /opt/flex_sdk_3/bin to environment path variable.

Create project folder in workspace /home/asantoso/workspace/flex/

With project name example: actionscript

The final directory structure is:
/home/asantoso/workspace/flex/actionscript/
/home/asantoso/workspace/flex/actionscript/src/
/home/asantoso/workspace/flex/actionscript/bin/

Step 2:
Copy flex-config.xml from ${flex_sdk_home}/frameworks to /home/asantoso/workspace/flex/actionscript/flex-config.xml

cp $flex_sdk_home/frameworks/flex-config.xml /home/asantoso/workspace/flex/actionscript/flex-config.xml

Edit flex-config.xml:
vi $flex_sdk_home/frameworks/flex-config.xml

Important step:

${flexlib} is a special config file variable used by the compiler and the value refers to the directory of the sdk. in this example ${flexlib} points to /opt/flex_sdk_3/frameworks. Since you are using flex-config.xml in a different folder, you need to correct the values of external library path elements, library path elements, namespace manifest path. You can statically set the paths manually, or append ${flexlib} to external library path elements, library path elements, namespace manifest path.

Now, uncomment the source-path element, and add a path-element child in the source-path element, which points to the root src folder of our project.
<path-element>/home/asantoso/workspace/flex/actionscript/src/</path-element>

save flex-config.xml


Step 3:
Create actionscript file: /home/asantoso/workspace/flex/actionscript/src/com/example/quickstart/Greeter.as

package com.example.quickstart
{
public class Greeter
{
public var name:String;
private var secretValue:Number;

public function Greeter(initialName:String=”Agus”)
{
name = initialName;
}

public function sayHello():String
{
var result:String;
if(name!=null && name.length>0){
result = “Hello there, “+name+”.”;
}
else{
result=”Hello there, anonymous.”;
}
return result;
}
}
}


Create mxml file: /home/asantoso/workspace/flex/actionscript/com/example/quickstart/Greeter_mx.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initApp();">
<mx:Script>
<![CDATA[
import com.example.quickstart.Greeter;
private function initApp():void{
var myGreeter:Greeter = new Greeter();
output.text=myGreeter.sayHello();
output.text+="\n";
myGreeter.name="Harold";
output.text += myGreeter.sayHello();
}

]]>
</mx:Script>

<mx:Text id=”output” width=”100%” textAlign=”center”/>

</mx:Application>


Step 4:
Create a file /home/asantoso/workspace/flex/actionscript/compile
vi compile

#!/bin/bash
mxmlc -load-config flex-config.xml ./src/com/example/quickstart/Greeter_mx.mxml -output ./bin/Greeter_mx.swf

make the file executable: chmod 755 compile

./compile

or compile them manually as follows:

optional step, compile actionscript class file (just for testing):
mxmlc Greeter.as

Compile flex’s mxml file:
mxmlc Greeter_mx.mxml

To test the example swf:
Open /home/asantoso/com/example/quickstart/Greeter_mx.swf in Firefox


About this entry