package ch.sbb.maven.plugins.iib.mojos;
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 java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
/**
* Packages a built Maven Project with <packaging> iib-bar into the Maven .zip file for (Maven) installation / deployment
*
* Implemented with help from: https://github.com/TimMoore/mojo-executor/blob/master/README.md
*/
@Mojo(name = "package-iib-bar")
public class PackageIibBarMojo extends AbstractMojo {
/**
* The path to write the assemblies/iib-bar-project.xml file to before invoking the maven-assembly-plugin.
*/
@Parameter(defaultValue = "${project.build.directory}/assemblies/iib-bar-project.xml", readonly = true)
private File buildAssemblyFile;
/**
* The path to write the assemblies/iib-bar-project.xml file to before invoking the maven-assembly-plugin.
*/
@Parameter(defaultValue = "${project.build.directory}/assemblies/iib-bar-project-continuous-delivery.xml", readonly = true)
private File buildAssemblyFileContinuousDelivery;
/**
* The Maven Project Object
*/
@Parameter(property = "project", required = true, readonly = true)
protected MavenProject project;
/**
*
*/
@Parameter(property = "iib.continuousDelivery", required = true, readonly = true, defaultValue = "false")
protected boolean continuousDelivery;
/**
* The Maven Session Object
*/
@Parameter(property = "session", required = true, readonly = true)
protected MavenSession session;
/**
* The Maven PluginManager Object
*/
@Component
protected BuildPluginManager buildPluginManager;
public void execute() throws MojoFailureException, MojoExecutionException {
packageIibBarArtifact();
}
private void packageIibBarArtifact() throws MojoFailureException, MojoExecutionException {
String assemblyPath = "/assemblies/iib-bar-project.xml";
//
if (continuousDelivery) {
assemblyPath = "/assemblies/iib-bar-project-continuous-delivery.xml";
}
InputStream is = this.getClass().getResourceAsStream(assemblyPath);
FileOutputStream fos;
File buildAssembly = null;
if (continuousDelivery) {
buildAssembly = buildAssemblyFileContinuousDelivery;
} else {
buildAssembly = buildAssemblyFile;
}
buildAssembly.getParentFile().mkdirs();
try {
fos = new FileOutputStream(buildAssembly);
} catch (FileNotFoundException e) {
// should never happen, as the file is packaged in this plugin's jar
throw new MojoFailureException("Error creating the build assembly file: " + buildAssembly);
}
try {
IOUtil.copy(is, fos);
} catch (IOException e) {
// should never happen
throw new MojoFailureException("Error creating the assembly file: " + buildAssembly.getAbsolutePath());
}
// mvn org.apache.maven.plugins:maven-assembly-plugin:2.4:single -Ddescriptor=target\assemblies\iib-bar-project.xml -Dassembly.appendAssemblyId=false
executeMojo(plugin(groupId("org.apache.maven.plugins"), artifactId("maven-assembly-plugin"), version("2.4")), goal("single"), configuration(element(name("descriptor"),
"${project.build.directory}" + assemblyPath), element(name("appendAssemblyId"), "false")), executionEnvironment(project, session, buildPluginManager));
// delete the archive-tmp directory
try {
FileUtils.deleteDirectory(new File(project.getBuild().getDirectory(), "archive-tmp"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}