Skip to content

How to develop a new flavour

jrgsanchez edited this page Jan 9, 2017 · 1 revision

If you have an idea, show it to us! The project has been designed with the capacity of developing new flavours in a simple way.

Just create a Maven module and be sure your new module follows these points:

  • Add the Core dependency.
  • Define the generation workflow.
  • Design the Velocity files.
  • Create the launcher.
  • Define the ANT task.
  • Make your module visible in the website.

Adding the Core dependency

Add the dependency to the POM file.

    <dependency>
        <groupId>com.bbva.kltt</groupId>
        <artifactId>KLTT-APIRestGeneratorCore</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>

Defining the generation workflow

You have to create a class which extends GeneratorGlobalLanguage class. GeneratorGlobalLanguage contains the generate method that is the main method of the code generation, it can be overridden to define your own workflow. You can do whatever you want, the only condition is: when the execution of this method ends, the code must have been generated and stored in the output folder because it will be sent to the web client. Every module must have its own Generator. The next piece of code is an example of the generate method:

/**
* @param translatorType with the translator type
* @throws APIRestGeneratorException with an occurred exception
*/
public void generate(final String translatorType) throws APIRestGeneratorException
{
        // Verify the restrictions
        this.verifyRestrictions();
        
        // Initialize file directories
        this.initializeFileDirectories();
        
        // Generate the top level POM file
        this.generateTopLevelPom(translatorType);
        
        // Generate the Java Models - Project
        this.generateModelsProject();
        
        // Generate the Java Rest interface - Project
        this.generateRestInterfaceProject();
        
        // Generate the Java Rest implementation - Project
        this.generateRestImplementationProject();
        
        // Generate the Java example project
        this.generateExampleProject();
        
        // Finally, generates the top level POM file
        this.generateMavenInstallFile(translatorType);
}

Designing the Velocity files

API Rest Generator uses Apache Velocity to generate the new files, independent of the programming language. In summary, the main steps are:

  • Create a new class which extends GeneratorBase class.
  • Add the variables you need in the Velocity file, overriding the createVelocityContext method.
  • Create the Velocity file. It must have the same name and package as the new Java class but the difference is that the package and file must be under resources folder.
  • Instantiate the new Java class and call its generate method.

Creating the launcher

The launcher is a class that starts the generation process. It has to extend GlobalLauncher class and contain the main method which will be called by the ANT task. The main method receives an array with these Strings:

  • Path of the API Specification file.
  • Path of the generatorBuilder folder.
  • Type of the parser.

In the main method, you will have to call the launch method with these parameters:

  • API Specification path and generatorBuilder folder's path in an array of Strings.
  • The module name (it is suggested to define it as a constant).
  • The name of the generator class (it is the name of the class you have defined before, it can be defined as a constant).
  • Type of the parser.

Defining the ANT task

API Rest Generator uses Apache ANT to generate the code and compile it. In the generatorBuilder folder, you can find two XML files where the ANT tasks (Run the generator, Create the folders, Compile and Install with Maven, ...) are defined. To execute the ANT tasks, it is necessary a class that extends GlobalAntExecutor class and call its super constructor method with the properties of your ANT task:

  • Path of generatorBuilder directory.
  • Path of the XML file where the ANT task is defined (If one of the two files, that are already in the project, suits your needs, use it).
  • The module name that you have defined as a constant.
  • Class of the launcher you have created in the step before.
  • Type of parser (only swagger is available).

Making the module visible

Just create a class that extends AbstractDependencyUtility class and call the super constructor method with the properties of your new module.

  • Type of the module (server or client).
  • Name of the module to show (i.e. Spring MVC).
  • Name of the module (i.e. serverSpring).