use of fmpp.setting.Settings in project drill by apache.
the class FMPPMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (project == null) {
throw new MojoExecutionException("This plugin can only be used inside a project.");
}
String outputPath = output.getAbsolutePath();
if ((!output.exists() && !output.mkdirs()) || !output.isDirectory()) {
throw new MojoFailureException("can not write to output dir: " + outputPath);
}
String templatesPath = templates.getAbsolutePath();
if (!templates.exists() || !templates.isDirectory()) {
throw new MojoFailureException("templates not found in dir: " + outputPath);
}
// add the output directory path to the project source directories
switch(scope) {
case "compile":
project.addCompileSourceRoot(outputPath);
break;
case "test":
project.addTestCompileSourceRoot(outputPath);
break;
default:
throw new MojoFailureException("scope must be compile or test");
}
final Stopwatch sw = Stopwatch.createStarted();
try {
getLog().info(format("Freemarker generation:\n scope: %s,\n config: %s,\n templates: %s", scope, config.getAbsolutePath(), templatesPath));
final File tmp = Files.createTempDirectory("freemarker-tmp").toFile();
String tmpPath = tmp.getAbsolutePath();
final String tmpPathNormalized = tmpPath.endsWith(File.separator) ? tmpPath : tmpPath + File.separator;
Settings settings = new Settings(new File("."));
settings.set(Settings.NAME_SOURCE_ROOT, templatesPath);
settings.set(Settings.NAME_OUTPUT_ROOT, tmp.getAbsolutePath());
settings.load(config);
settings.addProgressListener(new TerseConsoleProgressListener());
settings.addProgressListener(new ProgressListener() {
@Override
public void notifyProgressEvent(Engine engine, int event, File src, int pMode, Throwable error, Object param) throws Exception {
if (event == EVENT_END_PROCESSING_SESSION) {
getLog().info(format("Freemarker generation took %dms", sw.elapsed(TimeUnit.MILLISECONDS)));
sw.reset();
Report report = moveIfChanged(tmp, tmpPathNormalized);
if (!tmp.delete()) {
throw new MojoFailureException(format("can not delete %s", tmp));
}
getLog().info(format("Incremental output update took %dms", sw.elapsed(TimeUnit.MILLISECONDS)));
getLog().info(format("new: %d", report.newFiles));
getLog().info(format("changed: %d", report.changedFiles));
getLog().info(format("unchanged: %d", report.unchangedFiles));
}
}
});
if (addMavenDataLoader) {
getLog().info("Adding maven data loader");
settings.setEngineAttribute(MavenDataLoader.MAVEN_DATA_ATTRIBUTE, new MavenData(project));
settings.add(Settings.NAME_DATA, format("maven: %s()", MavenDataLoader.class.getName()));
}
settings.execute();
} catch (Exception e) {
throw new MojoFailureException(MiscUtil.causeMessages(e), e);
}
}
Aggregations