Search in sources :

Example 6 with MojoFailureException

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

the class PrepareCatalogSpringBootMojo method executeLanguages.

protected void executeLanguages(Set<String> starters) throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel language json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> languageFiles = new TreeSet<File>();
    // find all languages from the components directory
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] languages = componentsDir.listFiles();
        if (languages != null) {
            for (File dir : languages) {
                // skip camel-spring-dm
                if (dir.isDirectory() && "camel-spring-dm".equals(dir.getName())) {
                    continue;
                }
                // the directory must be in the list of known starters
                if (!starters.contains(dir.getName())) {
                    continue;
                }
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
                }
            }
        }
    }
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes");
        findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
    }
    getLog().info("Found " + languageFiles.size() + " language.properties files");
    getLog().info("Found " + jsonFiles.size() + " language json files");
    // make sure to create out dir
    languagesOutDir.mkdirs();
    for (File file : jsonFiles) {
        // for spring-boot we need to amend the json file to use -starter as the artifact-id
        try {
            String text = loadText(new FileInputStream(file));
            text = ARTIFACT_PATTERN.matcher(text).replaceFirst("\"artifactId\": \"camel-$1-starter\"");
            // write new json file
            File to = new File(languagesOutDir, file.getName());
            FileOutputStream fos = new FileOutputStream(to, false);
            fos.write(text.getBytes());
            fos.close();
        } catch (IOException e) {
            throw new MojoFailureException("Cannot write json file " + file, e);
        }
    }
    File all = new File(languagesOutDir, "../languages.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = languagesOutDir.list();
        List<String> languages = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String languageName = name.substring(0, name.length() - 5);
                languages.add(languageName);
            }
        }
        Collections.sort(languages);
        for (String name : languages) {
            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) FileInputStream(java.io.FileInputStream) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 7 with MojoFailureException

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

the class PrepareReadmeMojo method executeDataFormatsReadme.

protected void executeDataFormatsReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
    Set<File> dataFormatFiles = new TreeSet<>();
    if (dataFormatsDir != null && dataFormatsDir.isDirectory()) {
        File[] files = dataFormatsDir.listFiles();
        if (files != null) {
            dataFormatFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<DataFormatModel> models = new ArrayList<>();
        for (File file : dataFormatFiles) {
            String json = loadText(new FileInputStream(file));
            DataFormatModel model = generateDataFormatModel(json);
            // special for bindy as we have one common file
            if (model.getName().startsWith("bindy")) {
                model.setName("bindy");
            }
            models.add(model);
        }
        // sort the models
        Collections.sort(models, new DataFormatComparator());
        // how many different artifacts
        int count = models.stream().map(DataFormatModel::getArtifactId).collect(toSet()).size();
        // how many deprecated
        long deprecated = models.stream().filter(m -> "true".equals(m.getDeprecated())).count();
        // filter out camel-core
        List<DataFormatModel> dataFormats = new ArrayList<>();
        for (DataFormatModel model : models) {
            if (coreOnly) {
                if ("camel-core".equals(model.getArtifactId())) {
                    // only include core components
                    dataFormats.add(model);
                }
            } else {
                // we want to include everything in the big file (also from camel-core)
                dataFormats.add(model);
            }
        }
        // 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 data formats
        boolean exists = file.exists();
        String changed = templateDataFormats(dataFormats, count, deprecated);
        boolean updated = updateDataFormats(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) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) TreeSet(java.util.TreeSet) File(java.io.File)

Example 8 with MojoFailureException

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

the class PrepareReadmeMojo method executeLanguagesReadme.

protected void executeLanguagesReadme(boolean coreOnly) throws MojoExecutionException, MojoFailureException {
    Set<File> languageFiles = new TreeSet<>();
    if (languagesDir != null && languagesDir.isDirectory()) {
        File[] files = languagesDir.listFiles();
        if (files != null) {
            languageFiles.addAll(Arrays.asList(files));
        }
    }
    try {
        List<LanguageModel> models = new ArrayList<>();
        for (File file : languageFiles) {
            String json = loadText(new FileInputStream(file));
            LanguageModel model = generateLanguageModel(json);
            models.add(model);
        }
        // sort the models
        Collections.sort(models, new LanguageComparator());
        // filter out camel-core
        List<LanguageModel> languages = new ArrayList<>();
        for (LanguageModel model : models) {
            if (coreOnly) {
                if ("camel-core".equals(model.getArtifactId())) {
                    // only include core components
                    languages.add(model);
                }
            } else {
                // we want to include everything in the big file (also from camel-core)
                languages.add(model);
            }
        }
        // how many different artifacts
        int count = languages.stream().map(LanguageModel::getArtifactId).collect(toSet()).size();
        // how many deprecated
        long deprecated = languages.stream().filter(l -> "true".equals(l.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 data formats
        boolean exists = file.exists();
        String changed = templateLanguages(languages, count, deprecated);
        boolean updated = updateLanguages(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) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) File(java.io.File)

Example 9 with MojoFailureException

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

the class PrepareCatalogKarafMojo method executeLanguages.

protected void executeLanguages(Set<String> features) throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel language json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> languageFiles = new TreeSet<File>();
    // find all languages from the components directory
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] languages = componentsDir.listFiles();
        if (languages != null) {
            for (File dir : languages) {
                // skip camel-spring-dm
                if (dir.isDirectory() && "camel-spring-dm".equals(dir.getName())) {
                    continue;
                }
                // the directory must be in the list of known features
                if (!features.contains(dir.getName())) {
                    continue;
                }
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
                }
            }
        }
    }
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes");
        findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
    }
    getLog().info("Found " + languageFiles.size() + " language.properties files");
    getLog().info("Found " + jsonFiles.size() + " language json files");
    // make sure to create out dir
    languagesOutDir.mkdirs();
    for (File file : jsonFiles) {
        File to = new File(languagesOutDir, file.getName());
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
    }
    File all = new File(languagesOutDir, "../languages.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = languagesOutDir.list();
        List<String> languages = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String languageName = name.substring(0, name.length() - 5);
                languages.add(languageName);
            }
        }
        Collections.sort(languages);
        for (String name : languages) {
            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 10 with MojoFailureException

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

the class PrepareCatalogKarafMojo method findKarafFeatures.

private Set<String> findKarafFeatures() throws MojoExecutionException, MojoFailureException {
    // load features.xml file and parse it
    Set<String> answer = new LinkedHashSet<>();
    try {
        InputStream is = new FileInputStream(new File(featuresDir, "features.xml"));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setNamespaceAware(false);
        dbf.setValidating(false);
        dbf.setXIncludeAware(false);
        Document dom = dbf.newDocumentBuilder().parse(is);
        NodeList children = dom.getElementsByTagName("features");
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == ELEMENT_NODE) {
                NodeList children2 = child.getChildNodes();
                for (int j = 0; j < children2.getLength(); j++) {
                    Node child2 = children2.item(j);
                    if ("feature".equals(child2.getNodeName())) {
                        String artifactId = child2.getAttributes().getNamedItem("name").getTextContent();
                        if (artifactId != null && artifactId.startsWith("camel-")) {
                            answer.add(artifactId);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error reading features.xml file", e);
    }
    return answer;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) File(java.io.File)

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