---
title: "基于Maven的Flex/Webapp项目示例"
author: "bupter520"
date: "2010-09-15T15:45:03Z"
tags: ["Flex", "Java", "Tomcat", "maven", "项目管理"]
categories: ["编程语言"]
slug: "maven-flex-webapp"
draft: false
lastmod: "2010-09-15T15:45:03Z"
id: 763978
url: "https://bupter520.iteye.com/blog/763978"
---

这是一个配置为多模块maven项目的Flex3/J2EE项目示例。你可以以这种结构为基础来构建自己的项目。Flex项目应当看作是Web应用项目(WAR)的依赖项。使用Maven Dependency插件，SWF构件就会被自动复制到web应用项目的根目录中。

此示例项目包括三个模块：

- flex-gui :SWF构件，
- java-service : Java facade 示例，
- web-app : Web应用程序，同时依赖于flex-ui和java-services构件.

**flex-gui 项目**

“flex-gui”项目是一个典型的包含.as和.mxml源文件的Flex项目。这个模块产生SWF构件（组装：SWF）。

**java-service 项目**

“java-service”项目是一个包含java facade的标准java项目，该java façade为GUI提供原始数据源。这个模块产生JAR构件（组装：JAR）。

**web-app 项目**

“web-app”项目是包含配置文件的Web应用程序项目，这些配置文件需部署到Tomcat等应用服务器容器中。其他资源如JSP, Servlet，图片等也需要包括进来。这个模块产生WAR构件（组装：WAR）。

在你自己的项目中也可以添加其他模块进来，进一步增强业务层或持久层。

**必要条件**

创建这个项目需要 :

