Search in sources :

Example 1 with Resource

use of org.apache.maven.model.Resource in project che by eclipse.

the class MavenModelUtil method convertToMavenResource.

private static Resource convertToMavenResource(MavenResource mavenResource) {
    Resource resource = new Resource();
    resource.setDirectory(mavenResource.getDirectory());
    resource.setFiltering(mavenResource.isFiltered());
    resource.setTargetPath(mavenResource.getTargetPath());
    resource.setIncludes(mavenResource.getIncludes());
    resource.setExcludes(mavenResource.getExcludes());
    return resource;
}
Also used : MavenResource(org.eclipse.che.maven.data.MavenResource) Resource(org.apache.maven.model.Resource)

Example 2 with Resource

use of org.apache.maven.model.Resource in project spring-boot by spring-projects.

the class AbstractRunMojo method addResources.

private void addResources(List<URL> urls) throws IOException {
    if (this.addResources) {
        for (Resource resource : this.project.getResources()) {
            File directory = new File(resource.getDirectory());
            urls.add(directory.toURI().toURL());
            FileUtils.removeDuplicatesFromOutputDirectory(this.classesDirectory, directory);
        }
    }
}
Also used : Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 3 with Resource

use of org.apache.maven.model.Resource in project camel by apache.

the class PackageDataFormatMojo method prepareDataFormat.

