Sometimes you want to turn on and off certain features in your code, pass in to the code constants defined in your build. 

So-called "defines" are used for this. Here comes a configuration example for this:

<plugin>
    <groupId>net.flexmojos.oss</groupId>
    <artifactId>flexmojos-maven-plugin</artifactId>
    <version>7.1.0</version>
    <extensions>true</extensions>
    <configuration>
		<defines>
		    <property>
        		<name>CFG::VERSION</name>
		        <value>"STANDARD"</value>
		    </property>
		    <property>
        		<name>CFG::DEBUG</name>
		        <value>true</value>
		    </property>
		    <property>
        		<name>CFG::LOGLEVEL</name>
		        <value>2</value>
		    </property>
		</defines>
    </configuration>
</plugin>

Note that for String defines, you need to escape the constant with double quotes.

In the code these defines are referenced the following way:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[

        public static const VERSION:String = CFG::VERSION;
		public static const DEBUG:Boolean = CFG::DEBUG;
		public static const LOGLEVEL:Number = CFG::LOGLEVEL;

        ]]>
    </fx:Script>

	<s:Label text="Hello World from the {VERSION} edition. Debug is set to {DEBUG} and Loglevel to {LOGLEVEL}" />
</s:Application>

But you can also include/exclude entire code-blocks from the compilation:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			creationComplete="onCreationComplete(event)">

    <fx:Script>
        <![CDATA[
 
		public function onCreationComplete(event:FlexEvent):void {
			log();
		}
		CFG::FLEX3
		public static function log():void {
			trace("output some log trace for Flex 3");
		}
 
		CFG::FLEX4
		public static function log():void {
			trace("output some log trace for Flex 4");
		}

        ]]>
    </fx:Script>
</s:Application>

The first implementation is used if "CFG::FLEX3" is set and the second if "CFG::FLEX4" is set. If both or none are set, you get compile errors.

Using Profiles

Some times you will want to enable/disable the defines depending on the profiles enabled:

<plugin>
    <groupId>net.flexmojos.oss</groupId>
    <artifactId>flexmojos-maven-plugin</artifactId>
    <version>7.1.0</version>
  	<extensions>true</extensions>
    <configuration>
		<defines>
		    <property>
        		<name>CFG::DEBUG</name>
		        <value>false</value>
		    </property>
		</defines>
    </configuration>
</plugin>
 
...
 
<profiles>
    <profile>
        <id>debug-version</id>
        <build>
            <plugins>
				<plugin>
				    <groupId>net.flexmojos.oss</groupId>
 				    <artifactId>flexmojos-maven-plugin</artifactId>
				    <extensions>true</extensions>
				    <configuration>
						<defines>
						    <property>
				        		<name>CFG::DEBUG</name>
						        <value>true</value>
						    </property>
						</defines>
				    </configuration>
				</plugin>
            </plugins>
        </build>
    </profile>
</profiles>

So if you build the project with the "debug-version" profiled enabled, the define will be set to true, otherwise to false.

Creating multiple editions

In one case, a user was asking for a way to build two versions of the same lib in one build. He wanted to compile a "standard" and an "enterprise" version. The following configuration would create two SWFs in the target directory. One with the "CFG::VERSION" set to "STANDARD" and one with this set to "ENTERPRISE". In order to do this, we needed to add an additional "execution" to the configuration. As both would overwrite each other, in order to prevent this, for the second execution we simply make Flexmojos give the output a different name:

<plugin>
    <groupId>net.flexmojos.oss</groupId>
    <artifactId>flexmojos-maven-plugin</artifactId>
    <version>7.1.0</version>
    <extensions>true</extensions>
    <executions>
        <execution>
            <id>compile-enterprise</id>
            <goals>
                <goal>compile-swf</goal>
            </goals>
            <configuration>
                <finalName>${artifactId}-enterprise-${version}</finalName>
                <defines>
                    <property>
                        <name>CFG::VERSION</name>
                        <value>"ENTERPRISE"</value>
                    </property>
                </defines>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <defines>
            <property>
                <name>CFG::VERSION</name>
                <value>"STANDARD"</value>
            </property>
        </defines>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.apache.flex</groupId>
            <artifactId>compiler</artifactId>
            <version>4.14.1</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</plugin>

This will create two SWFs. One named "{artifactId}-{verion}.swf" and one "{artifactId}-enterprise-{verion}.swf" with the difference that the enterprise version will have all the enterprise features enabled that were turned on by the defines.

  • No labels