[Flex]用Anvil构建企业级Flex应用

download:http://www.ohloh.net/projects/anvilflex/download?filename=anvil-0.5.1.zip

OVERVIEW

Anvil was designed to help make enterprise flex projects easier and to provide a portal environment for running flex applications.  Today Anvil best serves as a sample of how to build enterprise applications.  We are working on making it easier to quickly create a customized environment for flex.

What we have found was the biggest challenge with developing a large enterprise framework was how to cleanly divide the application into multiple modules that can be built, maintained and loaded separately.  One of the primary challenges with multiple modules is how to cleanly separate the handling of non-visual events.  An example would be having one module call a service in another module.  Another issue we ran into was how to isolate events with-in a module, especially if there is multiple copies of the same module in an application.  An example of this is when you have a portal framework where the user can display the same sub-window multiple times.  The standard event dispatcher does not work for these cases and most frameworks do not handle this situation either.  For example the popular cairngorm framework was designed to only work in a monolthic environment and event handling gets messed up when you have duplicate copies of a module.

Anvil comes with two sample applications to help you get started.  One is a basic sample in the basicSample directory and the other is Anvil Portal in the anvilPortal directory.  The biggest difference is the basic sample does not use any security.  The anvil/dist directory has pre-built war files so you can easily see what basic Anvil applications look like.

The anvil/dist directory also contains compiled distribution files of the core Shell flex library, the FlexMDI flex library, the shell and utility jar files and the war files for the basic sample and anvil portal.

In the shell directory is the core library files of Anvil for both the Flex and Java side.  Then in the layouts directory is the primary visual layout library, FlexMDI, based on code from the Flex Lib project.

The utils directory contains a Java project with a number of utilities for working with Flex.  This includes a tool to create ActionScript Data Objects from Java Data Objects.  Another utility is Taz, a flex compiler built on top of mxmlc and compc that makes compiling large projects much faster and easier.  There are samples of how to call Taz from ant build scripts.  And the final utility is a FlexBuilder project file generator.

QUICK START

For the impatient here is a quick overview of setting up the sample applications.  Install Java and Tomcat to just run the samples.  We have a detailed post on this here.  Also install Flex and Ant to build the samples.  Be sure to setup the appropriate home environment variables, like JAVA_HOME, CATALINA_HOME, ANT_HOME and FLEX_HOME.

You can copy the sample war files out of the dist directory into your favorite servlet container.   Then to run anvil use the following url in your browser:
http://localhost:8080/anvilPortal/

To rebuild the application you can run ant at any directory level. So running ant under shell will re-compile the shell and under anvilPortal will rebuild the sample.  Running ant war  will rebuild the war files under anvilPortal/build and basicSample/build.

SETTING UP FLEX BUILDER

To setup the Flex Builder project files for any subproject, cd to that directory and modify anvil.conf.  Probally the only change will be to where you want it to output files and then run:
ant gen-proj

This will also work from the top level directory.

If you want to reset your project files run:
ant clear-proj

Then in Flex Builder go to file -> import -> other and then under General choose Existing Projects into Workspace. For the root directory first choose shell.  Once you have shell imported and build, then import FlexMDI, anvilPortal and basicSample.

USING THE ANVIL UTILITIES

Both the Taz compiler wrapper and the FlexBuilder project file generator use the same config files.  The root config file is called anvil.conf and contains the default values, like output directory and the order library files should be compiled in.

Then each project directory has its own config file, like anvilPortal.conf or basicSample.conf.  This contains information about each application, module or library.

The files are very short and simplistic, hopefuly making it easy to configure new projects.
To run the taz compiler there is a common ant macro definition in flex.ant.xml called <anvil.taz/>.

For an example of using it see the anvilPortal build.xml file.

To run the actionScript project generators there is a script in the root directory called runAsGen.sh.
This has the following parameters
1 – the input directory of the compiler java class files.  By default it is in out/production/shell
2 – the library directories to scan for the necessary jar files
3 – the output directory – this defaults to newAsClasses

BUILDING YOUR OWN APPLICATION BASED ON ANVIL

By using the AnvilPortal framework as a template, you can jumpstart a Flex Application with a Java, Spring, and Spring Security back end.  For now it requires a little bit of tedious copy and pasting, but hopefully in the future we can automate it with Maven.

First set up the basic directory structure for your project and then copy in the necessary files from Anvil.  The way anvil portal is organized is their is one directory for each module and then inside of there a src/flex and src/java.

As you copy across the files, you will want to get rid of the .svn directories.  The easiest way to do this is run this command from the starting directory of the project, before you import it into SVN!
find . -name ‘.svn’ | xargs rm -rf

Be sure to not put a * at the end, that will delete a lot more than expected!

Based on the AnvilPortal sample there are several file and directories we are going to need:

1 – The common ant build files from the anvil/dist:
flex.ant.xml    java.ant.xml    utils.ant.xml
2 – The config files from anvil/anvilPortal/etc – these are use to configure the basic security and java application options
3 – The /anvil/anvilPortal/web directory – be sure to delete the crossdomain.xml file!  These file configure BlazeDS remoting and the starting web page
4 – From the main /anvil/anvilPortal directory copy the following files:
anvil.conf  – This file configures the anvil compile process and the eclipse project file generator
common.ant.xml  –  This defines the common ant build tasks for the project.  This makes it easier to reuse ant tasks across multiple modules.
build.properties  – This defines the basic build properties
build.xml – The main build file

Now we need to modify the files to fit the new project.  Here is a list of recommended changes:

1 – build.properties – change the project.name.  Also if you want to use a non-standard directory layout you can change where the files are located in this file.  project.root is the parent folder of the project.  project.tail is the directory of the current module that is being compiled.
2 – In build.xml change anvil.root to point to the same directory as the project.root (or even better you could do a global search and replace across the entire project and replace anvil.root with project.root).
3 – Now in build.xml change the project name and the name of modules you are defining.
4 – Modify the config files in the etc directory according to your project.

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