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

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

Introduction

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

Prototype

public String getArtifactId() 

Source Link

Usage

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

License:Open Source License

private List<MavenProject> populateReactorProjects() {
    List<MavenProject> projects = new ArrayList<MavenProject>();
    if (reactorProjects != null && reactorProjects.size() > 1) {
        Iterator reactorItr = reactorProjects.iterator();

        while (reactorItr.hasNext()) {
            MavenProject reactorProject = (MavenProject) reactorItr.next();

            if (reactorProject != null && reactorProject.getParent() != null
                    && project.getArtifactId().equals(reactorProject.getParent().getArtifactId())) {
                String name = reactorProject.getGroupId() + ":" + reactorProject.getArtifactId();
                getLog().info("Have reactor project with name " + name);
                projects.add(reactorProject);
            }//from  w w w .j a  v  a  2s . c  o m
        }
    }
    return projects;
}

From source file:org.alfresco.maven.plugin.amp.AbstractAmpMojo.java

License:Open Source License

/**
 * Builds the webapp for the specified project with the new packaging task
 * thingy//from  w w  w . jav a 2s.  c  o  m
 * <p/>
 * Classes, libraries and tld files are copied to
 * the <tt>webappDirectory</tt> during this phase.
 *
 * @param project         the maven project
 * @param webappDirectory the target directory
 * @throws MojoExecutionException if an error occured while packaging the webapp
 * @throws MojoFailureException   if an unexpected error occured while packaging the webapp
 * @throws IOException            if an error occured while copying the files
 */
public void buildAmp(MavenProject project, File webappDirectory)
        throws MojoExecutionException, MojoFailureException, IOException {

    AmpStructure cache;
    if (mUseCache && mCacheFile.exists()) {
        cache = new AmpStructure(webappStructureSerialier.fromXml(mCacheFile));
    } else {
        cache = new AmpStructure(null);
    }

    final long startTime = System.currentTimeMillis();
    getLog().info("Assembling AMP [" + project.getArtifactId() + "] in [" + webappDirectory + "]");

    final OverlayManager overlayManager = new OverlayManager(mOverlays, project, dependentAmpIncludes,
            dependentAmpExcludes);
    final List packagingTasks = getPackagingTasks(overlayManager);
    final AmpPackagingContext context = new DefaultAmpPackagingContext(webappDirectory, cache, overlayManager);
    final Iterator it = packagingTasks.iterator();
    while (it.hasNext()) {
        AmpPackagingTask ampPackagingTask = (AmpPackagingTask) it.next();
        ampPackagingTask.performPackaging(context);
    }

    // Post packaging
    final List postPackagingTasks = getPostPackagingTasks();
    final Iterator it2 = postPackagingTasks.iterator();
    while (it2.hasNext()) {
        AmpPostPackagingTask task = (AmpPostPackagingTask) it2.next();
        task.performPostPackaging(context);

    }
    getLog().info("AMP assembled in[" + (System.currentTimeMillis() - startTime) + " msecs]");

}

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 va2 s.  c om*/
        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());
        }//w  w  w  .j av  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());
        }/*ww  w . j a v a2s  . 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());
        }/*www  . 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.apache.cxf.maven_plugin.wadlto.AbstractCodeGeneratorMojo.java

License:Apache License

private Artifact resolveRemoteWadlArtifact(Artifact artifact) throws MojoExecutionException {

    /**//from   www . j  av  a 2 s.c  o  m
     * First try to find the artifact in the reactor projects of the maven session.
     * So an artifact that is not yet built can be resolved
     */
    List<MavenProject> rProjects = mavenSession.getProjects();
    for (MavenProject rProject : rProjects) {
        if (artifact.getGroupId().equals(rProject.getGroupId())
                && artifact.getArtifactId().equals(rProject.getArtifactId())
                && artifact.getVersion().equals(rProject.getVersion())) {
            Set<Artifact> artifacts = rProject.getArtifacts();
            for (Artifact pArtifact : artifacts) {
                if ("wadl".equals(pArtifact.getType())) {
                    return pArtifact;
                }
            }
        }
    }

    ArtifactResolutionRequest request = new ArtifactResolutionRequest();
    request.setArtifact(artifact);
    request.setResolveRoot(true).setResolveTransitively(false);
    request.setServers(mavenSession.getRequest().getServers());
    request.setMirrors(mavenSession.getRequest().getMirrors());
    request.setProxies(mavenSession.getRequest().getProxies());
    request.setLocalRepository(mavenSession.getLocalRepository());
    request.setRemoteRepositories(mavenSession.getRequest().getRemoteRepositories());
    ArtifactResolutionResult result = repositorySystem.resolve(request);

    return result.getOriginatingArtifact();
}

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

