Example usage for org.apache.maven.project MavenProjectHelper attachArtifact

List of usage examples for org.apache.maven.project MavenProjectHelper attachArtifact

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProjectHelper attachArtifact.

Prototype

void attachArtifact(MavenProject project, String artifactType, String artifactClassifier, File artifactFile);

Source Link

Document

Add or replace an artifact to the current project.

Usage

From source file:com.github.maven_nar.AbstractNarLayout.java

License:Apache License

protected final void attachNar(final ArchiverManager archiverManager, final MavenProjectHelper projectHelper,
        final MavenProject project, final String classifier, final File dir, final String include)
        throws MojoExecutionException {
    final File narFile = new File(project.getBuild().getDirectory(),
            project.getBuild().getFinalName() + "-" + classifier + "." + NarConstants.NAR_EXTENSION);
    if (narFile.exists()) {
        narFile.delete();/*from w w w. j  a v a 2 s.  c  om*/
    }
    try {
        final Archiver archiver = archiverManager.getArchiver(NarConstants.NAR_ROLE_HINT);
        archiver.addDirectory(dir, new String[] { include }, null);
        archiver.setDestFile(narFile);
        archiver.createArchive();
    } catch (final NoSuchArchiverException e) {
        throw new MojoExecutionException("NAR: cannot find archiver", e);
    } catch (final ArchiverException e) {
        throw new MojoExecutionException("NAR: cannot create NAR archive '" + narFile + "'", e);
    } catch (final IOException e) {
        throw new MojoExecutionException("NAR: cannot create NAR archive '" + narFile + "'", e);
    }
    projectHelper.attachArtifact(project, NarConstants.NAR_TYPE, classifier, narFile);
}

From source file:com.sap.prd.mobile.ios.mios.XCodePackageManager.java

License:Apache License

static void attachLibrary(final XCodeContext xcodeContext, File buildDir, final MavenProject project,
        final MavenProjectHelper projectHelper) {

    final File fatBinary = XCodeBuildLayout.getBinary(buildDir, xcodeContext.getConfiguration(),
            xcodeContext.getSDK(), project.getArtifactId());

    if (!fatBinary.exists())
        throw new RuntimeException(fatBinary + " should be attached but does not exist.");

    final String classifier = xcodeContext.getConfiguration() + "-" + xcodeContext.getSDK();

    projectHelper.attachArtifact(project, "a", classifier, fatBinary);

    LOGGER.info("Archive file '" + fatBinary + "' attached as side artifact for '" + project.getArtifact()
            + "' with classifier '" + classifier + "'.");
}

From source file:net.oneandone.maven.plugins.prerelease.core.Prerelease.java

License:Apache License

public void artifactFiles(MavenProject project, MavenProjectHelper projectHelper) throws IOException {
    FileNode file;//from ww w.j ava2  s  .  c o m
    String type;
    String classifier;
    String[] tmp;

    for (Map.Entry<FileNode, String[]> entry : artifactFiles().entrySet()) {
        file = entry.getKey();
        tmp = entry.getValue();
        classifier = tmp[0];
        type = tmp[1];
        if ("pom".equals(type) && !project.getPackaging().equals("pom")) {
            // ignored
        } else {
            if (classifier == null) {
                project.getArtifact().setFile(file.toPath().toFile());
            } else {
                projectHelper.attachArtifact(project, type, classifier, file.toPath().toFile());
            }
        }
    }
}

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

License:Apache License

