On Flex we have several ways to integrate java-server and flex-client. The most used are RemoteObjects and/or Dataservices.
Using this, we have typed communication, but, it mean we must have the same DTO (Data Transfer Object or ValueObject or Pojo or whatever you wanna call it) on both sides. That mean 2 sources files with same data.
Duplicating information is the first step on getting out-to-date information. A good way to avoid this is write one side DTO and generate other. Is exactly that what Granite GAS3 Generator does.
Gas 3 is a AntTask to generate .as files based on Java .class files. In other to get this behavior, flex-mojos uses Gas3's generator. It works on a similar way. But it doesn't run over a java project, it runs on a flex project.
There is a sample here.
The steps to get code generation are simple:
1 - Add generator mojo goal to your pom:
Code Block |
---|
<build>
<plugins>
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>%{flexmojos.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> |
2 - Add a jar dependency, who contains Java DTO classes:
Code Block |
---|
<dependencies>
<dependency>
<groupId>org.sonatype.flexmojos.itest</groupId>
<artifactId>generator-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies> |
All jar dependencies will be loaded at runtime. To filter what classes to generate use includeJavaClasses/excludeJavaClasses configuration.
To view all configuration options:
Code Block |
---|
mvn help:describe -DgroupId=org.sonatype.flexmojos -DartifactId=flexmojos-maven-plugin -Dfull=true
|
3 - Compile your project.
At this moment, it generate the sources with GAS3 style, but, you can configure your custom template.
4 - Configure your custom templates
To use custom templates it is necessary to configure flexmojos-maven-plugin to use flexmojos-generator-graniteds version 2.0. If this is not done, the custom templates are not used properly!
Code Block |
---|
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<beanTemplate>
<template>${project.basedir}/generatorTemplates/beanBase.gsp</template>
<template>${project.basedir}/generatorTemplates/bean.gsp</template>
</beanTemplate>
<entityTemplate>
<template>${project.basedir}/generatorTemplates/entityBase.gsp</template>
<template>${project.basedir}/generatorTemplates/entity.gsp</template>
</entityTemplate>
<includeJavaClasses>
<includeClass>org.example.TranslationItem</includeClass>
<includeClass>org.example.TranslationItems</includeClass>
<includeClass>org.example.TranslationNotFoundException</includeClass>
<includeClass>org.example.TranslationAlreadyExistsException</includeClass>
</includeJavaClasses>
</configuration>
</execution> |
This example shows an example configuration.
- <generatorToUse> this settings configures flexmojos-maven-plugin to use the generator version 2
- <beanTemplate> here you can give your custom templates vor the bean creation. Please keep in mind the paths are only examples an must be adjusted to your project.
- <entityTemplate> same like <beanTemplate>
- <includeJavaClasses> give here all classes which shall be created
Granite 1.x, 2.x and 2.1 support on Flexmojos 3.6
By default generator mojo will only support one kind of granite generator... meaning you can only use graniteds21 with flexmojos 3.6
If you need to use graniteds2 or granite1 use this new approach:
Code Block |
---|
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-generator-maven-plugin</artifactId>
<version>${flexmojos.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorToUse>graniteds2</generatorToUse>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-generator-graniteds-2.0.0</artifactId>
<version>${flexmojos.version}</version>
</dependency>
</dependencies>
</plugin> |
Info | ||
---|---|---|
| ||
Pay attention to plugin artifactId: flexmojos-generator-maven-plugin |
The same logic goes for granite1.
Warning |
---|
YOU ONLY NEED TO GO DOWN THIS ROAD WHEN YOU NEED TO USE OTHER GRANITE GENERATOR BESIDES THE DEFAULT. AS USUAL YOU CAN ONLY USE ONE GRANITE GENERATOR PER REACTOR, DUE TO API INCOMPATIBILITIES BETWEEN GRANITE GENERATOR RELEASES. |