License:Apache License

@SuppressWarnings("unchecked")
public Artifact resolveRemoteWsdlArtifact(List remoteRepos, Artifact artifact) throws MojoExecutionException {

    /**/*from ww  w  .  j  ava 2s . c o  m*/
     * First try to find the artifact in the reactor projects of the maven session.
     * So an artifact that is not yet built can be resolved
     */
    List<MavenProject> rProjects = mavenSession.getSortedProjects();
    for (MavenProject rProject : rProjects) {
        if (artifact.getGroupId().equals(rProject.getGroupId())
                && artifact.getArtifactId().equals(rProject.getArtifactId())
                && artifact.getVersion().equals(rProject.getVersion())) {
            Set<Artifact> artifacts = rProject.getArtifacts();
            for (Artifact pArtifact : artifacts) {
                if ("wsdl".equals(artifact.getType())) {
                    return pArtifact;
                }
            }
        }
    }

    /**
     * If this did not work resolve the artifact using the artifactResolver
     */
    try {
        artifactResolver.resolve(artifact, remoteRepos, localRepository);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Error downloading wsdl artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Resource can not be found.", e);
    }

    return artifact;
}

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

License:Apache License

/**
 * Bundle one project only without building its childre
 * //from   w  ww.  j  a  va2 s .  c  o  m
 * @param project
 * @throws MojoExecutionException
 */
protected BundleInfo bundle(MavenProject project) throws MojoExecutionException {
    Artifact artifact = project.getArtifact();
    getLog().info("Bundling " + artifact);

    try {
        Map instructions = new LinkedHashMap();
        instructions.put(Analyzer.IMPORT_PACKAGE, wrapImportPackage);

        project.getArtifact().setFile(getFile(artifact));
        File outputFile = getOutputFile(artifact);

        if (project.getArtifact().getFile().equals(outputFile)) {
            /* TODO find the cause why it's getting here */
            return null;
            //                getLog().error(
            //                                "Trying to read and write " + artifact + " to the same file, try cleaning: "
            //                                    + outputFile );
            //                throw new IllegalStateException( "Trying to read and write " + artifact
            //                    + " to the same file, try cleaning: " + outputFile );
        }

        Analyzer analyzer = getAnalyzer(project, instructions, new Properties(), getClasspath(project));

        Jar osgiJar = new Jar(project.getArtifactId(), project.getArtifact().getFile());

        outputFile.getAbsoluteFile().getParentFile().mkdirs();

        Collection exportedPackages;
        if (isOsgi(osgiJar)) {
            /* if it is already an OSGi jar copy it as is */
            getLog().info("Using existing OSGi bundle for " + project.getGroupId() + ":"
                    + project.getArtifactId() + ":" + project.getVersion());
            String exportHeader = osgiJar.getManifest().getMainAttributes().getValue(Analyzer.EXPORT_PACKAGE);
            exportedPackages = analyzer.parseHeader(exportHeader).keySet();
            FileUtils.copyFile(project.getArtifact().getFile(), outputFile);
        } else {
            /* else generate the manifest from the packages */
            exportedPackages = analyzer.getExports().keySet();
            Manifest manifest = analyzer.getJar().getManifest();
            osgiJar.setManifest(manifest);
            osgiJar.write(outputFile);
        }

        BundleInfo bundleInfo = addExportedPackages(project, exportedPackages);

        // cleanup...
        analyzer.close();
        osgiJar.close();

        return bundleInfo;
    }
    /* too bad Jar.write throws Exception */
    catch (Exception e) {
        throw new MojoExecutionException(
                "Error generating OSGi bundle for project " + getArtifactKey(project.getArtifact()), e);
    }
}

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

License:Apache License

/**
 * @param jar//w  ww  . j a v  a  2 s.c  om
 * @throws IOException
 */
private void doMavenMetadata(MavenProject currentProject, Jar jar) throws IOException {
    String path = "META-INF/maven/" + currentProject.getGroupId() + "/" + currentProject.getArtifactId();
    File pomFile = new File(baseDir, "pom.xml");
    jar.putResource(path + "/pom.xml", new FileResource(pomFile));

    Properties p = new Properties();
    p.put("version", currentProject.getVersion());
    p.put("groupId", currentProject.getGroupId());
    p.put("artifactId", currentProject.getArtifactId());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    p.store(out, "Generated by org.apache.felix.bundleplugin");
    jar.putResource(path + "/pom.properties",
            new EmbeddedResource(out.toByteArray(), System.currentTimeMillis()));
}