Using Maven to build Flex project including stylesheets

拥有回忆 提交于 2020-01-17 01:22:34

问题


Flex stylesheets are parsed by the compiler and bundled with their assets (graphics, usually) into a swf file, which can then be loaded at runtime. I've got a project with a fixed number of styles (currently 4: old corporate style, new corporate style, and high contrast versions of both). Using Flash Builder (Eclipse with Flex IDE plugin) I'm able to have all .css files compiled to .swf files alongside the primary project artifact .swf file. This is also possible with ant scripts.

My current understanding is that Maven wants to only create one artifact per project (POM file), but may have some additional ones added (like zip packaging). For scalability reasons - I've got a complex setup of many library and module projects, several of them having their own individual stylesheets - it would be very impractical to split up the projects into the 'main' project and copies for each stylesheet. At least on the Eclipse project side, having some subfolders with POM files in each, all refered by a master pom file and referring the same src/ location (and being inside one Eclipse project) would probably work. Though that's ugly and needs individual artifactIds for each, and still need to be assembled somehow.

The important thing is to be able to have a final assembly which contains the (Eclipse) projects main swf file and each stylesheets swf file (and some static files like localized texts to be loaded at runtime). This will be part of a large assembly of several of those projects which I've described in a separate question.

I'm using Eclipse 3.6.1 (Helios) with Flash Builder Plugin (i.e. Flex4), targetting Flex SDK 3.5 (FlashPlayer 10.0). I've installed Maven 3.0.2, using Sonatypes flexmojos-maven-plugin 3.7.1 (as it seems more active than the one from Servebox). I've manually created pom files for my projects and they work (though only compiling one swf artifact file, depending on whether I specify the main .as or one of the .css files as sourceFile)

I've tried for some days now to understand "The Maven Way" (which seems to be tailored for Java and not fitting perfectly for Flex), but couldn't get this to work so that I have a single project, or at least a single assembly with everything in it.

What would be the best way to do this here?


回答1:


I did not find a way to create multiple artifacts from the same POM file, not even to run the flexmojo compile target more than once (even when binding with execution tag, Maven will complain). So the best way I found was using a quite similar solution as for my overall project, which I've described in this question, including my solution, and which I suggest reading, as I'll base on that. I had hoped for a less hackish solution here, using this multi-project-approach seems a bit dirty to me. That's also why I split the question, which in turn got me the honor of being noticed by Jeff Atwood himself :)

I do want to use only one Eclipse project for my plugin, but need several folders to use multiple POMs. My folder structure looks like this:

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

I'm using the master_build folder as a central location to store my swf-assembly file for my simple swf projects, so I put my assembly descriptor for the plugins containing stylesheets in the same location.

The POM compiling the plugin (POMs/plugin/pom.xml) is quite similar to the one of the simple swf projects (see other question):

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

The POM for the stylesheet is a nearly identical version of it: the .css file is specified as source, dependencies are not needed here (at least in my case), but it still needs to be packaged so that I can later reference (and repack) it for the assembly.

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

The assembly descriptor is also a simplified version, only packaging the swf file. I'm not sure it's needed and perhaps the assembly could be done with the default Maven artifact directly, but it was easiest for me to just do it like in every other project.

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

The assembly project has this 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>

The plugin assembly descriptor packs all this together. It's also in a central location and thus written in a generic way: It references group-id and artifactId from the calling POM and utilizing a name convention I've introduced. The final project will be build by the assembly POM, thus there I specify the 'nice' artifactId. The main project POM (see below) gets a manually derived name, while all other projects (main application and all stylesheets) are automatically derived from this and produce Maven artifacts which are only used internally. I've more than one stylesheet, so my real assembly POM lists multiple stylesheet dependencies (as opposed to this example), whereas my assembly descriptor takes them all using a wildcard.

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

Note that I used the same ID for this assembly descriptor as I've used for the simple swf projects (as shown in my other question). This makes it easier to specify the correct artifact in the overall assembly plugin, as I can just use 'WebContent' as classifier for all my projects.

Finally, the main POM for my project is just a small caller for those other POMs:

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

A new stylesheet can be added by:

  • adding the red.css stylesheet and all accompanying files to the src/ directory
  • duplicating the colored_plugin/POMs/style1/pom.xml to colored_plugin/POMs/style2/pom.xml
  • adding it to colored_plugin/pom.xm as a module
  • adding it to colored_plugin/POMs/assembly/pom.xm as a dependency
  • due to the wildcard and naming convention, the assembly descriptor (/master_build/plugin-assembly.xml) does not need a change

The final project artifact is a zip file to be consumed (and merged with the other projects) by my master assembly and contains those files:

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

It will be stored in the repository as

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


来源:https://stackoverflow.com/questions/5356589/using-maven-to-build-flex-project-including-stylesheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!