`

用Maven构建Flex4项目实践记录

阅读更多

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

参考:

Maven Reference Chapter 13. Developing with Flexmojos: http://www.sonatype.com/books/mvnref-book/reference/flex-dev.html (将一些前因后果说的比较明确,建议首先阅读 )

Flexmojos Homepage: http://flexmojos.sonatype.org/

Getting Started: http://flexmojos.sonatype.org/getting-started.html

wiki: https://docs.sonatype.org/display/FLEXMOJOS/Home

maven与flex的结合(flexmojos): http://reuental.iteye.com/blog/652895

IntelliJ IDEA下使用FlexMojos : https://docs.sonatype.org/display/FLEXMOJOS/Tips+for+using+FlexMojos+with+IntelliJ+IDEA

-------------------------------------------------------------------------------------------------------------------------------------------------------------------


一、相关环境

OS: Ubuntu 9.10

Maven V2.2.1

Flexmojos V3.7.1

Flex SDK 4.1

 

二、准备工作

1、Maven全局设置:增加1个包含Flex framework的仓库

注:根据实践,不设置也可,在pom里指定也是OK的

2、将 Flash Player 加到环境变量 PATH 中以便支持单元测试

注:如果本机有FlexSDK,可在 %FLEXSDKHOME% / runtimes/player/ 下找到各OS平台对应的Flash Player 安装文件,安装即可;若本机没有,可以到Adobe网站下载安装。

 

、构建过程

1、通过模板创建webapp(命令行方式),终端执行:

mvn archetype:generate -DarchetypeRepository=http://repository.sonatype.org/content/groups/flexgroup -DarchetypeGroupId=org.sonatype.flexmojos -DarchetypeArtifactId=flexmojos-archetypes-modular-webapp -DarchetypeVersion=3.7.1

     根据提示依次输入 groupId、 artifactId、version、package,确认后进行构建,构建成功会产生如下结构的项目文件夹(testwebapp 为我输入的 artifactId 值):


 

注:由模板生成项目基础结构这一步,在几款IDE(本身支持Maven或安装Maven插件)中,在新建工程的向导引领下,同样可以完成。

 

2、默认创建出来的项目所使用的FlexSDK版本为3.2.0.3958,想要构建Flex4项目需要修改以下几处:

1)  编辑 %artifactId% / pom.xml  , 增加 仓库配置 和 FlexSDK 版本属性,修改后的 pom.xml 如下:

<?xml version="1.0"?>
<!--
    Copyright 2008 Marvin Herman Froeder
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.llp.flex</groupId>
  <artifactId>testwebapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>  

  <modules>
    <module>swc</module>
    <module>swf</module>
    <module>war</module>
  </modules>

  <!-- Flex SDK版本属性 -->
  <properties>
    <flex.sdk.version>4.1.0.16248</flex.sdk.version>
  </properties>

  <!-- Flex framework 和 Flexmojos 仓库 -->
  <repositories>
    <repository>
      <id>flexmojos</id>
      <url>http://repository.sonatype.org/content/groups/flexgroup/</url>
      <releases>  
        <enabled>true</enabled>  
      </releases>  
      <snapshots>  
        <enabled>true</enabled>  
      </snapshots>  
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>flexmojos</id>
      <url>http://repository.sonatype.org/content/groups/flexgroup/</url>
      <releases>  
        <enabled>true</enabled>  
      </releases>  
      <snapshots>  
        <enabled>true</enabled>  
      </snapshots>  
    </pluginRepository>
  </pluginRepositories>

</project>

 

2)  编辑 %artifactId% / swc / pom.xml  和 %artifactId% / swf / pom.xml :

     a) flexmojos-maven-plugin 下增加一些配置(FlexSDK 编译版本、Flash Player 版本等)

     b) 修改 flex-framework 的版本

     c) 修正parent 配置段的 artifactId 的值

     d) 增加 goals 配置(仅 %artifactId% / swf / pom.xml 需要配置)

 

     修改后的 pom.xml 如下(2个pom文件大部分是相同,故下面省略了一些内容):

<!--此处省略-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.llp.flex</groupId>
    <artifactId>testwebapp</artifactId><!--此处按实际的artifactId值进行修改-->
    <version>1.0-SNAPSHOT</version>
  </parent>

<!--此处省略-->
  <build>
    <sourceDirectory>src/main/flex</sourceDirectory>
    <testSourceDirectory>src/test/flex</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.sonatype.flexmojos</groupId>
        <artifactId>flexmojos-maven-plugin</artifactId>
        <version>3.7.1</version>
        <extensions>true</extensions>
        <!-- 生成一个调用SWF的HTML文件(%artifactId% / swf / pom.xml 独有配置) -->
        <executions>
          <execution>
             <goals>
              <goal>wrapper</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <locales>
            <locale>en_US</locale>
          </locales>
          <!-- 指定Flash Player版本 -->
          <targetPlayer>10.0.0</targetPlayer>
        </configuration>
        <!-- 指定编译版本 -->
        <dependencies>
          <dependency>
            <groupId>com.adobe.flex</groupId>
            <artifactId>compiler</artifactId>
            <version>${flex.sdk.version}</version>
            <type>pom</type>
           </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>com.adobe.flex.framework</groupId>
      <artifactId>flex-framework</artifactId>
      <version>${flex.sdk.version}</version>
      <type>pom</type>
    </dependency>

<!--此处省略-->
  </dependencies>

<!--此处省略-->
</project>
 

3、编译项目,终端执行:

mvn install

   过程中会调用FlashPlayer进行单元测试,成功后可以在各模块的 target 文件夹下找到编译后的成品;

   其中 %artifactId% / swf / target 下有一个 html 文件,浏览器打开该文件,可以看到 Hello World! 。

 

   至此,构建过程实践成功。

 

四、 在IDE下使用该项目(承接上文步骤)

1、IDE 为 Flex/Flash Builder 或 Eclipse

1)  生成 Flex/Flash Builder 项目文件,终端执行:

mvn flexmojos:flexbuilder

2)  运行 IDE,导入该项目即可。

 

2、IDE 为 Intellij IDEA

1)  编辑 %artifactId% / swc / pom.xml  和 %artifactId% / swf / pom.xml,  flexmojos-maven-plugin 下增加一项配置,修改后的 pom.xml 片段如下:

<!--此处省略-->
  <build>
    <sourceDirectory>src/main/flex</sourceDirectory>
    <testSourceDirectory>src/test/flex</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.sonatype.flexmojos</groupId>
        <artifactId>flexmojos-maven-plugin</artifactId>
        <version>3.7.1</version>
        <extensions>true</extensions>
        <!-- 生成一个调用SWF的HTML文件(%artifactId% / swf / pom.xml 独有配置) -->
        <executions>
          <execution>
             <goals>
              <goal>wrapper</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <locales>
            <locale>en_US</locale>
          </locales>
          <!-- for using FlexMojos with IntelliJ IDEA -->
          <configurationReport>true</configurationReport>
          <!-- 指定Flash Player版本 -->
          <targetPlayer>10.0.0</targetPlayer>
        </configuration>
        <!-- 指定编译版本 -->
        <dependencies>
          <dependency>
            <groupId>com.adobe.flex</groupId>
            <artifactId>compiler</artifactId>
            <version>${flex.sdk.version}</version>
            <type>pom</type>
           </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

<!--此处省略-->
 

2) 编译项目,终端执行:

mvn install

   编译后,在各个模块的 target 下会有一个文件名形如 ...config-report.xml 的文件。

 

3) 运行 Intellij IDEA,点击菜单 File -> Open Project, 选择 %artifactId% / pom.xml 即可;

     点击菜单 File - Project Structure - Facets - Flex, 可以看到 Flex SDK 的选项中有 compiler-4.1.0.16248.pom 。

 

注:

1) 由于我的OS是Ubuntu,没有相关编辑支持,所以导入IDE后也没有什么实际用处;即使IDE支持,由于项目类型为Maven,所以也得不到像Flex/AIR项目那样的build、run支持,一切都只能通过Maven来进行。

2) Intellij IDEA 下构建该项目获其他Flex项目时,无法选择该SDK ,并且存在一个错误:PHP home is not specified ,尚未解决。

 

 

五、 遇到问题与解决

Q1、运行 mvn install 时出现错误:

Flex compiler and flex framework versions doesn't match. Compiler: '3.2.0.3958' - Framework: '4.1.0.16248'.

解决:指定Flex SDK编译版本,详见上文 三 -> 2 -> 2)

 

Q2、运行 mvn install 时在执行 testcase 时失败:

Invalid state: the flashplayer is closed, but the sockets still running...

解决:指定Flash Player版本为10,详见上文 三 -> 2 -> 2)

 

Q3、运行 mvn install 时在执行 testcase 时失败:

Accept timed out .../TestRunner.swf

解决:同 Q2。

 

  • 大小: 15.5 KB
分享到:
评论
2 楼 JAVA-JVM 2013-04-07  
您好,我看了您的一篇文章“用Maven构建Flex4项目实践记录”,关于这边文章,我按照说明的步骤操作,直到mvn install这一步,都能够正常编译,但是到将编译后的项目文件,转换成Flex项目,也就是使用mvn flexmojos:flexbuilder命令的时候,总是提示找不到“flexmojos”这个插件,或者说找不到FlexBuilder,请问在将项目文件转换成Flex工程的时候需要做Flex环境的什么配置吗,希望能够回信,非常感谢。

关于flexmojos的mvn插件下载地址都按照说明已经配置。
1 楼 airlink 2010-10-18  
能不能反向做:先有FlexBuilder项目,然后配置pom的路径参数,最后可以自动化脚本编译?

相关推荐

Global site tag (gtag.js) - Google Analytics