it.serendigity.maven.plugin.lifecycle.helper.ExecutionPlanFileMojo Maven / Gradle / Ivy
package it.serendigity.maven.plugin.lifecycle.helper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Path;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import it.serendigity.maven.plugin.lifecycle.helper.output.CSVTable;
import it.serendigity.maven.plugin.lifecycle.helper.utils.TextUtils;
import it.serendigity.maven.plugin.lifecycle.helper.vo.MavenExecutionPlanInfo;
/**
* List execution plan for the current project to an output file.
*/
@Mojo(name = "execution-plan-file", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true, requiresProject = true)
public class ExecutionPlanFileMojo extends AbstractLifecycleMojo {
private static final String OUTPUT_FILE_NAME = "lifecycle-helper";
private static final String CSV = "CSV";
public enum FILE_FORMAT {
CSV
}
/**
* Output directory to write the execution plan. The default is the project's
* build directory. For goals that don't require a project the current directory
* will be used.
*/
@Parameter(property = "lifecycle-helper.outputDirectory", defaultValue = "${project.build.directory}")
private File paramOutputDirectory;
/**
* The name of the execution plan file. A file extension matching the configured
* {@code fileFormat} will be added if not specified.
*/
@Parameter(property = "lifecycle-helper.outputFileName", defaultValue = OUTPUT_FILE_NAME)
private String paramOutputFileName;
@Parameter(property = "lifecycle-helper.fileFormat", defaultValue = CSV)
private FILE_FORMAT paramFileFormat;
public void execute() throws MojoExecutionException, MojoFailureException {
String fileName = TextUtils.normalizeFileNameWithExtension(paramOutputFileName, paramFileFormat.name());
validateOutputDirectory(paramOutputDirectory);
MavenExecutionPlanInfo executionPlanInfo = calculateExecutionPlan(false);
CSVTable csv = new CSVTable(executionPlanInfo);
String table = csv.createTable();
try {
Path fullPathOutputFile = paramOutputDirectory.toPath().resolve(fileName);
File fout = fullPathOutputFile.toFile();
if (fout.exists()) {
getLog().error("Output file name already exists! " + fileName + "(" + paramOutputDirectory + ")");
} else {
FileOutputStream fos = new FileOutputStream(fout);
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))) {
bw.write(table);
}
}
handleOutput("\nFile " + fileName + " created in " + paramOutputDirectory + ".");
} catch (IOException e) {
getLog().error(e);
}
}
protected void validateOutputDirectory(File dir) {
if (!dir.exists()) {
dir.mkdirs();
}
}
@Override
protected String headerParametersString() {
String header = super.headerParametersString();
header = header + "\nFormat: " + paramFileFormat;
header = header + "\nDir: " + paramOutputDirectory;
header = header + "\nFile: " + paramOutputFileName;
return header;
}
}