Search in sources :

Example 11 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project camel by apache.

the class PrepareCatalogKarafMojo method executeComponents.

protected void executeComponents(Set<String> features) throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel component json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> componentFiles = new TreeSet<File>();
    // find all json files in components and camel-core
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] components = componentsDir.listFiles();
        if (components != null) {
            for (File dir : components) {
                // skip camel-spring-dm
                if (dir.isDirectory() && "camel-spring-dm".equals(dir.getName())) {
                    continue;
                }
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    // the directory must be in the list of known features
                    if (!features.contains(dir.getName())) {
                        continue;
                    }
                    // special for camel-salesforce which is in a sub dir
                    if ("camel-salesforce".equals(dir.getName())) {
                        target = new File(dir, "camel-salesforce-component/target/classes");
                    } else if ("camel-linkedin".equals(dir.getName())) {
                        target = new File(dir, "camel-linkedin-component/target/classes");
                    }
                    findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
                }
            }
        }
    }
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes");
        findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
    }
    getLog().info("Found " + componentFiles.size() + " component.properties files");
    getLog().info("Found " + jsonFiles.size() + " component json files");
    // make sure to create out dir
    componentsOutDir.mkdirs();
    Set<String> alternativeSchemes = new HashSet<>();
    for (File file : jsonFiles) {
        File to = new File(componentsOutDir, file.getName());
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
    }
    File all = new File(componentsOutDir, "../components.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = componentsOutDir.list();
        List<String> components = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String componentName = name.substring(0, name.length() - 5);
                components.add(componentName);
            }
        }
        Collections.sort(components);
        for (String name : components) {
            fos.write(name.getBytes());
            fos.write("\n".getBytes());
        }
        fos.close();
    } catch (IOException e) {
        throw new MojoFailureException("Error writing to file " + all);
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project camel by apache.

the class PrepareCatalogKarafMojo method executeOthers.

protected void executeOthers(Set<String> features) throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel other json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> otherFiles = new TreeSet<File>();
    // find all languages from the components directory
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] others = componentsDir.listFiles();
        if (others != null) {
            for (File dir : others) {
                // the directory must be in the list of known features
                if (!features.contains(dir.getName())) {
                    continue;
                }
                // (camel-jetty is a placeholder, as camel-jetty9 is the actual component)
                if ("camel-core-osgi".equals(dir.getName()) || "camel-core-xml".equals(dir.getName()) || "camel-box".equals(dir.getName()) || "camel-http-common".equals(dir.getName()) || "camel-jetty".equals(dir.getName()) || "camel-jetty-common".equals(dir.getName()) || "camel-linkedin".equals(dir.getName()) || "camel-olingo2".equals(dir.getName()) || "camel-salesforce".equals(dir.getName())) {
                    continue;
                }
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    findOtherFilesRecursive(target, jsonFiles, otherFiles, new CamelOthersFileFilter());
                }
            }
        }
    }
    getLog().info("Found " + otherFiles.size() + " other.properties files");
    getLog().info("Found " + jsonFiles.size() + " other json files");
    // make sure to create out dir
    othersOutDir.mkdirs();
    for (File file : jsonFiles) {
        File to = new File(othersOutDir, file.getName());
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
    }
    File all = new File(othersOutDir, "../others.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = othersOutDir.list();
        List<String> others = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String otherName = name.substring(0, name.length() - 5);
                others.add(otherName);
            }
        }
        Collections.sort(others);
        for (String name : others) {
            fos.write(name.getBytes());
            fos.write("\n".getBytes());
        }
        fos.close();
    } catch (IOException e) {
        throw new MojoFailureException("Error writing to file " + all);
    }
}
Also used : TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 13 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project camel by apache.

the class PrepareReadmeMojo method executeComponentsReadme.

