Search in sources :

Example 31 with MojoFailureException

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());
    }
}
Also used : InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) MojoFailureException(org.apache.maven.plugin.MojoFailureException) File(java.io.File) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 32 with MojoFailureException

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;
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) File(java.io.File)

Example 33 with MojoFailureException

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TerseConsoleProgressListener(fmpp.progresslisteners.TerseConsoleProgressListener) ProgressListener(fmpp.ProgressListener) TerseConsoleProgressListener(fmpp.progresslisteners.TerseConsoleProgressListener) MavenData(org.apache.drill.fmpp.mojo.MavenDataLoader.MavenData) File(java.io.File) Settings(fmpp.setting.Settings) Engine(fmpp.Engine)

Example 34 with MojoFailureException

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());
    }
}
Also used : StringWriter(java.io.StringWriter) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Features(org.apache.karaf.features.internal.model.Features) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File)

Example 35 with MojoFailureException

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RequireService(org.apache.karaf.util.tracker.annotation.RequireService) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) URL(java.net.URL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Services(org.apache.karaf.util.tracker.annotation.Services) ProvideService(org.apache.karaf.util.tracker.annotation.ProvideService) TreeSet(java.util.TreeSet) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) Managed(org.apache.karaf.util.tracker.annotation.Managed)

Aggregations

MojoFailureException (org.apache.maven.plugin.MojoFailureException)157 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)94 File (java.io.File)93 IOException (java.io.IOException)91 ArrayList (java.util.ArrayList)50 TreeSet (java.util.TreeSet)33 FileInputStream (java.io.FileInputStream)32 FileOutputStream (java.io.FileOutputStream)21 List (java.util.List)21 Map (java.util.Map)21 MavenProject (org.apache.maven.project.MavenProject)18 Set (java.util.Set)15 MalformedURLException (java.net.MalformedURLException)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 InputStream (java.io.InputStream)12 URLClassLoader (java.net.URLClassLoader)12 Matcher (java.util.regex.Matcher)12 Artifact (org.apache.maven.artifact.Artifact)12 AbstractMojo (org.apache.maven.plugin.AbstractMojo)12