All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.itroadlabs.apicross.mavenplugin.SourceCodeGeneratorMojo Maven / Gradle / Ivy

The newest version!
package io.github.itroadlabs.apicross.mavenplugin;

import io.github.itroadlabs.apicross.CodeGenerator;
import io.github.itroadlabs.apicross.CodeGeneratorOptions;
import io.github.itroadlabs.apicross.utils.OpenApiSpecificationParseException;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.util.List;


@Mojo(name = "generate-code")
public class SourceCodeGeneratorMojo extends AbstractMojo {
    @Parameter
    private String specUrl;
    @Parameter
    private String generatorClassName;
    @Parameter
    private CodeGeneratorOptions generatorOptions;

    @Override
    public void execute() throws MojoExecutionException {
        CodeGenerator codeGenerator;

        try {
            codeGenerator = (CodeGenerator) Class.forName(generatorClassName).newInstance();
        } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
            getLog().error(e.getMessage(), e);
            throw new MojoExecutionException(e.getMessage(), e);
        }

        try {
            codeGenerator.setSpecUrl(specUrl);
            codeGenerator.setOptions(generatorOptions);
            codeGenerator.generate();
        } catch (OpenApiSpecificationParseException spe) {
            SwaggerParseResult swaggerParseResult = spe.getSwaggerParseResult();
            List messages = swaggerParseResult.getMessages();
            StringBuilder stringBuilder = new StringBuilder();
            for (String message : messages) {
                stringBuilder.append(message).append("\n");
            }
            getLog().error(stringBuilder.toString());
            throw new MojoExecutionException(spe.getMessage(), spe);
        } catch (Exception e) {
            getLog().error(e.getMessage(), e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy