com.danielhalima.aem.MyMojo.java Source code

Java tutorial

Introduction

Here is the source code for com.danielhalima.aem.MyMojo.java

Source

package com.danielhalima.aem;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed 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.
 */

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

import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Arrays;

// http://stackoverflow.com/questions/5707677/making-a-maven-archetype-how-to-run-a-mojo-on-a-projects-generation
// http://stackoverflow.com/questions/21414568/maven-not-passing-d-commandline-arguments-to-my-archetype-post-processing-goal

/**
 * Goal which runs archetype post processing.
 *
 * @author Daniel Henrique Alves Lima
 * @since 1.0
 */
@Mojo(name = "run", defaultPhase = LifecyclePhase.PROCESS_SOURCES,
        /* Don't try to run anything related to the current project. */
        requiresProject = false)
public class MyMojo extends AbstractMojo {
    /**
     * Base directory of the post processing project.
     */
    @Parameter(defaultValue = "${basedir}/_post_generate", required = true, property = "archpp.baseDirectory")
    private String baseDirectory;

    /**
     * Should the base directory be deleted?
     */
    @Parameter(defaultValue = "true", property = "archpp.deleteOnExit")
    private boolean deleteOnExit;

    @Component
    private Invoker invoker;

    public void execute() throws MojoExecutionException {

        getLog().info("Running archetype post processing tasks");

        File projectBasedir = new File(this.baseDirectory);

        if (projectBasedir.exists()) {
            InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory(projectBasedir)
                    .setGoals(Arrays.asList(new String[] { "process-sources" }));

            try {
                invoker.execute(request);
                if (this.deleteOnExit) {
                    FileUtils.forceDeleteOnExit(projectBasedir);
                }
            } catch (MavenInvocationException e) {
                throw new MojoExecutionException("Could not run post processing tasks.", e);
            } catch (IOException e) {
                throw new MojoExecutionException("Could not delete directory " + projectBasedir + ".", e);
            }
        } else {
            getLog().info("Post processing tasks skipped: unavailable basedir " + this.baseDirectory);
        }

    }
}