Flex is great, but it isn't a server technology. There, I said it.
I was very stubborn when it came to using Flex. There were a variety of reasons that I've never tried to build anything substantial with the technology. One of them was the apparent disrespect to Flash Developers. The other was the idea that I had to run Flex on a server. The last was the price tag. There were some others, but those were the major three for me personally.
As a long time Flash Developer I've seen the birth and rapid growth of "Rich Internet Applications." From smart clips came components, then the v2 architecture, and now Macromedia has an entire RIA framework available through Flex, but somehow along the way the Flash Developers got left behind. Maybe not officially, but it seems that way. In a way, we were the ones that drove the creation of Flex in the first place.. but so what? It wasn't long ago that Flash MX 2004 Professional was touted as the tool for developing Rich Itnernet Applications. Then Flex came along with an improved workflow and upgraded component set, and now Flash has gone the way of designers.
So where does that leave the Flash Developers? Jesse had an interesting take on this a few months ago. Times change, and it looks as though Flex is going to be the future of RIA development. Is it inevitable? Who knows, but for Flash Developers, I suppose we'll just "keep on keepin' on," as they say. There are still a ton of things I would favor Flash for, but for applications I don't think Flex can be beat. Aral would agree, I'm sure. Flex is worth learning because it really does improve how RIAs are built, but I'm still partially stuck on the pricing. The time saved should pay for the price tag, but you have to make a long term commitment to it for it to pay for itself.
Anyway, I'm not trying to get philosophical or anything. I've avoided Flex long enough, and now that I've started to use it there was a major problem I needed to solve right out of the starting gate. Flex should not be server-side technology. Period.
In my eyes, Flex is and should be just a compiler. The MXML markup is converted to ActionScript, run through some magic, and *poof* we get a .swf file. We all know that .swf files run in the Flash Player which is all client side technology. So why is a server necessary? It was probably a strategic move on Macromedia's part, like how Generator was Enterprise as well. After all, no one would pay then of thousands of dollars for a compiler.. but when it's bundled as a server and labeled Enterprise, the wool is pulled over the eyes. Needless to say I disagree with the Flex pricing model / server strategy, but I've already mentioned that.
Anyway, on to the good stuff. If you're like me then you don't think Flex should be running on the server at all, and it's actually not that hard to get around this. In the installation directory there are two jar files, mxmlc.jar and compc.jar, our compilers. The former produces .swf files form .mxml files. The later produces .swc files. We can invoke these compilers directly giving them appropriate input, and deploy the .swf output to our server.
This technique is similar to what Ted posted about in scaling Flex, but goes one step further in that it doesn't require flex running at all, not even on localhost.
Below is my ant build.xml file for automated precompiling of Flex applications. You'll need to have version 1.6+ of ant to use it since I'm using the new "macrodef" tag. If you want to build everything, you can run "ant all", otherwise just configure your modules / libraries and you can build them one at a time or in groups ("ant library1", "ant module1", "ant libraries", "ant modules" etc).
This is probably not just a copy and paste solution - you'll at least need to configure the libary / module sections, but also note the path for the Flex installation directory and also the path for the WEB-INF stuff. I just copied the WEB-INF directory into /etc of my project directory and went from there. Make sure your flex-config.xml file has the right paths as well. Watch out for "webroot" in the maskeswf macro as well, and also careful of the path where rsl's will be located on the server (currently I just use "libs"). Make sure the library destination jives with the url in your .sws files.
Anyway, here's the script. Comments are appreciated..
<project default="all">
<property name="flex.dist.lib" value="C:\Program Files\Macromedia\Flex\lib" />
<property name="flex.compc.jar" value="${flex.dist.lib}\compc.jar" />
<property name="flex.mxmlc.jar" value="${flex.dist.lib}\mxmlc.jar" />
<property name="app.dir" value="." />
<property name="config.xml" value="${app.dir}/etc/WEB-INF/flex/flex-config.xml" />
<property name="src.dir" value="${app.dir}/src" />
<property name="dest.dir" value="${app.dir}/bin" />
<property name="library.dest.dir" value="${dest.dir}/libs" />
<target name="init">
<!-- create directories -->
<mkdir dir="${dest.dir}" />
<mkdir dir="${library.dest.dir}" />
</target>
<!-- macro to easily create an rsl -->
<macrodef name="makelibrary">
<attribute name="library" />
<sequential>
<!-- compile the shared library -->
<java jar="${flex.compc.jar}" dir="${app.dir}" fork="true" failonerror="true">
<arg line="-flexlib '${flex.dist.lib}' -configuration
${config.xml} -webroot . -o ${src.dir}/@{library}.swc ${src.dir}/@{library}.sws" />
</java>
<!-- extract the .swf from the .swc for the library -->
<unzip src="${src.dir}/@{library}.swc" dest="${src.dir}">
<patternset>
<include name="**/*.swf" />
</patternset>
</unzip>
<!-- delete the .swc -->
<delete file="${src.dir}/@{library}.swc" />
<!-- rename and move the extracted library.swf to the name of the library -->
<move file="${src.dir}/library.swf" tofile="${library.dest.dir}/@{library}.swf" />
</sequential>
</macrodef>
<!-- macro to convert .mxml into .swf -->
<macrodef name="makeswf">
<attribute name="module" />
<sequential>
<!-- might need to watch out for webroot here... -->
<java jar="${flex.mxmlc.jar}" dir="${app.dir}" fork="true" failonerror="true">
<arg line="-flexlib '${flex.dist.lib}' -configuration
${config.xml} -webroot . -o ${dest.dir}/@{module}.swf ${src.dir}/@{module}.mxml" />
</java>
</sequential>
</macrodef>
<!-- individual library targets to build one at a time -->
<target name="library1" depends="init">
<makelibrary library="Library1" />
</target>
<target name="library2" depends="init">
<makelibrary library="Library2" />
</target>
<!-- group libraries target to build all rsls used -->
<target name="libraries" depends="library1,library2">
</target>
<!-- individual module target to build one at a time -->
<target name="module1" depends="init">
<makeswf module="Module1" />
</target>
<target name="module2" depends="init">
<makeswf module="Module2" />
</target>
<!-- group modules target to build all .swf files in an app -->
<target name="modules" depends="module1,module2">
</target>
<!-- group target to build all parts of an app -->
<target name="all" depends="libraries,modules">
</target>
</project>

