ae.maven.ActiveEntityGeneratorMojo.java Source code

Java tutorial

Introduction

Here is the source code for ae.maven.ActiveEntityGeneratorMojo.java

Source

/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/
package ae.maven;

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import ae.config.Project;
import ae.generator.Codifier;

/**
 * Active Entity mojo to perform class generation from Project configuration. This class is an Maven adapter to DefaultClassGenerator class.
 * 
 * @since 3.0
 * 
 * @phase generate-sources
 * @goal generate
 */
public class ActiveEntityGeneratorMojo extends AbstractMojo {

    /**
     * Destination directory for Java classes (ignoring their package names).
     * 
     * @parameter expression="${generate.destinationDirectory}" default-value= "${project.build.sourceDirectory}/java/generated-sources/ae"
     */
    private File destinationDirectory;

    /**
     * Project class to use as a base for class generation.
     * 
     * @parameter expression="${generate.ae}"
     * @required
     */
    private String ae;

    /**
     * Location of StringTemplate template file for superclass generation.If omitted, default template is used.
     * 
     * @parameter expression="${generate.superTemplate}" default-value="active_entity_base_definition"
     */
    private String superTemplate;

    /**
     * Location of StringTemplate template file for Entity class generation. If omitted, default template is used.
     * 
     * @parameter expression="${generate.template}" default-value="active_entity_definition"
     */
    private String template;

    /**
     * Location of StringTemplate template file for superclass naming. If omitted, default template is used.
     * 
     * @parameter expression="${generate.superFilename}" default-value="base_class_name"
     */
    private String superFilename;

    /**
     * Location of StringTemplate template file for Entity class generation. If omitted, default template is used.
     * 
     * @parameter expression="${generate.filename}" default-value="class_name"
     */
    private String filename;

    /**
     * Location of StringTemplate template files for class generation. If omitted, default template is used.
     * 
     * @parameter expression="${generate.templatesDirectory}" default-value="src/main/config/ae-templates"
     */
    private File templatesDirectory;

    /**
     * extension o generated files.
     * 
     * @parameter expression="${generate.ext}" default-value="java"
     */
    private String generatedExtension;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (this.ae == null) {
            throw new MojoExecutionException("no ae project configured");
        }
        if (!this.destinationDirectory.exists()) {
            this.destinationDirectory.mkdirs();
        }
        try {
            final Project aeProject = buildActiveEntityProject();
            final Codifier codifier = new Codifier();
            codifier.output(this.destinationDirectory.getAbsolutePath()).templatesDir(this.templatesDirectory)
                    .extension(this.generatedExtension).modelTemplate(this.template).modelFileName(this.filename)
                    .baseTemplate(this.superTemplate).baseFileName(this.superFilename)
                    .javaPackage(aeProject.getDefaultPackage());
            codifier.generateCodeFor(aeProject);
        } catch (final Exception e) {
            throw new MojoExecutionException("Error generating classes: ", e);
        }
    }

    private Project buildActiveEntityProject()
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        final Class<?> projectClass = Class.forName(this.ae);
        if (!Project.class.isAssignableFrom(projectClass)) {
            throw new IllegalArgumentException("[" + this.ae + "] does not extend [" + Project.class + "].");
        }
        final Project aeProject = (Project) projectClass.newInstance();
        return aeProject;
    }
}