Example usage for org.apache.maven.project MavenProject getBuild

List of usage examples for org.apache.maven.project MavenProject getBuild

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject getBuild.

Prototype

public Build getBuild() 

Source Link

Usage

From source file:org.ajax4jsf.builder.mojo.AbstractCDKMojo.java

License:Open Source License

protected ClassLoader createProjectClassLoader(MavenProject project, boolean useCCL) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {/*ww  w.  j ava 2s .c o  m*/
        List<?> compileClasspathElements = project.getCompileClasspathElements();
        String outputDirectory = project.getBuild().getOutputDirectory();
        URL[] urls = new URL[compileClasspathElements.size() + 1];
        int i = 0;
        urls[i++] = new File(outputDirectory).toURI().toURL();
        for (Iterator<?> iter = compileClasspathElements.iterator(); iter.hasNext();) {
            String element = (String) iter.next();
            urls[i++] = new File(element).toURI().toURL();
        }

        if (useCCL) {
            classLoader = new URLClassLoader(urls, classLoader);
        } else {
            classLoader = new URLClassLoader(urls);
        }
    } catch (MalformedURLException e) {
        getLog().error("Bad URL in classpath", e);
    } catch (DependencyResolutionRequiredException e) {
        getLog().error("Dependencies not resolved ", e);
    }

    return classLoader;
}

From source file:org.apache.camel.maven.packaging.PackageComponentMojo.java

License:Apache License

public static void prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper,
        File componentOutDir) throws MojoExecutionException {
    File camelMetaDir = new File(componentOutDir, "META-INF/services/org/apache/camel/");

    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());
        }//ww w  .  jav  a  2 s.c o m
        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");
        try {
            properties.store(new FileWriter(outFile), "Generated by camel-package-maven-plugin");
            log.info("Generated " + outFile + " containing " + count + " Camel "
                    + (count > 1 ? "components: " : "component: ") + names);

            if (projectHelper != null) {
                List<String> includes = new ArrayList<String>();
                includes.add("**/component.properties");
                projectHelper.addResource(project, componentOutDir.getPath(), includes,
                        new ArrayList<String>());
                projectHelper.attachArtifact(project, "properties", "camelComponent", outFile);
            }
        } 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?");
    }
}

From source file:org.apache.camel.maven.packaging.PackageDataFormatMojo.java

License:Apache License

public static void prepareDataFormat(Log log, MavenProject project, MavenProjectHelper projectHelper,
        File dataFormatOutDir, File schemaOutDir) throws MojoExecutionException {
    File camelMetaDir = new File(dataFormatOutDir, "META-INF/services/org/apache/camel/");

    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());
        }/*from   w  w w.j  a  v  a  2 s . co  m*/
        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) != '.') {
                        count++;
                        if (buffer.length() > 0) {
                            buffer.append(" ");
                        }
                        buffer.append(name);
                    }

                    // find out the javaType for each data format
                    try {
                        String text = loadText(new FileInputStream(file));
                        Map<String, String> map = parseAsMap(text);
                        String javaType = map.get("class");
                        if (javaType != null) {
                            javaTypes.put(name, javaType);
                        }
                    } catch (IOException e) {
                        throw new MojoExecutionException("Failed to read file " + file + ". Reason: " + e, e);
                    }
                }
            }
        }
    }

    // find camel-core and grab the data format model from there, and enrich this model with information from this artifact
    // 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);
                        if (json != null) {
                            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("javaType")) {
                                    dataFormatModel.setModelJavaType(row.get("javaType"));
                                }
                                // override description for camel-core, as otherwise its too generic
                                if ("camel-core".equals(project.getArtifactId())) {
                                    if (row.containsKey("description")) {
                                        dataFormatModel.setLabel(row.get("description"));
                                    }
                                }
                            }
                            log.debug("Model " + dataFormatModel);

                            // build json schema for the data format
                            String properties = after(json, "  \"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");
                            FileOutputStream fos = new FileOutputStream(out, false);
                            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");
        try {
            properties.store(new FileWriter(outFile), "Generated by camel-package-maven-plugin");
            log.info("Generated " + outFile + " containing " + count + " Camel "
                    + (count > 1 ? "dataformats: " : "dataformat: ") + names);

            if (projectHelper != null) {
                List<String> includes = new ArrayList<String>();
                includes.add("**/dataformat.properties");
                projectHelper.addResource(project, dataFormatOutDir.getPath(), includes,
                        new ArrayList<String>());
                projectHelper.attachArtifact(project, "properties", "camelDataFormat", outFile);
            }
        } 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?");
    }
}