- [Maven 2.0.x](http://maven.apache.org/ "Apache Maven"),
- 最新版的BlazeDS-Turnkey或者Tomcat 实例.

**依赖项**

- 示例基于Maven Flex Plugin 2.1.1. 更多信息请参见项目主页.
- Flex SDK版本3.2.0.3958.
- FlexBuilder 3.0.2.xxx集成开发环境,或其它文本编辑器.

**多模块项目布局**

    ...

    +sampleProject

      -pom.xml ( the parent pom )

      +flex-gui

        -pom.xml

        +src

          +main

            +flex

              + org.servebox.sample ( All as/mxml sources here )

          +resources

          +test

            +flex

              + org.servebox.test.sample ( All classes for unit test here )

      +java-service

        -pom.xml

        +src

          +main

            +java

              + org.servebox.sample ( All java sources here )

          +test

            +java

              + org.servebox.test.sample ( All classes for unit tests here )

      +web-app

        -pom.xml

        +src

          +main

            +resources

              - additional resources for webapp ( such as images, video etc... )

            +webapp

              +WEB-INF

                +flex

                  -services-config.xml

                -web.xml

**POM概述**

**父类POM**

**Main部分**

父类pom使用这个pom组装(这是“aggregator project”) :

    **&lt;modelVersion&gt;**4.0.0**&lt;/modelVersion&gt;**

    **&lt;groupId&gt;**org.servebox.sample.multi-module-app**&lt;/groupId&gt;**

    **&lt;artifactId&gt;**multi-module-sample**&lt;/artifactId&gt;**

    **&lt;version&gt;**1.0-SNAPSHOT**&lt;/version&gt;**

    **&lt;packaging&gt;**pom**&lt;/packaging&gt;**

    **&lt;name&gt;**Flex3-J2EE-MultiModules-Sample-Application**&lt;/name&gt;**

**Modules部分**

模块部分定义以下三个子模块:

    **&lt;modules&gt;**

        **&lt;module&gt;**flex-gui**&lt;/module&gt;**

        **&lt;module&gt;**java-service**&lt;/module&gt;**

        **&lt;module&gt;**web-app**&lt;/module&gt;**

    **&lt;/modules&gt;**

**插件存储库和存储库**

以下插件存储库直接定义了Maven Flex 插件的位置：

    **&lt;pluginRepositories&gt;**

        **&lt;pluginRepository&gt;**

            **&lt;id&gt;**servebox**&lt;/id&gt;**

            **&lt;name&gt;**ServeBox Plugin Repository**&lt;/name&gt;**

            **&lt;url&gt;**http://maven.servebox.org/repository**&lt;/url&gt;**

        **&lt;/pluginRepository&gt;**

    **&lt;/pluginRepositories&gt;**

以下存储库定义了Flex SDK和ActionScript Foundry依赖项的位置：

    **&lt;repositories&gt;**

        **&lt;repository&gt;**

            **&lt;id&gt;**servebox**&lt;/id&gt;**

            **&lt;name&gt;**ServeBox Plugin Repository**&lt;/name&gt;**

            **&lt;url&gt;**http://maven.servebox.org/repository**&lt;/url&gt;**

        **&lt;/repository&gt;**

    **&lt;/repositories&gt;**

**Build部分**

**将项目导入到Eclipse/FlexBuilder工作空间**

通过Maven Eclipse插件和Maven Flex插件， Maven可以针对Flex、Java特性自动生成eclipse项目属性文件。下载实例项目，加压到你的工作空间目录，为这个多模块项目另外创建一个工作空间目录，然后在该多模块项目的根目录下运行以下命令行代码：

mvn flex:eclipse eclipse:eclipse

在导入“现有项目”到工作空间后，你需要定义M2\_REPO变量便于Flex和Java使用，参看以下两个截图：

**构建项目**

首先, 你得在根项目的pom.xml中定义相关属性：

    **&lt;properties&gt;**

        **&lt;tomcat**.managerurl**&gt;**http://localhost:8400/manager**&lt;/tomcat**.managerurl**&gt;**

        **&lt;tomcat**.serverprofile**&gt;&lt;/tomcat**.serverprofile**&gt;**

        **&lt;webapp**.name**&gt;**sample-multi-project**&lt;/webapp**.name**&gt;**

        **&lt;tomcat**.server.root**&gt;**D:\\dev\\blazeds-turnkey-3.2.0.3978\\tomcat\\webapps\\${webapp.name}**&lt;/tomcat**.server.root**&gt;**

        **&lt;tomcat**.server.user**&gt;**tomcat**&lt;/tomcat**.server.user**&gt;**

        **&lt;tomcat**.server.password**&gt;**tomcat**&lt;/tomcat**.server.password**&gt;**

    **&lt;/properties&gt;**

上述配置可以根据自己的实际环境加以更改。

这个项目可能使用单个命令就可以构建。使用命令行工具，从根目录运行“mvn clean install”：包含在reactor中的所有项目将会被构建，并安装在你本地Maven存储库中 (默认情况下： ~/.m2/repository).

如果你想要这个应用程序自动部署到tomcat中，使用 “tomcat：exploded”，并确保：

- Tomcat实例处于运行中，
- 配置文件${tomcat.root]/conf/tomcat-users.xml包含“tomcat”用户 (这个对象用户Mavean管理Tomcat实例) :

**&lt;?xml** version='1.0' encoding='utf-8'**?&gt;**

**&lt;tomcat-users&gt;**

  **&lt;role** rolename="tomcat"**/&gt;**

  **&lt;role** rolename="manager"**/&gt;**

  **&lt;role** rolename="admin"**/&gt;**

  **&lt;user** username="tomcat" password="tomcat" roles="tomcat,admin,manager"**/&gt;**

  **&lt;user** username="admin" password="" roles="admin,manager"**/&gt;**

**&lt;/tomcat-users&gt;**

**下载源代码**

使用以下路径下载本文使用的源代码文件: [multi-module-sample-10-snapshot-source](http://www.servebox.org/wp-uploads/multi-module-sample-10-snapshot-source.zip)

Tags: [flex-gui](http://www.servebox.org/tag/flex-gui/), [java-service](http://www.servebox.org/tag/java-service/), [web-app](http://www.servebox.org/tag/web-app/)