protected void executeComponentsReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
    Set<File> componentFiles = new TreeSet<>();
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] files = componentsDir.listFiles();
        if (files != null) {
            componentFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<ComponentModel> models = new ArrayList<>();
        for (File file : componentFiles) {
            String json = loadText(new FileInputStream(file));
            ComponentModel model = generateComponentModel(json, coreOnly);
            // filter out alternative schemas which reuses documentation
            boolean add = true;
            if (!model.getAlternativeSchemes().isEmpty()) {
                String first = model.getAlternativeSchemes().split(",")[0];
                if (!model.getScheme().equals(first)) {
                    add = false;
                }
            }
            if (add) {
                models.add(model);
            }
        }
        // sort the models
        Collections.sort(models, new ComponentComparator());
        // filter out unwanted components
        List<ComponentModel> components = new ArrayList<>();
        for (ComponentModel model : models) {
            if (coreOnly) {
                if ("camel-core".equals(model.getArtifactId())) {
                    // only include core components
                    components.add(model);
                }
            } else {
                // we want to include everything in the big file (also from camel-core)
                components.add(model);
            }
        }
        // how many different artifacts
        int count = components.stream().map(ComponentModel::getArtifactId).collect(toSet()).size();
        // how many deprecated
        long deprecated = components.stream().filter(c -> "true".equals(c.getDeprecated())).count();
        // update the big readme file in the core/components dir
        File file;
        if (coreOnly) {
            file = new File(readmeCoreDir, "readme.adoc");
        } else {
            file = new File(readmeComponentsDir, "readme.adoc");
        }
        // update regular components
        boolean exists = file.exists();
        String changed = templateComponents(components, count, deprecated);
        boolean updated = updateComponents(file, changed);
        if (updated) {
            getLog().info("Updated readme.adoc file: " + file);
        } else if (exists) {
            getLog().debug("No changes to readme.adoc file: " + file);
        } else {
            getLog().warn("No readme.adoc file: " + file);
        }
    } catch (IOException e) {
        throw new MojoFailureException("Error due " + e.getMessage(), e);
    }
}
Also used : Arrays(java.util.Arrays) PackageHelper.writeText(org.apache.camel.maven.packaging.PackageHelper.writeText) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) ArrayList(java.util.ArrayList) OtherModel(org.apache.camel.maven.packaging.model.OtherModel) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateRuntime(org.mvel2.templates.TemplateRuntime) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) Collections(edu.emory.mathcs.backport.java.util.Collections) EipModel(org.apache.camel.maven.packaging.model.EipModel) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) Comparator(java.util.Comparator) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) AbstractMojo(org.apache.maven.plugin.AbstractMojo) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TreeSet(java.util.TreeSet) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) File(java.io.File)

Example 14 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project camel by apache.

the class PrepareReadmeMojo method executeEipsReadme.

