Search in sources :

Example 16 with MojoFailureException

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

the class PrepareCatalogMojo method executeModel.

protected void executeModel() throws MojoExecutionException, MojoFailureException {
    getLog().info("================================================================================");
    getLog().info("Copying all Camel model json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> duplicateJsonFiles = new TreeSet<File>();
    Set<File> missingLabels = new TreeSet<File>();
    Set<File> missingJavaDoc = new TreeSet<File>();
    Map<String, Set<String>> usedLabels = new TreeMap<String, Set<String>>();
    // find all json files in camel-core
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes/org/apache/camel/model");
        PackageHelper.findJsonFiles(target, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
    }
    // find all json files in camel-spring
    if (springDir != null && springDir.isDirectory()) {
        File target = new File(springDir, "target/classes/org/apache/camel/spring");
        PackageHelper.findJsonFiles(target, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
        File target2 = new File(springDir, "target/classes/org/apache/camel/core/xml");
        PackageHelper.findJsonFiles(target2, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
    }
    getLog().info("Found " + jsonFiles.size() + " model json files");
    // make sure to create out dir
    modelsOutDir.mkdirs();
    for (File file : jsonFiles) {
        File to = new File(modelsOutDir, file.getName());
        if (to.exists()) {
            duplicateJsonFiles.add(to);
            getLog().warn("Duplicate model name detected: " + to);
        }
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
        try {
            // check if we have a label as we want the eip to include labels
            String text = loadText(new FileInputStream(file));
            // just do a basic label check
            if (text.contains("\"label\": \"\"")) {
                missingLabels.add(file);
            } else {
                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> models = usedLabels.get(s);
                        if (models == null) {
                            models = new TreeSet<String>();
                            usedLabels.put(s, models);
                        }
                        models.add(name);
                    }
                }
            }
            // check all the properties if they have description
            List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", text, true);
            for (Map<String, String> row : rows) {
                String name = row.get("name");
                // skip checking these as they have no documentation
                if ("outputs".equals(name) || "transforms".equals(name)) {
                    continue;
                }
                String doc = row.get("description");
                if (doc == null || doc.isEmpty()) {
                    missingJavaDoc.add(file);
                    break;
                }
            }
        } catch (IOException e) {
        // ignore
        }
    }
    File all = new File(modelsOutDir, "../models.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = modelsOutDir.list();
        List<String> models = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String modelName = name.substring(0, name.length() - 5);
                models.add(modelName);
            }
        }
        Collections.sort(models);
        for (String name : models) {
            fos.write(name.getBytes());
            fos.write("\n".getBytes());
        }
        fos.close();
    } catch (IOException e) {
        throw new MojoFailureException("Error writing to file " + all);
    }
    printModelsReport(jsonFiles, duplicateJsonFiles, missingLabels, usedLabels, missingJavaDoc);
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Matcher(java.util.regex.Matcher) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 17 with MojoFailureException

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

the class PrepareCatalogMojo method executeDataFormats.

protected Set<String> executeDataFormats() throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying all Camel dataformat json descriptors");
    // lets use sorted set/maps
    Set<File> jsonFiles = new TreeSet<File>();
    Set<File> duplicateJsonFiles = new TreeSet<File>();
    Set<File> dataFormatFiles = new TreeSet<File>();
    Map<String, Set<String>> usedLabels = new TreeMap<String, Set<String>>();
    Set<File> missingFirstVersions = new TreeSet<File>();
    // find all data formats from the components directory
    if (componentsDir != null && componentsDir.isDirectory()) {
        File[] dataFormats = componentsDir.listFiles();
        if (dataFormats != null) {
            for (File dir : dataFormats) {
                if (dir.isDirectory() && !"target".equals(dir.getName())) {
                    File target = new File(dir, "target/classes");
                    findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter());
                }
            }
        }
    }
    if (coreDir != null && coreDir.isDirectory()) {
        File target = new File(coreDir, "target/classes");
        findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter());
    }
    getLog().info("Found " + dataFormatFiles.size() + " dataformat.properties files");
    getLog().info("Found " + jsonFiles.size() + " dataformat json files");
    // make sure to create out dir
    dataFormatsOutDir.mkdirs();
    for (File file : jsonFiles) {
        File to = new File(dataFormatsOutDir, file.getName());
        if (to.exists()) {
            duplicateJsonFiles.add(to);
            getLog().warn("Duplicate dataformat 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 label as we want the data format 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> dataFormats = usedLabels.get(s);
                    if (dataFormats == null) {
                        dataFormats = new TreeSet<String>();
                        usedLabels.put(s, dataFormats);
                    }
                    dataFormats.add(name);
                }
            }
            // detect missing first version
            List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("dataformat", text, false);
            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> answer = new LinkedHashSet<>();
    File all = new File(dataFormatsOutDir, "../dataformats.properties");
    try {
        FileOutputStream fos = new FileOutputStream(all, false);
        String[] names = dataFormatsOutDir.list();
        List<String> dataFormats = new ArrayList<String>();
        // sort the names
        for (String name : names) {
            if (name.endsWith(".json")) {
                // strip out .json from the name
                String dataFormatName = name.substring(0, name.length() - 5);
                dataFormats.add(dataFormatName);
            }
        }
        Collections.sort(dataFormats);
        for (String name : dataFormats) {
            fos.write(name.getBytes());
            fos.write("\n".getBytes());
            // remember dataformat name
            answer.add(name);
        }
        fos.close();
    } catch (IOException e) {
        throw new MojoFailureException("Error writing to file " + all);
    }
    printDataFormatsReport(jsonFiles, duplicateJsonFiles, usedLabels, missingFirstVersions);
    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) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 18 with MojoFailureException

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

the class EipDocumentationEnricherMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (pathToModelDir == null) {
        throw new MojoExecutionException("pathToModelDir parameter must not be null");
    }
    validateExists(inputCamelSchemaFile, "inputCamelSchemaFile");
    validateIsFile(inputCamelSchemaFile, "inputCamelSchemaFile");
    validateExists(camelCoreDir, "camelCoreDir");
    validateExists(camelCoreXmlDir, "camelCoreXmlDir");
    validateExists(camelSpringDir, "camelSpringDir");
    validateIsDirectory(camelCoreDir, "camelCoreDir");
    validateIsDirectory(camelCoreXmlDir, "camelCoreXmlDir");
    validateIsDirectory(camelSpringDir, "camelSpringDir");
    try {
        runPlugin();
    } catch (Exception e) {
        throw new MojoExecutionException("Error during plugin execution", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 19 with MojoFailureException

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

the class SpringBootStarterMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!isStarterAllowed()) {
        getLog().info("Spring-Boot-Starter: starter not allowed for module " + project.getArtifactId() + ": skipping.");
        return;
    }
    try {
        // create the starter directory
        File starterDir = starterDir();
        getLog().info("Spring-Boot-Starter: starter dir for the component is: " + starterDir.getAbsolutePath());
        if (!starterDir.exists()) {
            starterDir.mkdirs();
        }
        // create the base pom.xml
        Document pom = createBasePom();
        // Apply changes to the starter pom
        fixExcludedDependencies(pom);
        fixAdditionalDependencies(pom);
        fixAdditionalRepositories(pom);
        // Write the starter pom
        File pomFile = new File(starterDir, "pom.xml");
        writeXmlFormatted(pom, pomFile);
        // write LICENSE, USAGE and spring.provides files
        writeStaticFiles();
        writeSpringProvides();
        // synchronized all starters with their parent pom 'modules' section
        synchronizeParentPom();
    } catch (Exception e) {
        throw new MojoFailureException("Unable to create starter", e);
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) Document(org.w3c.dom.Document) File(java.io.File) DependencyTreeBuilderException(org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 20 with MojoFailureException

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

the class SpringBootAutoConfigurationMojo method createDataFormatAutoConfigurationSource.

private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, boolean hasOptions, String overrideDataFormatName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("DataFormat", "DataFormatAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.RuntimeCamelException");
    javaClass.addImport("org.apache.camel.spi.DataFormat");
    javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
    String body = createDataFormatBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType() + "Factory";
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.DataFormatFactory");
    method.addParameter("CamelContext", "camelContext").setFinal(true);
    if (hasOptions) {
        method.addParameter(configurationName, "configuration").setFinal(true);
    }
    // Determine all the aliases
    // adding the '-dataformat' suffix to prevent collision with component names
    String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

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