From source file:org.apache.camel.maven.packaging.PackageLanguageMojo.java

License:Apache License

public static void prepareLanguage(Log log, MavenProject project, MavenProjectHelper projectHelper,
        File languageOutDir, File schemaOutDir) throws MojoExecutionException {
    File camelMetaDir = new File(languageOutDir, "META-INF/services/org/apache/camel/");

    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());
        }/*from   w  ww  .j  a va 2 s  .  c o m*/
        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) {
                    // skip directories as there may be a sub .resolver directory such as in camel-script
                    if (file.isDirectory()) {
                        continue;
                    }
                    String name = file.getName();
                    if (name.charAt(0) != '.') {
                        count++;
                        if (buffer.length() > 0) {
                            buffer.append(" ");
                        }
                        buffer.append(name);
                    }

                    // find out the javaType for each data format
                    try {
                        String text = loadText(new FileInputStream(file));
                        Map<String, String> map = parseAsMap(text);
                        String javaType = map.get("class");
                        if (javaType != null) {
                            javaTypes.put(name, javaType);
                        }
                    } catch (IOException e) {
                        throw new MojoExecutionException("Failed to read file " + file + ". Reason: " + e, e);
                    }
                }
            }
        }
    }

    // find camel-core and grab the data format model from there, and enrich this model with information from this artifact
    // 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);
                        if (json != null) {
                            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")) {
                                    languageModel.setTitle(row.get("title"));
                                }
                                if (row.containsKey("description")) {
                                    languageModel.setDescription(row.get("description"));
                                }
                                if (row.containsKey("label")) {
                                    languageModel.setLabel(row.get("label"));
                                }
                                if (row.containsKey("javaType")) {
                                    languageModel.setModelJavaType(row.get("javaType"));
                                }
                            }
                            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");
                            FileOutputStream fos = new FileOutputStream(out, false);
                            fos.write(schema.getBytes());
                            fos.close();

                            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");
        try {
            properties.store(new FileWriter(outFile), "Generated by camel-package-maven-plugin");
            log.info("Generated " + outFile + " containing " + count + " Camel "
                    + (count > 1 ? "languages: " : "language: ") + names);

            if (projectHelper != null) {
                List<String> includes = new ArrayList<String>();
                includes.add("**/language.properties");
                projectHelper.addResource(project, languageOutDir.getPath(), includes, new ArrayList<String>());
                projectHelper.attachArtifact(project, "properties", "camelLanguage", outFile);
            }
        } 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?");
    }
}

From source file:org.apache.cxf.maven_plugin.ClassLoaderSwitcher.java

License:Apache License

/**
 * Create and set the classloader that is needed for creating the java sources from wsdl
 *
 * @param project//  w  w w.ja v  a 2  s . co m
 * @param useCompileClasspath
 * @param classesDir
 */
public String switchClassLoader(MavenProject project, boolean useCompileClasspath, String classpath,
        List<?> classpathElements) {
    List<URL> urlList = new ArrayList<>();
    StringBuilder buf = new StringBuilder();

    try {
        buf.append(classpath);
        buf.append(File.pathSeparatorChar);
        urlList.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
    } catch (MalformedURLException e) {
        // ignore
    }
    for (Object classpathElement : classpathElements) {
        buf.append(classpathElement.toString());
        buf.append(File.pathSeparatorChar);
    }

    buf.append(File.pathSeparatorChar);
    @SuppressWarnings("deprecation")
    List<?> artifacts = useCompileClasspath ? project.getCompileArtifacts() : project.getTestArtifacts();
    for (Artifact a : CastUtils.cast(artifacts, Artifact.class)) {
        try {
            if (a.getFile() != null && a.getFile().exists()) {
                urlList.add(a.getFile().toURI().toURL());
                buf.append(a.getFile().getAbsolutePath());
                buf.append(File.pathSeparatorChar);
                // System.out.println("     " +
                // a.getFile().getAbsolutePath());
            }
        } catch (MalformedURLException e) {
            // ignore
        }
    }

    origContextClassloader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = ClassLoaderUtils.getURLClassLoader(urlList, origContextClassloader);
    String newCp = buf.toString();

    log.debug("Classpath: " + urlList.toString());

    origProps = new HashMap<>(System.getProperties());

    origClassPath = System.getProperty("java.class.path");

    Thread.currentThread().setContextClassLoader(loader);
    System.setProperty("java.class.path", newCp);
    return newCp;
}

