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

com.aeontronix.genesis.mojo.TemplateMojo Maven / Gradle / Ivy

There is a newer version: 1.0-beta1
Show newest version
package com.aeontronix.genesis.mojo;

import com.aeontronix.genesis.*;
import com.kloudtek.util.FileUtils;
import com.kloudtek.util.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.awt.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Goal which touches a timestamp file.
 */
@Mojo(name = "template", defaultPhase = LifecyclePhase.INITIALIZE, requiresProject = false)
public class TemplateMojo extends AbstractMojo {
    @Parameter(defaultValue = "${template}", property = "template", required = true)
    private String template;
    @Parameter(defaultValue = "${target}", property = "target", required = true)
    private File target;
    @Parameter(property = "vars")
    private Map vars;
    /**
     * Variables file (.properties)
     */
    private File varsFile;
    @Parameter(property = "defaults")
    private Map defaults;
    @Parameter(defaultValue = "${nonInteractive}", property = "nonInteractive")
    private boolean nonInteractive;
    @Parameter(defaultValue = "${headless}", property = "headless")
    private boolean headless;
    @Parameter(defaultValue = "${genesis.skip}", property = "skip")
    private boolean skip;
    @Parameter(defaultValue = "${abort}", property = "abort")
    private String abort;
    /**
     * Delete the template file after executing
     */
    @Parameter(defaultValue = "${delete}", property = "delete")
    private boolean delete;

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public File getTarget() {
        return target;
    }

    public void setTarget(File target) {
        this.target = target;
    }

    public Map getVars() {
        return vars;
    }

    public void setVars(Map vars) {
        this.vars = vars;
    }

    public boolean isNonInteractive() {
        return nonInteractive;
    }

    public void setNonInteractive(boolean nonInteractive) {
        this.nonInteractive = nonInteractive;
    }

    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    public boolean isDelete() {
        return delete;
    }

    public void setDelete(boolean delete) {
        this.delete = delete;
    }

    public void execute() throws MojoExecutionException {
        if (!skip) {
            boolean disableHeadless = !headless && GraphicsEnvironment.isHeadless();
            try {
                if (disableHeadless) {
                    // !@#$@!#$!@#%$&!@#($&!@# anypoint / eclipse running maven headless
                    // brute forcing it back to system
                    try {
                        Field toolkit = Toolkit.class.getDeclaredField("toolkit");
                        toolkit.setAccessible(true);
                        toolkit.set(null, null);
                        Field headless = GraphicsEnvironment.class.getDeclaredField("headless");
                        headless.setAccessible(true);
                        headless.set(null, false);
                    } catch (Throwable e) {
                        getLog().error("Unable to get out of headless mode :(... template execution will probably fail if user input is required");
                    }
                }
                getLog().debug("Loading genesis template");
                Template template = Template.create(this.template);
                TemplateExecutor exec = new TemplateExecutor(template);
                if (varsFile != null) {
                    if (!varsFile.exists()) {
                        throw new MojoExecutionException("Vars file not found: " + varsFile.getPath());
                    }
                    if (vars == null) {
                        vars = new HashMap<>();
                    }
                    if (varsFile.getPath().endsWith(".properties")) {
                        Properties p = new Properties();
                        try (FileReader fr = new FileReader(varsFile)) {
                            p.load(fr);
                            for (Map.Entry entry : p.entrySet()) {
                                vars.put((String)entry.getKey(),(String)entry.getValue());
                            }
                        }
                    } else {
                        throw new MojoExecutionException("Invalid vars file extension: " + varsFile.getPath());
                    }
                }
                if (vars != null) {
                    exec.setVariables(vars);
                }
                if (defaults != null) {
                    exec.setDefaultOverrides(defaults);
                }
                exec.setHeadless(this.headless);
                exec.setNonInteractive(nonInteractive);
                getLog().info("Executing genesis template");
                exec.execute(target);
                getLog().info("Finished generate template project");
                if (StringUtils.isNotBlank(abort)) {
                    throw new MojoExecutionException(abort);
                }
            } catch (TemplateNotFoundException | InvalidTemplateException | IOException | TemplateExecutionException e) {
                getLog().error(e);
                throw new MojoExecutionException(e.getMessage(), e);
            } finally {
                if (delete) {
                    File f = new File(template);
                    if (f.exists()) {
                        try {
                            FileUtils.delete(f);
                        } catch (IOException e) {
                            //
                        }
                    }
                }
            }
        } else {
            getLog().info("Skipping genesis template");
        }
    }

    private Template executeTemplate() throws TemplateNotFoundException, InvalidTemplateException, IOException {
        return Template.create(template);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy