Search in sources :

Example 91 with MojoFailureException

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

the class PrepareUserGuideMojo method executeLanguages.

protected void executeLanguages() 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);
        }
        // sor the models
        Collections.sort(models, new LanguageComparator());
        // the summary file has the TOC
        File file = new File(userGuideDir, "SUMMARY.md");
        // update languages
        StringBuilder languages = new StringBuilder();
        languages.append("* Expression Languages\n");
        for (LanguageModel model : models) {
            String line = "\t* " + link(model) + "\n";
            languages.append(line);
        }
        boolean updated = updateLanguages(file, languages.toString());
        if (updated) {
            getLog().info("Updated user guide file: " + file);
        } else {
            getLog().debug("No changes to user guide 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) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) File(java.io.File)

Example 92 with MojoFailureException

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

the class SpringBootAutoConfigurationMojo method writeAdditionalSpringMetaData.

private void writeAdditionalSpringMetaData(String prefix, String type, String name) throws MojoFailureException {
    String fullQualifiedName = prefix + "." + type + "." + name + "." + "enabled";
    String fileName = "META-INF/additional-spring-configuration-metadata.json";
    File target = new File(SpringBootHelper.starterResourceDir(baseDir, project.getArtifactId()), fileName);
    deleteFileOnMainArtifact(target);
    try {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Map<String, Object> map = null;
        List<Map<String, Object>> properties = null;
        if (target.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(target));
            map = gson.fromJson(br, Map.class);
            properties = (List<Map<String, Object>>) map.get("properties");
            if (properties != null && properties.stream().anyMatch(m -> fullQualifiedName.equals(m.get("name")))) {
                getLog().debug("No changes to existing file: " + target);
                return;
            }
        }
        Map<String, Object> meta = new HashMap();
        meta.put("name", fullQualifiedName);
        meta.put("type", "java.lang.Boolean");
        meta.put("defaultValue", true);
        meta.put("description", "Enable " + name + " " + type);
        if (properties == null) {
            properties = new ArrayList<>(1);
        }
        if (map == null) {
            map = new HashMap();
        }
        properties.add(meta);
        map.put("properties", properties);
        FileUtils.write(target, gson.toJson(map));
    } catch (Exception e) {
        throw new MojoFailureException("IOError with file " + target, e);
    }
}
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) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Gson(com.google.gson.Gson) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 93 with MojoFailureException

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

the class SpringBootAutoConfigurationMojo method createComponentAutoConfigurationSource.

private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, boolean hasOptions, String overrideComponentName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentAutoConfiguration");
    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("ComponentAutoConfiguration", "ComponentConfiguration");
    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(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    // add method for auto configure
    String body = createComponentBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType();
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    if (hasOptions) {
        method.addParameter(configurationName, "configuration");
    }
    // Determine all the aliases
    String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);
    method.addAnnotation(Lazy.class);
    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.component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).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)

Example 94 with MojoFailureException

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

the class SpringBootAutoConfigurationMojo method getProjectClassLoader.

protected ClassLoader getProjectClassLoader() throws MojoFailureException {
    final List classpathElements;
    try {
        classpathElements = project.getTestClasspathElements();
    } catch (org.apache.maven.artifact.DependencyResolutionRequiredException e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
    final URL[] urls = new URL[classpathElements.size()];
    int i = 0;
    for (Iterator it = classpathElements.iterator(); it.hasNext(); i++) {
        try {
            urls[i] = new File((String) it.next()).toURI().toURL();
        } catch (MalformedURLException e) {
            throw new MojoFailureException(e.getMessage(), e);
        }
    }
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    return new URLClassLoader(urls, tccl != null ? tccl : getClass().getClassLoader());
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) Iterator(java.util.Iterator) URLClassLoader(java.net.URLClassLoader) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) File(java.io.File)

Example 95 with MojoFailureException

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

the class PrepareCatalogMojo method executeArchetypes.

protected void executeArchetypes() throws MojoExecutionException, MojoFailureException {
    getLog().info("Copying Archetype Catalog");
    // find the generate catalog
    File file = new File(archetypesDir, "target/classes/archetype-catalog.xml");
    // make sure to create out dir
    archetypesOutDir.mkdirs();
    if (file.exists() && file.isFile()) {
        File to = new File(archetypesOutDir, file.getName());
        try {
            copyFile(file, to);
        } catch (IOException e) {
            throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
        }
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) 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