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

li.rudin.mavenjs.plugin.AbstractMavenjsMojo Maven / Gradle / Ivy

package li.rudin.mavenjs.plugin;

import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId;
import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment;
import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId;
import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;
import static org.twdata.maven.mojoexecutor.MojoExecutor.version;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public abstract class AbstractMavenjsMojo extends AbstractMojo
{

	/**
	 * Maven js build dir
	 */
	@Parameter(defaultValue="${project.build.directory}/mavenjs") String mavenjsBuildDir;
	
	
	@Parameter(defaultValue="${project}", readonly=true) MavenProject project;
	@Parameter(defaultValue="${session}", readonly=true) MavenSession session;
	@Component BuildPluginManager pluginManager;

	/**
	 * Json mapper
	 */
	final ObjectMapper mapper = new ObjectMapper();

	protected AbstractMavenjsMojo()
	{

		//disable writing of null values
		mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

	}
	
	/**
	 * Unpacks the dependencies to the mavenjs target folder
	 * @throws MojoExecutionException
	 */
	protected void unpackDependencies() throws MojoExecutionException
	{
		executeMojo(
				plugin(
						groupId("org.apache.maven.plugins"),
						artifactId("maven-dependency-plugin"),
						version("2.8")
						),
						goal("unpack-dependencies"),
						configuration(
								element(name("outputDirectory"), mavenjsBuildDir),
								element(name("excludes"), "**\\/*.class")
								),
								executionEnvironment(
										project,
										session,
										pluginManager
										)
				);
	}
	
	/**
	 * Optimizes with the given config file
	 * @param configFile
	 * @throws MojoExecutionException
	 */
	protected void optimize(String configFile) throws MojoExecutionException
	{
		//https://github.com/mcheely/requirejs-maven-plugin
		executeMojo(
				plugin(
						groupId("com.github.mcheely"),
						artifactId("requirejs-maven-plugin"),
						version("2.0.0")
						),
						goal("optimize"),
						configuration(
								element("configFile", configFile)
								),
								executionEnvironment(
										project,
										session,
										pluginManager
										)
				);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy