Search in sources :

Example 41 with Resource

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

the class ValidateMojo method stripRootPath.

private String stripRootPath(String name) {
    // strip out any leading source / resource directory
    List list = project.getCompileSourceRoots();
    for (Object obj : list) {
        String dir = (String) obj;
        dir = asRelativeFile(dir);
        if (name.startsWith(dir)) {
            return name.substring(dir.length() + 1);
        }
    }
    list = project.getTestCompileSourceRoots();
    for (Object obj : list) {
        String dir = (String) obj;
        dir = asRelativeFile(dir);
        if (name.startsWith(dir)) {
            return name.substring(dir.length() + 1);
        }
    }
    List resources = project.getResources();
    for (Object obj : resources) {
        Resource resource = (Resource) obj;
        String dir = asRelativeFile(resource.getDirectory());
        if (name.startsWith(dir)) {
            return name.substring(dir.length() + 1);
        }
    }
    resources = project.getTestResources();
    for (Object obj : resources) {
        Resource resource = (Resource) obj;
        String dir = asRelativeFile(resource.getDirectory());
        if (name.startsWith(dir)) {
            return name.substring(dir.length() + 1);
        }
    }
    return name;
}
Also used : Resource(org.apache.maven.model.Resource) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with Resource

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

the class PackageComponentMojo method prepareComponent.

public static void prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper, File componentOutDir, BuildContext buildContext) throws MojoExecutionException {
    File camelMetaDir = new File(componentOutDir, "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, componentOutDir.getPath(), Collections.singletonList("**/component.properties"), Collections.emptyList());
    }
    if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/component")) {
        return;
    }
    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/component");
        if (f.exists() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null) {
                for (File file : files) {
                    // skip directories as there may be a sub .resolver directory
                    if (file.isDirectory()) {
                        continue;
                    }
                    String name = file.getName();
                    if (name.charAt(0) != '.') {
                        count++;
                        if (buffer.length() > 0) {
                            buffer.append(" ");
                        }
                        buffer.append(name);
                    }
                }
            }
        }
    }
    if (count > 0) {
        Properties properties = new Properties();
        String names = buffer.toString();
        properties.put("components", 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, "component.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 component 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 ? "components: " : "component: ") + 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/component directory found. Are you sure you have created a Camel component?");
    }
}
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) FileInputStream(java.io.FileInputStream)

Example 43 with Resource

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

the class PackageHelper method haveResourcesChanged.

public static boolean haveResourcesChanged(Log log, MavenProject project, BuildContext buildContext, String suffix) {
    String baseDir = project.getBasedir().getAbsolutePath();
    for (Resource r : project.getBuild().getResources()) {
        File file = new File(r.getDirectory());
        if (file.isAbsolute()) {
            file = new File(r.getDirectory().substring(baseDir.length() + 1));
        }
        String path = file.getPath() + "/" + suffix;
        log.debug("checking  if " + path + " (" + r.getDirectory() + "/" + suffix + ") has changed.");
        if (buildContext.hasDelta(path)) {
            log.debug("Indeed " + suffix + " has changed.");
            return true;
        }
    }
    return false;
}
Also used : Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 44 with Resource

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

the class SpringBootAutoConfigurationMojo method findDataFormatNames.

private List<String> findDataFormatNames() {
    List<String> dataFormatNames = new ArrayList<String>();
    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) {
                    // skip directories as there may be a sub .resolver directory
                    if (file.isDirectory()) {
                        continue;
                    }
                    String name = file.getName();
                    if (name.charAt(0) != '.') {
                        dataFormatNames.add(name);
                    }
                }
            }
        }
    }
    return dataFormatNames;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 45 with Resource

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

the class UpdateReadmeMojo method findComponentNames.

private List<String> findComponentNames() {
    List<String> componentNames = new ArrayList<String>();
    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/component");
        if (f.exists() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null) {
                for (File file : files) {
                    // skip directories as there may be a sub .resolver directory
                    if (file.isDirectory()) {
                        continue;
                    }
                    String name = file.getName();
                    if (name.charAt(0) != '.') {
                        componentNames.add(name);
                    }
                }
            }
        }
    }
    return componentNames;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) File(java.io.File)

Aggregations

Resource (org.apache.maven.model.Resource)65 File (java.io.File)49 MavenProjectResourcesStub (org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub)17 ArrayList (java.util.ArrayList)16 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)15 IOException (java.io.IOException)11 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)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 URLClassLoader (java.net.URLClassLoader)3 Iterator (java.util.Iterator)3 FileOutputStream (java.io.FileOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2