【问题标题】:Using Maven to build Flex project including stylesheets使用 Maven 构建包含样式表的 Flex 项目
【发布时间】:2011-03-18 18:57:05
【问题描述】:

Flex 样式表由编译器解析并与它们的资产(通常是图形)捆绑到一个 swf 文件中,然后可以在运行时加载该文件。我有一个具有固定数量风格的项目(目前有 4 种:旧公司风格、新公司风格和两者的高对比度版本)。使用 Flash Builder(带有 Flex IDE 插件的 Eclipse),我可以将所有 .css 文件与主要项目工件 .swf 文件一起编译为 .swf 文件。这也可以通过 ant 脚本实现。

我目前的理解是,Maven 只想为每个项目(POM 文件)创建一个工件,但可能会添加一些额外的工件(如 zip 打包)。出于可扩展性的原因 - 我有许多库和模块项目的复杂设置,其中一些有自己的单独样式表 - 将项目拆分为“主”项目并为每个样式表复制是非常不切实际的。至少在 Eclipse 项目方面,每个子文件夹都有 POM 文件,所有子文件夹都由主 pom 文件引用并引用相同的 src/ 位置(并且在一个 Eclipse 项目中)可能会起作用。虽然这很丑陋,并且每个人都需要单独的 artifactId,但仍然需要以某种方式组装。

重要的是能够拥有一个包含 (Eclipse) 项目主 swf 文件和每个样式表 swf 文件(以及一些静态文件,如要在运行时加载的本地化文本)的最终程序集。这将是我在in a separate question 描述的几个项目的大型集合的一部分。

我正在使用带有 Flash Builder 插件(即 Flex4)的 Eclipse 3.6.1 (Helios),目标是 Flex SDK 3.5 (FlashPlayer 10.0)。我已经使用 Sonatypes flexmojos-maven-plugin 3.7.1 安装了 Maven 3.0.2(因为它似乎比来自 Servebox 的那个更活跃)。我已经为我的项目手动创建了 pom 文件并且它们可以工作(尽管只编译一个 swf 工件文件,这取决于我将主 .as 文件还是 .css 文件之一指定为 sourceFile)

我已经尝试了几天来理解“Maven 方式”(它似乎是为 Java 量身定制的,并不完全适合 Flex),但无法让它工作,所以我只有一个项目,或至少一个包含所有内容的程序集。

这里最好的方法是什么?

【问题讨论】:

  • 这与您之前的问题有何不同?
  • 它实际上是相当相关的,但我不确定是否应该将两者都打包到同一个问题中,所以我只是将它们链接起来。基本上在这里我问是否可以在一个 Maven 项目中生成多个工件。由于我目前的理解是 Maven 需要每个项目一个主要工件,Flex Mojo 只创建一个 swf 文件,那么如何设置它以生成它们可能只有一个 artifactId?另一个问题更上一层楼,如何将几个项目(更独立)组装在一起。我会对组合解决方案感到非常满意:)

标签: eclipse apache-flex actionscript-3 maven stylesheet


【解决方案1】:

我没有找到从同一个 POM 文件创建多个工件的方法,甚至没有多次运行 flexmojo 编译目标(即使与执行标签绑定,Maven 也会抱怨)。所以我发现最好的方法是使用与我的整个项目非常相似的解决方案,我已经描述了in this question, including my solution,我建议阅读它,因为我会以此为基础。我曾希望在这里有一个不那么骇人听闻的解决方案,使用这种多项目方法对我来说似乎有点脏。这也是我拆分问题的原因,这反过来又让我有幸被杰夫·阿特伍德本人注意到:)

我只想为我的插件使用一个 Eclipse 项目,但需要多个文件夹来使用多个 POM。我的文件夹结构如下所示:

colored_plugin/src/colored_plugin_main.as
colored_plugin/src/green.css
colored_plugin/src/...
colored_plugin/src/assets/rainbow.jpg          <- will be embedded into stylesheet swf file
colored_plugin/WebContent/assets/labels.xlf
colored_plugin/WebContent/config/config.xlf
colored_plugin/POMs/assembly/pom.xml
colored_plugin/POMs/plugin/pom.xml
colored_plugin/POMs/style1/pom.xml
colored_plugin/pom.xml
master_build/pom.xml
master_build/swf-assembly.xml
master_build/css-assembly.xml
master_build/plugin-assembly.xml
master_build/assembly/pom.xml
master_build/assembly/final-asembly.xml

我使用 master_build 文件夹作为中心位置来存储我的简单 swf 项目的 swf 程序集文件,因此我将包含样式表的插件的程序集描述符放在同一位置。

编译插件的 POM (POMs/plugin/pom.xml) 与简单的 swf 项目之一非常相似(请参阅其他问题):

<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>

    <parent>
        <groupId>de.zefiro.maven.example</groupId>
        <artifactId>colored_plugin_internal</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../</relativePath>
        </parent>

    <name>Colored Plugin (Plugin Executeable)</name>
    <artifactId>${project.parent.artifactId}_swf</artifactId>
    <packaging>swf</packaging>

    <build>
        <sourceDirectory>../../src</sourceDirectory>

        <!-- This name needs to be exactly specified, as the runtime will load it as it's specified in the configuration xml -->
        <finalName>SampleProjectModuleColored</finalName>

        <plugin>
            <groupId>org.sonatype.flexmojos</groupId>
            <artifactId>flexmojos-maven-plugin</artifactId>
            <version>3.7.1</version>
            <configuration>
                <sourceFile>colored_plugin_main.as</sourceFile>
            </configuration>
        </plugin>

        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>../../../master_build/swf-assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>WebContent-packaging</id>
                        <phase>package</phase>
                        <goals><goal>single</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${group-id}</groupId>
            <artifactId>project_swc</artifactId>
            <version>${project.version}</version>
            <scope>${zefiro.swf.scope}</scope> <!-- tell Flex Mojo that we want to compile using runtime shared libraries (rsl) -->
            <type>swc</type>
        </dependency>
        [...]
    </dependencies>