But doesn't the flex .swf expire itself after a certain amount of time? I was under the impression that they had some kind of timebomb to prevent this type of thing.
Only the Trial and Developer versions of Flex have the timebomb. The licensed version doesn't - you just need to make sure you have a Flex license for wherever the .swf goes. For licensing issues, talk with Macromedia. :-)
What Remote Object and Webservice proxies using named services? Don't you need a server to get that to work?
Al
>What Remote Object and Webservice proxies using named services? Don't you need a server to get that to work?
Yes. Web services will work (not sure about remote objects).
The charts on MXNA are created in Flex and precompiled. They connect to the mxna webservices.
http://weblogs.macromedia.com/mxna/reports/
mike chambers
mesh@macromedia.com
Really Impressive post.
But as the frist user post a commentary. The timebomb still make us or force to use until flex use.
So, But I really appreciated the post even I tested here and the Lake of Memory stopped when I've tested out of Flex using the Ant.
Great stuff
The WebServices and Remoting can be used without the proxy. In MXML simply add useProxy="false" to mx:WebService tags and configure your crossdomian.xml file for data exchange security and your set.
It's funny Darron, your ANT script looks more complex than most of my Flex MXML apps. There is a simpler way to compile and deploy to static SWF and it is multi-user. I will post this later today.
Ted ;)
RemoteObjects require Flex' built in Flash Remoting. It differs from the standard Flash Remoting in that it has a different security mechnism, and the named services in the flex-config.xml file.
You will need to use another gateway such as OpenAMF if you wish to use Java RemoteObjects without using the Flex server.
Hi Darron,
Well said! Hopefully Flash 8 will let developers use MXML on client side.
The only reason I can think of compiling the SWF on a server is so that data (from a database, from XML, etc.) can be included in the SWF, thus avoiding subsequent requests to the server.
BTW, how does your Ant script avoid the "expiration date" limit with Flex-compiled SWFs?
Mike
In regards to the expiration date, this is what I see when I use my script.
Warning: applications compiled into standalone SWFs using the Trial and Developer editions of Macromedia Flex expire 1 day after creation. This restriction is only in place for the Trial and Developer editions of Macromedia Flex.
I admit I'm running a trial version of Flex right now, trying to build something to show management it's a good purchase decision. I assume, judging from the message above, that using a non-trial/developer version of Flex removes the expiration date.
@Ted - I know you. You're just biased against Java. ;-) Looking forward to seeing your version!
This sounds similar to the SOLO mode of Laszlo server. Web services are fair enough for dynamic information, but from what I understand the only advantage is the usage of MXML. Some of the biggest strengths like pre-rendered data and powerful customization will be missing. I haven't used Flex, so correct me if I'm wrong.
Please forgive my ignorance, but what is a 'Ant' ?
I have started to learn Flex, I have a good Actionscript/coldfusion background, but I don't understand very well the underlying technology. Would this allow me to compile .mxml files, let's say from Eclipse?
Ant is an automated build tool. It allows you to specify instructions in an build.xml file and it will process the instructions for you. It started off being used to build Java apps, but the instructions available has grown and now it can do a wide range of features. You can see from the above that I'm invoking a jar file, unzipping a file, moving stuff around, creating directories, etc.
Yes, this will let you compile .mxml files from eclipse. You'll even see the Warnings and such in the Eclipse Console window. Just make a build.xml file in your project, and Eclipse will open it with the Ant editor plugin.
I posted my build utility on this blog entry.
How to use Flex with and without a server
Cheers,
ted :)
Any chance we could get a small "Hello World" example? I'm an Flex/Ant newbie and this is killing me.
http://iamin.blogdriver.com/iamin/1173623.html
Use Ant and Flex build .mxml to .swf
Maybe I haven't delved deep enough yet, but isn't the current version of the Flex SDK (for Flex 2) free?
I only see that you have to pay for FlexBuilder, and perhaps some optional service libraries.
@ Clarance, Yes, Flex 2 does not require a server. This post, dated June 1, 2005, was intended for Flex 1/1.5.