From source file:org.apache.cxf.maven_plugin.common.ClassLoaderSwitcher.java

License:Apache License

/**
 * Create and set the classloader that is needed for creating the java sources from wsdl
 *
 * @param project/*from w  w w.  j a v a  2  s. c o m*/
 * @param useCompileClasspath
 * @param classesDir
 */
public Set<URI> switchClassLoader(MavenProject project, boolean useCompileClasspath, File classesDir) {
    List<URL> urlList = new ArrayList<>();
    StringBuilder buf = new StringBuilder();
    Set<URI> ret = new LinkedHashSet<URI>();

    try {
        urlList.add(classesDir.toURI().toURL());
        if (!useCompileClasspath) {
            urlList.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
        }
    } catch (MalformedURLException e) {
        // ignore
    }

    buf.append(classesDir.getAbsolutePath());
    ret.add(classesDir.toURI());
    buf.append(File.pathSeparatorChar);
    if (!useCompileClasspath) {
        buf.append(project.getBuild().getOutputDirectory());
        ret.add(new File(project.getBuild().getOutputDirectory()).toURI());
        buf.append(File.pathSeparatorChar);
    }
    @SuppressWarnings("deprecation")
    List<?> artifacts = useCompileClasspath ? project.getCompileArtifacts() : project.getTestArtifacts();
    for (Artifact a : CastUtils.cast(artifacts, Artifact.class)) {
        try {
            if (a.getFile() != null && a.getFile().exists()) {
                urlList.add(a.getFile().toURI().toURL());
                buf.append(a.getFile().getAbsolutePath());
                ret.add(a.getFile().toURI());
                buf.append(File.pathSeparatorChar);
                // System.out.println("     " +
                // a.getFile().getAbsolutePath());
            }
        } catch (MalformedURLException e) {
            // ignore
        }
    }

    origContextClassloader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = ClassLoaderUtils.getURLClassLoader(urlList, origContextClassloader);
    String newCp = buf.toString();

    log.debug("Classpath: " + urlList.toString());

    origProps = new HashMap<>(System.getProperties());

    origClassPath = System.getProperty("java.class.path");

    Thread.currentThread().setContextClassLoader(loader);
    System.setProperty("java.class.path", newCp);
    return ret;
}

From source file:org.apache.felix.bundleplugin.BundlePlugin.java

License:Apache License

/**
 * TODO this should return getMaven2Osgi().getBundleFileName( project.getArtifact() )
 *//*from  w  w w  .j  av a2s.  co  m*/
protected String getBundleName(MavenProject currentProject) {
    String finalName = currentProject.getBuild().getFinalName();
    if (null != classifier && classifier.trim().length() > 0) {
        return finalName + '-' + classifier + ".jar";
    }
    return finalName + ".jar";
}

From source file:org.apache.isis.tool.mavenplugin.IsisMojoSwagger.java

License:Apache License

private static File determineOutputDir(final MavenProject mavenProject, final String output) {
    final String targetDir = mavenProject.getBuild().getDirectory();
    final String outputDirStr = targetDir + File.separator + output;
    return new File(outputDirStr);
}

From source file:org.apache.isis.tool.mavenplugin.IsisMojoXsd.java

License:Apache License

private File determineOutputDir(final MavenProject mavenProject) {
    final String targetDir = mavenProject.getBuild().getDirectory();
    final String outputDirStr = targetDir + File.separator + output;
    return new File(outputDirStr);
}

From source file:org.apache.james.mailet.DefaultDescriptorsExtractor.java

License:Apache License

private void logDirectories(MavenProject project, Log log) {
    if (log.isDebugEnabled()) {
        log.debug("OutDir: " + project.getBuild().getOutputDirectory());
    }//from   ww  w .  j av  a2  s.c  o m
}