public static void generateArchetypeCatalog(Log log, MavenProject project, MavenProjectHelper projectHelper,
        File projectBuildDir, File outDir) throws MojoExecutionException, IOException {

    File rootDir = projectBuildDir.getParentFile();
    log.info("Scanning for Camel Maven Archetypes from root directory " + rootDir);

    // find all archetypes which are in the parent dir of the build dir
    File[] dirs = rootDir.listFiles(new FileFilter() {
        @Override/*ww  w .j  a  va 2  s  . co m*/
        public boolean accept(File pathname) {
            return pathname.getName().startsWith("camel-archetype") && pathname.isDirectory();
        }
    });

    List<ArchetypeModel> models = new ArrayList<ArchetypeModel>();

    for (File dir : dirs) {
        File pom = new File(dir, "pom.xml");
        if (!pom.exists() && !pom.isFile()) {
            continue;
        }

        boolean parent = false;
        ArchetypeModel model = new ArchetypeModel();

        // just use a simple line by line text parser (no need for DOM) just to grab 4 lines of data
        for (Object o : FileUtils.readLines(pom)) {

            String line = o.toString();

            // we only want to read version from parent
            if (line.contains("<parent>")) {
                parent = true;
                continue;
            }
            if (line.contains("</parent>")) {
                parent = false;
                continue;
            }
            if (parent) {
                // grab version from parent
                String version = between(line, "<version>", "</version>");
                if (version != null) {
                    model.setVersion(version);
                }
                continue;
            }

            String groupId = between(line, "<groupId>", "</groupId>");
            String artifactId = between(line, "<artifactId>", "</artifactId>");
            String description = between(line, "<description>", "</description>");

            if (groupId != null && model.getGroupId() == null) {
                model.setGroupId(groupId);
            }
            if (artifactId != null && model.getArtifactId() == null) {
                model.setArtifactId(artifactId);
            }
            if (description != null && model.getDescription() == null) {
                model.setDescription(description);
            }
        }

        if (model.getGroupId() != null && model.getArtifactId() != null && model.getVersion() != null) {
            models.add(model);
        }
    }

    log.info("Found " + models.size() + " archetypes");

    if (!models.isEmpty()) {

        // make sure there is a dir
        outDir.mkdirs();

        File out = new File(outDir, "archetype-catalog.xml");
        FileOutputStream fos = new FileOutputStream(out, false);

        // write top
        String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<archetype-catalog>\n  <archetypes>";
        fos.write(top.getBytes());

        // write each archetype
        for (ArchetypeModel model : models) {
            fos.write("\n    <archetype>".getBytes());
            fos.write(("\n      <groupId>" + model.getGroupId() + "</groupId>").getBytes());
            fos.write(("\n      <artifactId>" + model.getArtifactId() + "</artifactId>").getBytes());
            fos.write(("\n      <version>" + model.getVersion() + "</version>").getBytes());
            if (model.getDescription() != null) {
                fos.write(("\n      <description>" + model.getDescription() + "</description>").getBytes());
            }
            fos.write("\n    </archetype>".getBytes());
        }

        // write bottom
        String bottom = "\n  </archetypes>\n</archetype-catalog>\n";
        fos.write(bottom.getBytes());

        fos.close();

        log.info("Saved archetype catalog to file " + out);

        try {
            if (projectHelper != null) {
                log.info("Attaching archetype catalog to Maven project: " + project.getArtifactId());

                List<String> includes = new ArrayList<String>();
                includes.add("archetype-catalog.xml");
                projectHelper.addResource(project, outDir.getPath(), includes, new ArrayList<String>());
                projectHelper.attachArtifact(project, "xml", "archetype-catalog", out);
            }
        } catch (Exception e) {
            throw new MojoExecutionException("Failed to attach artifact to Maven project. Reason: " + e, e);
        }
    }
}

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());
        }/*from ww w.j a va 2s  . 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());
        }// w  w  w . ja  v a  2s  .c o 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 w w. j  a  v  a 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.neo4j.build.plugins.ease.EaseHelper.java

License:Apache License

static void writeAndAttachArtifactList(StringBuilder builder, MavenProject project,
        MavenProjectHelper projectHelper, Log log) throws MojoExecutionException {
    String buildDir = project.getBuild().getDirectory();
    String destFile = buildDir + File.separator + project.getArtifactId() + "-" + project.getVersion()
            + "-artifacts.txt";
    try {/* w ww . j a v a 2  s .c  om*/
        if (FileUtils.fileExists(destFile)) {
            FileUtils.fileDelete(destFile);
        }
        if (!FileUtils.fileExists(buildDir)) {
            FileUtils.mkdir(buildDir);
        }
        FileUtils.fileWrite(destFile, "UTF-8", builder.toString());
    } catch (IOException ioe) {
        throw new MojoExecutionException("Could not write artifact list.", ioe);
    }
    projectHelper.attachArtifact(project, "txt", "artifacts", FileUtils.getFile(destFile));
    log.info("Successfully attached artifact list to the project.");
}