public static void prepareDataFormat(Log log, MavenProject project, MavenProjectHelper projectHelper, File dataFormatOutDir, File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
    File camelMetaDir = new File(dataFormatOutDir, "META-INF/services/org/apache/camel/");
    // can stop the build before the end and eclipse always needs to know about that directory 
    if (projectHelper != null) {
        projectHelper.addResource(project, dataFormatOutDir.getPath(), Collections.singletonList("**/dataformat.properties"), Collections.emptyList());
    }
    if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/dataformat")) {
        return;
    }
    Map<String, String> javaTypes = new HashMap<String, String>();
    StringBuilder buffer = new StringBuilder();
    int count = 0;
    for (Resource r : project.getBuild().getResources()) {
        File f = new File(r.getDirectory());
        if (!f.exists()) {
            f = new File(project.getBasedir(), r.getDirectory());
        }
        f = new File(f, "META-INF/services/org/apache/camel/dataformat");
        if (f.exists() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null) {
                for (File file : files) {
                    String javaType = readClassFromCamelResource(file, buffer, buildContext);
                    if (!file.isDirectory() && file.getName().charAt(0) != '.') {
                        count++;
                    }
                    if (javaType != null) {
                        javaTypes.put(file.getName(), javaType);
                    }
                }
            }
        }
    }
    // and create json schema model file for this data format
    try {
        if (count > 0) {
            Artifact camelCore = findCamelCoreArtifact(project);
            if (camelCore != null) {
                File core = camelCore.getFile();
                if (core != null) {
                    URL url = new URL("file", null, core.getAbsolutePath());
                    URLClassLoader loader = new URLClassLoader(new URL[] { url });
                    for (Map.Entry<String, String> entry : javaTypes.entrySet()) {
                        String name = entry.getKey();
                        String javaType = entry.getValue();
                        String modelName = asModelName(name);
                        InputStream is = loader.getResourceAsStream("org/apache/camel/model/dataformat/" + modelName + ".json");
                        if (is == null) {
                            // use file input stream if we build camel-core itself, and thus do not have a JAR which can be loaded by URLClassLoader
                            is = new FileInputStream(new File(core, "org/apache/camel/model/dataformat/" + modelName + ".json"));
                        }
                        String json = loadText(is);
                        DataFormatModel dataFormatModel = new DataFormatModel();
                        dataFormatModel.setName(name);
                        dataFormatModel.setTitle("");
                        dataFormatModel.setModelName(modelName);
                        dataFormatModel.setLabel("");
                        dataFormatModel.setDescription(project.getDescription());
                        dataFormatModel.setJavaType(javaType);
                        dataFormatModel.setGroupId(project.getGroupId());
                        dataFormatModel.setArtifactId(project.getArtifactId());
                        dataFormatModel.setVersion(project.getVersion());
                        List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
                        for (Map<String, String> row : rows) {
                            if (row.containsKey("title")) {
                                String title = row.get("title");
                                dataFormatModel.setTitle(asModelTitle(name, title));
                            }
                            if (row.containsKey("label")) {
                                dataFormatModel.setLabel(row.get("label"));
                            }
                            if (row.containsKey("deprecated")) {
                                dataFormatModel.setDeprecated(row.get("deprecated"));
                            }
                            if (row.containsKey("javaType")) {
                                dataFormatModel.setModelJavaType(row.get("javaType"));
                            }
                            if (row.containsKey("firstVersion")) {
                                dataFormatModel.setFirstVersion(row.get("firstVersion"));
                            }
                            // override description for camel-core, as otherwise its too generic
                            if ("camel-core".equals(project.getArtifactId())) {
                                if (row.containsKey("description")) {
                                    dataFormatModel.setDescription(row.get("description"));
                                }
                            }
                        }
                        // first version special for json
                        String firstVersion = prepareJsonFirstVersion(name);
                        if (firstVersion != null) {
                            dataFormatModel.setFirstVersion(firstVersion);
                        }
                        log.debug("Model " + dataFormatModel);
                        // build json schema for the data format
                        String properties = after(json, "  \"properties\": {");
                        // special prepare for bindy/json properties
                        properties = prepareBindyProperties(name, properties);
                        properties = prepareJsonProperties(name, properties);
                        String schema = createParameterJsonSchema(dataFormatModel, properties);
                        log.debug("JSon schema\n" + schema);
                        // write this to the directory
                        File dir = new File(schemaOutDir, schemaSubDirectory(dataFormatModel.getJavaType()));
                        dir.mkdirs();
                        File out = new File(dir, name + ".json");
                        OutputStream fos = buildContext.newFileOutputStream(out);
                        fos.write(schema.getBytes());
                        fos.close();
                        log.debug("Generated " + out + " containing JSon schema for " + name + " data format");
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading dataformat model from camel-core. Reason: " + e, e);
    }
    if (count > 0) {
        Properties properties = new Properties();
        String names = buffer.toString();
        properties.put("dataFormats", names);
        properties.put("groupId", project.getGroupId());
        properties.put("artifactId", project.getArtifactId());
        properties.put("version", project.getVersion());
        properties.put("projectName", project.getName());
        if (project.getDescription() != null) {
            properties.put("projectDescription", project.getDescription());
        }
        camelMetaDir.mkdirs();
        File outFile = new File(camelMetaDir, "dataformat.properties");
        // which can cause a re-compile of all the source code
        if (outFile.exists()) {
            try {
                Properties existing = new Properties();
                InputStream is = new FileInputStream(outFile);
                existing.load(is);
                is.close();
                // are the content the same?
                if (existing.equals(properties)) {
                    log.debug("No dataformat changes detected");
                    return;
                }
            } catch (IOException e) {
            // ignore
            }
        }
        try {
            OutputStream os = buildContext.newFileOutputStream(outFile);
            properties.store(os, "Generated by camel-package-maven-plugin");
            os.close();
            log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "dataformats: " : "dataformat: ") + names);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
        }
    } else {
        log.debug("No META-INF/services/org/apache/camel/dataformat directory found. Are you sure you have created a Camel data format?");
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) Properties(java.util.Properties) Artifact(org.apache.maven.artifact.Artifact) URL(java.net.URL) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashMap(java.util.HashMap) PackageHelper.parseAsMap(org.apache.camel.maven.packaging.PackageHelper.parseAsMap) Map(java.util.Map)

Example 4 with Resource

use of org.apache.maven.model.Resource in project camel by apache.

the class PackageLanguageMojo method prepareLanguage.

public static void prepareLanguage(Log log, MavenProject project, MavenProjectHelper projectHelper, File languageOutDir, File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
    File camelMetaDir = new File(languageOutDir, "META-INF/services/org/apache/camel/");
    // can stop the build before the end and eclipse always needs to know about that directory 
    if (projectHelper != null) {
        projectHelper.addResource(project, languageOutDir.getPath(), Collections.singletonList("**/language.properties"), Collections.emptyList());
    }
    if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/language")) {
        return;
    }
    Map<String, String> javaTypes = new HashMap<String, String>();
    StringBuilder buffer = new StringBuilder();
    int count = 0;
    for (Resource r : project.getBuild().getResources()) {
        File f = new File(r.getDirectory());
        if (!f.exists()) {
            f = new File(project.getBasedir(), r.getDirectory());
        }
        f = new File(f, "META-INF/services/org/apache/camel/language");
        if (f.exists() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null) {
                for (File file : files) {
                    String javaType = readClassFromCamelResource(file, buffer, buildContext);
                    if (!file.isDirectory() && file.getName().charAt(0) != '.') {
                        count++;
                    }
                    if (javaType != null) {
                        javaTypes.put(file.getName(), javaType);
                    }
                }
            }
        }
    }
    // and create json schema model file for this data format
    try {
        if (count > 0) {
            Artifact camelCore = findCamelCoreArtifact(project);
            if (camelCore != null) {
                File core = camelCore.getFile();
                if (core != null) {
                    URL url = new URL("file", null, core.getAbsolutePath());
                    URLClassLoader loader = new URLClassLoader(new URL[] { url });
                    for (Map.Entry<String, String> entry : javaTypes.entrySet()) {
                        String name = entry.getKey();
                        String javaType = entry.getValue();
                        String modelName = asModelName(name);
                        InputStream is = loader.getResourceAsStream("org/apache/camel/model/language/" + modelName + ".json");
                        if (is == null) {
                            // use file input stream if we build camel-core itself, and thus do not have a JAR which can be loaded by URLClassLoader
                            is = new FileInputStream(new File(core, "org/apache/camel/model/language/" + modelName + ".json"));
                        }
                        String json = loadText(is);
                        LanguageModel languageModel = new LanguageModel();
                        languageModel.setName(name);
                        languageModel.setTitle("");
                        languageModel.setModelName(modelName);
                        languageModel.setLabel("");
                        languageModel.setDescription("");
                        languageModel.setJavaType(javaType);
                        languageModel.setGroupId(project.getGroupId());
                        languageModel.setArtifactId(project.getArtifactId());
                        languageModel.setVersion(project.getVersion());
                        List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
                        for (Map<String, String> row : rows) {
                            if (row.containsKey("title")) {
                                // title may be special for some languages
                                String title = asTitle(name, row.get("title"));
                                languageModel.setTitle(title);
                            }
                            if (row.containsKey("description")) {
                                // description may be special for some languages
                                String desc = asDescription(name, row.get("description"));
                                languageModel.setDescription(desc);
                            }
                            if (row.containsKey("label")) {
                                languageModel.setLabel(row.get("label"));
                            }
                            if (row.containsKey("deprecated")) {
                                languageModel.setDeprecated(row.get("deprecated"));
                            }
                            if (row.containsKey("javaType")) {
                                languageModel.setModelJavaType(row.get("javaType"));
                            }
                            if (row.containsKey("firstVersion")) {
                                languageModel.setFirstVersion(row.get("firstVersion"));
                            }
                        }
                        log.debug("Model " + languageModel);
                        // build json schema for the data format
                        String properties = after(json, "  \"properties\": {");
                        String schema = createParameterJsonSchema(languageModel, properties);
                        log.debug("JSon schema\n" + schema);
                        // write this to the directory
                        File dir = new File(schemaOutDir, schemaSubDirectory(languageModel.getJavaType()));
                        dir.mkdirs();
                        File out = new File(dir, name + ".json");
                        OutputStream fos = buildContext.newFileOutputStream(out);
                        fos.write(schema.getBytes());
                        fos.close();
                        buildContext.refresh(out);
                        log.debug("Generated " + out + " containing JSon schema for " + name + " language");
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading language model from camel-core. Reason: " + e, e);
    }
    if (count > 0) {
        Properties properties = new Properties();
        String names = buffer.toString();
        properties.put("languages", names);
        properties.put("groupId", project.getGroupId());
        properties.put("artifactId", project.getArtifactId());
        properties.put("version", project.getVersion());
        properties.put("projectName", project.getName());
        if (project.getDescription() != null) {
            properties.put("projectDescription", project.getDescription());
        }
        camelMetaDir.mkdirs();
        File outFile = new File(camelMetaDir, "language.properties");
        // which can cause a re-compile of all the source code
        if (outFile.exists()) {
            try {
                Properties existing = new Properties();
                InputStream is = new FileInputStream(outFile);
                existing.load(is);
                is.close();
                // are the content the same?
                if (existing.equals(properties)) {
                    log.debug("No language changes detected");
                    return;
                }
            } catch (IOException e) {
            // ignore
            }
        }
        try {
            OutputStream os = buildContext.newFileOutputStream(outFile);
            properties.store(os, "Generated by camel-package-maven-plugin");
            os.close();
            log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "languages: " : "language: ") + names);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
        }
    } else {
        log.debug("No META-INF/services/org/apache/camel/language directory found. Are you sure you have created a Camel language?");
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) Properties(java.util.Properties) Artifact(org.apache.maven.artifact.Artifact) URL(java.net.URL) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashMap(java.util.HashMap) PackageHelper.parseAsMap(org.apache.camel.maven.packaging.PackageHelper.parseAsMap) Map(java.util.Map)

Example 5 with Resource

use of org.apache.maven.model.Resource in project camel by apache.

the class PackageOtherMojo method prepareOthers.

public static void prepareOthers(Log log, MavenProject project, MavenProjectHelper projectHelper, File otherOutDir, File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
    // are there any components, data formats or languages?
    for (Resource r : project.getBuild().getResources()) {
        File f = new File(r.getDirectory());
        if (!f.exists()) {
            f = new File(project.getBasedir(), r.getDirectory());
        }
        File comp = new File(f, "META-INF/services/org/apache/camel/component");
        if (comp.exists() && comp.isDirectory()) {
            return;
        }
        File df = new File(f, "META-INF/services/org/apache/camel/dataformat");
        if (df.exists() && df.isDirectory()) {
            return;
        }
        File lan = new File(f, "META-INF/services/org/apache/camel/language");
        if (lan.exists() && lan.isDirectory()) {
            return;
        }
    }
    // can stop the build before the end and eclipse always needs to know about that directory
    if (projectHelper != null) {
        projectHelper.addResource(project, otherOutDir.getPath(), Collections.singletonList("**/other.properties"), Collections.emptyList());
    }
    if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/component") && !PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/dataformat") && !PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/language")) {
        return;
    }
    String name = project.getArtifactId();
    // strip leading camel-
    if (name.startsWith("camel-")) {
        name = name.substring(6);
    }
    try {
        // create json model
        OtherModel otherModel = new OtherModel();
        otherModel.setName(name);
        otherModel.setGroupId(project.getGroupId());
        otherModel.setArtifactId(project.getArtifactId());
        otherModel.setVersion(project.getVersion());
        otherModel.setDescription(project.getDescription());
        if (project.getName() != null && project.getName().contains("(deprecated)")) {
            otherModel.setDeprecated("true");
        } else {
            otherModel.setDeprecated("false");
        }
        otherModel.setFirstVersion(project.getProperties().getProperty("firstVersion"));
        otherModel.setLabel(project.getProperties().getProperty("label"));
        String title = project.getProperties().getProperty("title");
        if (title == null) {
            title = camelDashToTitle(name);
        }
        otherModel.setTitle(title);
        log.debug("Model " + otherModel);
        // write this to the directory
        File dir = schemaOutDir;
        dir.mkdirs();
        File out = new File(dir, name + ".json");
        OutputStream fos = buildContext.newFileOutputStream(out);
        String json = createJsonSchema(otherModel);
        fos.write(json.getBytes());
        fos.close();
        buildContext.refresh(out);
        log.debug("Generated " + out + " containing JSon schema for " + name + " other");
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading language model from camel-core. Reason: " + e, e);
    }
    // now create properties file
    File camelMetaDir = new File(otherOutDir, "META-INF/services/org/apache/camel/");
    Properties properties = new Properties();
    properties.put("name", name);
    properties.put("groupId", project.getGroupId());
    properties.put("artifactId", project.getArtifactId());
    properties.put("version", project.getVersion());
    properties.put("projectName", project.getName());
    if (project.getDescription() != null) {
        properties.put("projectDescription", project.getDescription());
    }
    camelMetaDir.mkdirs();
    File outFile = new File(camelMetaDir, "other.properties");
    // which can cause a re-compile of all the source code
    if (outFile.exists()) {
        try {
            Properties existing = new Properties();
            InputStream is = new FileInputStream(outFile);
            existing.load(is);
            is.close();
            // are the content the same?
            if (existing.equals(properties)) {
                log.debug("No changes detected");
                return;
            }
        } catch (IOException e) {
        // ignore
        }
    }
    try {
        OutputStream os = buildContext.newFileOutputStream(outFile);
        properties.store(os, "Generated by camel-package-maven-plugin");
        os.close();
        log.info("Generated " + outFile);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) FileInputStream(java.io.FileInputStream)

Aggregations

Resource (org.apache.maven.model.Resource)63 File (java.io.File)47 MavenProjectResourcesStub (org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub)17 ArrayList (java.util.ArrayList)16 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)14 IOException (java.io.IOException)10 Properties (java.util.Properties)8 MojoFailureException (org.apache.maven.plugin.MojoFailureException)8 FileInputStream (java.io.FileInputStream)7 List (java.util.List)6 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 URL (java.net.URL)4 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 Iterator (java.util.Iterator)3 FileOutputStream (java.io.FileOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2