</project>

样式表的 POM 是一个几乎相同的版本:.css 文件被指定为源,这里不需要依赖项(至少在我的情况下),但它仍然需要打包以便我以后可以为程序集引用(并重新打包)它。

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>de.zefiro.maven.example</groupId>
        <artifactId>colored_plugin_internal</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../</relativePath>
    </parent>

    <name>Colored Plugin (Stylesheet 'Green')</name>
    <artifactId>${project.parent.artifactId}_css_green</artifactId>
    <packaging>swf</packaging>

    <build>
        <sourceDirectory>../../src</sourceDirectory>
        <finalName>style_green</finalName>
        <plugins>
            <plugin>
                <groupId>org.sonatype.flexmojos</groupId>
                <artifactId>flexmojos-maven-plugin</artifactId>
                <version>3.7.1</version>
                <configuration>
                    <sourceFile>green.css</sourceFile>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>../../../master_build/css-assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>WebContent-packaging</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

程序集描述符也是简化版,只是打包了swf文件。我不确定它是否需要,也许可以直接使用默认的 Maven 工件来完成组装,但对我来说,就像在其他所有项目中一样进行组装是最简单的。

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>Stylesheet</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- Take the stylesheet swf artifact (only, not the other stuff which is generated there) -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.swf</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

组装项目有这个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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>de.zefiro.maven.example</groupId>
        <artifactId>colored_plugin_internal</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../</relativePath>
    </parent>

    <name>Colored Plugin (Assembly)</name>
    <artifactId>colored_plugin</artifactId>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>../../../master_build/plugin-assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>Plugin_assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${group-id}</groupId>
            <artifactId>${project.parent.artifactId}_swf</artifactId>
            <version>${project.version}</version>
            <type>zip</type>
            <classifier>WebContent</classifier>
        </dependency>
        <dependency>
            <groupId>${group-id}</groupId>
            <artifactId>${project.parent.artifactId}_css_green</artifactId>
            <version>${project.version}</version>
            <type>zip</type>
            <classifier>Stylesheet</classifier>
        </dependency>
    </dependencies>
</project>

插件程序集描述符将所有这些打包在一起。它也位于中心位置,因此以通用方式编写:它从调用 POM 引用 group-id 和 artifactId 并使用我介绍的名称约定。最终项目将由程序集 POM 构建,因此我指定了“不错”的 artifactId。主项目 POM(见下文)有一个手动派生的名称,而所有其他项目(主应用程序和所有样式表)都自动派生于此并生成仅在内部使用的 Maven 工件。我有不止一个样式表,所以我真正的程序集 POM 列出了多个样式表依赖项(与本示例相反),而我的程序集描述符使用通配符将它们全部获取。

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>WebContent</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- add the WebContent folder -->
        <fileSet>
            <directory>../../WebContent</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <!-- add plugin main executable -->
        <dependencySet>
            <includes>
                <include>${group-id}:${project.parent.artifactId}_swf:zip:WebContent:${project.version}</include>
            </includes>
            <useProjectAttachments>true</useProjectAttachments>
            <unpack>true</unpack>
            <outputDirectory>/</outputDirectory>
        </dependencySet>
        <!-- add stylesheets -->
        <dependencySet>
            <includes>
                <include>${group-id}:${project.parent.artifactId}_css_*:zip:Stylesheet:${project.version}</include>
            </includes>
            <useProjectAttachments>true</useProjectAttachments>
            <unpack>true</unpack>
            <outputDirectory>/assets/styles</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

请注意,我对这个程序集描述符使用的 ID 与用于简单 swf 项目的 ID 相同(如我的另一个问题所示)。这使得在整个程序集插件中指定正确的工件变得更加容易,因为我可以只使用“WebContent”作为我所有项目的分类器。

最后,我项目的主要 POM 只是其他 POM 的一个小调用者:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>de.zefiro.maven.example</groupId>
        <artifactId>full_build</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../master_build</relativePath>
    </parent>

    <name>Colored Plugin</name>
    <artifactId>colored_plugin_internal</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>pom/plugin</module>
        <module>pom/style1</module>
        <module>pom/assembly</module>
    </modules>

</project>

可以通过以下方式添加新样式表:

  • 将 red.css 样式表和所有随附文件添加到 src/ 目录
  • 将 coloured_plugin/POMs/style1/pom.xml 复制到 colour_plugin/POMs/style2/pom.xml
  • 将其作为模块添加到 coloured_plugin/pom.xm
  • 将其添加到 coloured_plugin/POMs/assembly/pom.xm 作为依赖项
  • 由于通配符和命名约定,程序集描述符 (/master_build/plugin-assembly.xml) 不需要更改

最终的项目工件是一个 zip 文件,由我的主程序集使用(并与其他项目合并)并包含这些文件:

SampleProjectModuleColored.swf
assets/labels.xlf
assets/styles/style_green.swf
config/config.xlf

它将存储在存储库中

de.zefiro.maven.example:colored_plugin:zip:WebContent:1.0-SNAPSHOT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2014-03-12
    • 2016-11-11
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多