use of org.apache.maven.plugin.MojoFailureException in project asterixdb by apache.
the class RecordManagerGeneratorMojo method generateSource.
private void generateSource(Generator.TemplateType mgrType, String template, String recordType, String outputPath) throws MojoFailureException {
InputStream is = getClass().getClassLoader().getResourceAsStream(template);
if (is == null) {
throw new MojoFailureException("template '" + template + "' not found in classpath");
}
StringBuilder sb = new StringBuilder();
File outputFile = new File(outputPath + File.separator + recordType + template);
try {
getLog().info("generating " + outputFile.toString());
Generator.generateSource(mgrType, packageName, typeMap.get(recordType), is, sb, debug);
is.close();
FileWriter outWriter = new FileWriter(outputFile);
outWriter.write(sb.toString());
outWriter.close();
} catch (Exception ex) {
getLog().error(ex);
throw new MojoFailureException("failed to generate " + outputFile.toString());
}
}
use of org.apache.maven.plugin.MojoFailureException in project drill by apache.
the class FMPPMojo method moveIfChanged.
private Report moveIfChanged(File root, String tmpPath) throws MojoFailureException, IOException {
Report report = new Report();
for (File file : root.listFiles()) {
if (file.isDirectory()) {
report.add(moveIfChanged(file, tmpPath));
if (!file.delete()) {
throw new MojoFailureException(format("can not delete %s", file));
}
} else {
String absPath = file.getAbsolutePath();
if (!absPath.startsWith(tmpPath)) {
throw new MojoFailureException(format("%s should start with %s", absPath, tmpPath));
}
String relPath = absPath.substring(tmpPath.length());
File outputFile = new File(output, relPath);
if (!outputFile.exists()) {
report.addNew();
} else if (!FileUtils.contentEquals(file, outputFile)) {
getLog().info(format("%s has changed", relPath));
if (!outputFile.delete()) {
throw new MojoFailureException(format("can not delete %s", outputFile));
}
report.addChanged();
} else {
report.addUnchanged();
}
if (!outputFile.exists()) {
File parentDir = outputFile.getParentFile();
if (parentDir.exists() && !parentDir.isDirectory()) {
throw new MojoFailureException(format("can not move %s to %s as %s is not a dir", file, outputFile, parentDir));
}
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new MojoFailureException(format("can not move %s to %s as dir %s can not be created", file, outputFile, parentDir));
}
FileUtils.moveFile(file, outputFile);
} else {
if (!file.delete()) {
throw new MojoFailureException(format("can not delete %s", file));
}
}
}
}
return report;
}
use of org.apache.maven.plugin.MojoFailureException 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);
}
}
use of org.apache.maven.plugin.MojoFailureException in project karaf by apache.
the class GenerateDescriptorMojo method saveDependencyChanges.
protected void saveDependencyChanges(Collection<Bundle> addedBundles, Collection<Bundle> removedBundles, Collection<Dependency> addedDependencys, Collection<Dependency> removedDependencys, ObjectFactory objectFactory) throws Exception {
File addedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.added.xml");
Features added = toFeatures(addedBundles, addedDependencys, objectFactory);
writeDependencies(added, addedFile);
File removedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.removed.xml");
Features removed = toFeatures(removedBundles, removedDependencys, objectFactory);
writeDependencies(removed, removedFile);
StringWriter out = new StringWriter();
out.write(saveTreeListing());
out.write("Dependencies have changed:\n");
if (!addedBundles.isEmpty() || !addedDependencys.isEmpty()) {
out.write("\tAdded dependencies are saved here: " + addedFile.getAbsolutePath() + "\n");
if (logDependencyChanges) {
JaxbUtil.marshal(added, out);
}
}
if (!removedBundles.isEmpty() || !removedDependencys.isEmpty()) {
out.write("\tRemoved dependencies are saved here: " + removedFile.getAbsolutePath() + "\n");
if (logDependencyChanges) {
JaxbUtil.marshal(removed, out);
}
}
out.write("Delete " + dependencyCache.getAbsolutePath() + " if you are happy with the dependency changes.");
if (failOnDependencyChange) {
throw new MojoFailureException(out.toString());
} else {
getLog().warn(out.toString());
}
}
use of org.apache.maven.plugin.MojoFailureException in project karaf by apache.
the class GenerateServiceMetadata method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
try {
boolean addSourceDirectory = false;
StringBuilder requirements = new StringBuilder();
StringBuilder capabilities = new StringBuilder();
ClassFinder finder = createFinder(classLoader);
List<Class<?>> classes = finder.findAnnotatedClasses(Services.class);
List<Class<?>> activators = new ArrayList<>();
for (Class<?> clazz : classes) {
URL classUrl = clazz.getClassLoader().getResource(clazz.getName().replace('.', '/') + ".class");
URL outputDirectoryUrl = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
if (classUrl == null || !classUrl.getPath().startsWith(outputDirectoryUrl.getPath())) {
System.out.println("Ignoring " + classUrl);
continue;
}
if (BundleActivator.class.isAssignableFrom(clazz)) {
activators.add(clazz);
}
Properties props = new Properties();
Services services = clazz.getAnnotation(Services.class);
if (services != null) {
for (RequireService req : services.requires()) {
String fltWithClass = combine(req.filter(), "(objectClass=" + req.value().getName() + ")");
addServiceReq(requirements, fltWithClass);
props.setProperty(req.value().getName(), req.filter());
}
for (ProvideService cap : services.provides()) {
addServiceCap(capabilities, cap);
}
}
Managed managed = clazz.getAnnotation(Managed.class);
if (managed != null) {
props.setProperty("pid", managed.value());
}
File file = new File(outputDirectory, "OSGI-INF/karaf-tracker/" + clazz.getName());
file.getParentFile().mkdirs();
try (OutputStream os = buildContext.newFileOutputStream(file)) {
props.store(os, null);
}
addSourceDirectory = true;
}
if (addSourceDirectory) {
Resource resource = new Resource();
resource.setDirectory(outputDirectory);
project.addResource(resource);
}
project.getProperties().setProperty(requirementsProperty, requirements.toString());
project.getProperties().setProperty(capabilitiesProperty, capabilities.toString());
if (activators.size() == 1) {
project.getProperties().setProperty(activatorProperty, activators.get(0).getName());
}
project.getProperties().setProperty("BNDExtension-Private-Package", "org.apache.karaf.util.tracker");
project.getProperties().setProperty("BNDPrependExtension-Import-Package", "!org.apache.karaf.util.tracker.annotation");
List<Class<?>> services = finder.findAnnotatedClasses(Service.class);
Set<String> packages = new TreeSet<>();
for (Class<?> clazz : services) {
packages.add(clazz.getPackage().getName());
}
if (!packages.isEmpty()) {
project.getProperties().setProperty("BNDExtension-Karaf-Commands", join(packages, ","));
}
} catch (Exception e) {
throw new MojoExecutionException("Error building commands help", e);
}
}
Aggregations