protected void executeEipsReadme() throws MojoExecutionException, MojoFailureException {
    Set<File> eipFiles = new TreeSet<>();
    if (eipsDir != null && eipsDir.isDirectory()) {
        File[] files = eipsDir.listFiles();
        if (files != null) {
            eipFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<EipModel> models = new ArrayList<>();
        for (File file : eipFiles) {
            String json = loadText(new FileInputStream(file));
            EipModel model = generateEipModel(json);
            // we only want actual EIPs from the models
            if (model.getLabel().startsWith("eip")) {
                models.add(model);
            }
        }
        // re-order the EIPs so we have them in different categories
        // sort the models
        Collections.sort(models, new EipComparator());
        // how many deprecated
        long deprecated = models.stream().filter(EipModel::isDeprecated).count();
        // update the big readme file in the core dir
        File file = new File(readmeCoreDir, "readme-eip.adoc");
        // update regular components
        boolean exists = file.exists();
        String changed = templateEips(models, deprecated);
        boolean updated = updateEips(file, changed);
        if (updated) {
            getLog().info("Updated readme-eip.adoc file: " + file);
        } else if (exists) {
            getLog().debug("No changes to readme-eip.adoc file: " + file);
        } else {
            getLog().warn("No readme-eip.adoc file: " + file);
        }
    } catch (IOException e) {
        throw new MojoFailureException("Error due " + e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EipModel(org.apache.camel.maven.packaging.model.EipModel) TreeSet(java.util.TreeSet) File(java.io.File)

Example 15 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project camel by apache.

the class PrepareCatalogMojo method executeComponents.

protected Set<String> executeComponents() throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel component json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> duplicateJsonFiles = new TreeSet<File>();
    Set<File> componentFiles = new TreeSet<File>();
    Set<File> missingComponents = new TreeSet<File>();
    Map<String, Set<String>> usedComponentLabels = new TreeMap<String, Set<String>>();
    Set<String> usedOptionLabels = new TreeSet<String>();
    Set<String> unlabeledOptions = new TreeSet<String>();
    Set<File> missingFirstVersions = new TreeSet<File>();
    // find all json files in components and camel-core
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] components = componentsDir.listFiles();
        if (components != null) {
            for (File dir : components) {
                // skip camel-spring-dm
                if (dir.isDirectory() && "camel-spring-dm".equals(dir.getName())) {
                    continue;
                }
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    // special for these as they are in sub dir
                    if ("camel-salesforce".equals(dir.getName())) {
                        target = new File(dir, "camel-salesforce-component/target/classes");
                    } else if ("camel-linkedin".equals(dir.getName())) {
                        target = new File(dir, "camel-linkedin-component/target/classes");
                    } else if ("camel-olingo2".equals(dir.getName())) {
                        target = new File(dir, "camel-olingo2-component/target/classes");
                    } else if ("camel-box".equals(dir.getName())) {
                        target = new File(dir, "camel-box-component/target/classes");
                    }
                    int before = componentFiles.size();
                    int before2 = jsonFiles.size();
                    findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
                    int after = componentFiles.size();
                    int after2 = jsonFiles.size();
                    if (before != after && before2 == after2) {
                        missingComponents.add(dir);
                    }
                }
            }
        }
    }
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes");
        int before = componentFiles.size();
        int before2 = jsonFiles.size();
        findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
        int after = componentFiles.size();
        int after2 = jsonFiles.size();
        if (before != after && before2 == after2) {
            missingComponents.add(coreDir);
        }
    }
    getLog().info("Found " + componentFiles.size() + " component.properties files");
    getLog().info("Found " + jsonFiles.size() + " component json files");
    // make sure to create out dir
    componentsOutDir.mkdirs();
    Set<String> alternativeSchemes = new HashSet<>();
    for (File file : jsonFiles) {
        File to = new File(componentsOutDir, file.getName());
        if (to.exists()) {
            duplicateJsonFiles.add(to);
            getLog().warn("Duplicate component name detected: " + to);
        }
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
        // check if we have a component label as we want the components to include labels
        try {
            String text = loadText(new FileInputStream(file));
            String name = asComponentName(file);
            Matcher matcher = LABEL_PATTERN.matcher(text);
            // grab the label, and remember it in the used labels
            if (matcher.find()) {
                String label = matcher.group(1);
                String[] labels = label.split(",");
                for (String s : labels) {
                    Set<String> components = usedComponentLabels.get(s);
                    if (components == null) {
                        components = new TreeSet<String>();
                        usedComponentLabels.put(s, components);
                    }
                    components.add(name);
                }
            }
            // check all the component options and grab the label(s) they use
            List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("componentProperties", text, true);
            for (Map<String, String> row : rows) {
                String label = row.get("label");
                if (label != null && !label.isEmpty()) {
                    String[] parts = label.split(",");
                    for (String part : parts) {
                        usedOptionLabels.add(part);
                    }
                }
            }
            // check all the endpoint options and grab the label(s) they use
            int unused = 0;
            rows = JSonSchemaHelper.parseJsonSchema("properties", text, true);
            for (Map<String, String> row : rows) {
                String label = row.get("label");
                if (label != null && !label.isEmpty()) {
                    String[] parts = label.split(",");
                    for (String part : parts) {
                        usedOptionLabels.add(part);
                    }
                } else {
                    unused++;
                }
            }
            if (unused >= UNUSED_LABELS_WARN) {
                unlabeledOptions.add(name);
            }
            // remember alternative schemes
            rows = JSonSchemaHelper.parseJsonSchema("component", text, false);
            for (Map<String, String> row : rows) {
                String alternativeScheme = row.get("alternativeSchemes");
                if (alternativeScheme != null && !alternativeScheme.isEmpty()) {
                    String[] parts = alternativeScheme.split(",");
                    for (int i = 1; i < parts.length; i++) {
                        // skip first as that is the regular scheme
                        String part = parts[i];
                        alternativeSchemes.add(part);
                    }
                }
            }
            // detect missing first version
            String firstVersion = null;
            for (Map<String, String> row : rows) {
                if (row.get("firstVersion") != null) {
                    firstVersion = row.get("firstVersion");
                }
            }
            if (firstVersion == null) {
                missingFirstVersions.add(file);
            }
        } catch (IOException e) {
        // ignore
        }
    }
    Set<String> componentNames = new LinkedHashSet<>();
    File all = new File(componentsOutDir, "../components.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = componentsOutDir.list();
        List<String> components = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String componentName = name.substring(0, name.length() - 5);
                components.add(componentName);
            }
        }
        Collections.sort(components);
        for (String name : components) {
            fos.write(name.getBytes());
            fos.write("\n".getBytes());
            // remember component name
            componentNames.add(name);
        }
        fos.close();
    } catch (IOException e) {
        throw new MojoFailureException("Error writing to file " + all);
    }
    printComponentsReport(jsonFiles, duplicateJsonFiles, missingComponents, usedComponentLabels, usedOptionLabels, unlabeledOptions, missingFirstVersions);
    // filter out duplicate component names that are alternative scheme names
    Set<String> answer = new LinkedHashSet<>();
    for (String componentName : componentNames) {
        if (!alternativeSchemes.contains(componentName)) {
            answer.add(componentName);
        }
    }
    return answer;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap)

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