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

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

Introduction

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

Prototype

public String getGroupId() 

Source Link

Usage

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  a2s .co  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 w  w w  .  j  a v 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   w ww. j a  va2s .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 w w. j a va  2 s.  c  om
 * @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/*from  w ww.  j a  v a 2s .co  m*/
 * @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()));
}

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

License:Apache License

protected Properties getDefaultProperties(MavenProject currentProject) {
    Properties properties = new Properties();

    String bsn;//  w w w.ja v  a2 s  . c o m
    try {
        bsn = getMaven2OsgiConverter().getBundleSymbolicName(currentProject.getArtifact());
    } catch (Exception e) {
        bsn = currentProject.getGroupId() + "." + currentProject.getArtifactId();
    }

    // Setup defaults
    properties.put(MAVEN_SYMBOLICNAME, bsn);
    properties.put(Analyzer.BUNDLE_SYMBOLICNAME, bsn);
    properties.put(Analyzer.IMPORT_PACKAGE, "*");
    properties.put(Analyzer.BUNDLE_VERSION, getMaven2OsgiConverter().getVersion(currentProject.getVersion()));

    // remove the extraneous Include-Resource and Private-Package entries from generated manifest
    properties.put(Analyzer.REMOVE_HEADERS, Analyzer.INCLUDE_RESOURCE + ',' + Analyzer.PRIVATE_PACKAGE);

    header(properties, Analyzer.BUNDLE_DESCRIPTION, currentProject.getDescription());
    StringBuffer licenseText = printLicenses(currentProject.getLicenses());
    if (licenseText != null) {
        header(properties, Analyzer.BUNDLE_LICENSE, licenseText);
    }
    header(properties, Analyzer.BUNDLE_NAME, currentProject.getName());

    if (currentProject.getOrganization() != null) {
        String organizationName = currentProject.getOrganization().getName();
        header(properties, Analyzer.BUNDLE_VENDOR, organizationName);
        properties.put("project.organization.name", organizationName);
        properties.put("pom.organization.name", organizationName);
        if (currentProject.getOrganization().getUrl() != null) {
            String organizationUrl = currentProject.getOrganization().getUrl();
            header(properties, Analyzer.BUNDLE_DOCURL, organizationUrl);
            properties.put("project.organization.url", organizationUrl);
            properties.put("pom.organization.url", organizationUrl);
        }
    }

    properties.putAll(currentProject.getProperties());
    properties.putAll(currentProject.getModel().getProperties());
    properties.putAll(getProperties(currentProject.getModel(), "project.build."));
    properties.putAll(getProperties(currentProject.getModel(), "pom."));
    properties.putAll(getProperties(currentProject.getModel(), "project."));
    properties.put("project.baseDir", baseDir);
    properties.put("project.build.directory", getBuildDirectory());
    properties.put("project.build.outputdirectory", getOutputDirectory());

    properties.put("classifier", classifier == null ? "" : classifier);

    // Add default plugins
    header(properties, Analyzer.PLUGIN, BlueprintPlugin.class.getName() + "," + SpringXMLType.class.getName());

    return properties;
}

From source file:org.apache.felix.obr.plugin.AbstractFileMojo.java

License:Apache License

/**
 * @return project based on command-line settings, with bundle attached
 * @throws MojoExecutionException/* w  w  w.jav  a2 s .c o m*/
 */
public MavenProject getProject() throws MojoExecutionException {
    final MavenProject project;
    if (pomFile != null && pomFile.exists()) {
        project = PomHelper.readPom(pomFile);

        groupId = project.getGroupId();
        artifactId = project.getArtifactId();
        version = project.getVersion();
        packaging = project.getPackaging();
    } else {
        project = PomHelper.buildPom(groupId, artifactId, version, packaging);
    }

    if (groupId == null || artifactId == null || version == null || packaging == null) {
        throw new MojoExecutionException("Missing group, artifact, version, or packaging information");
    }

    Artifact bundle = m_factory.createArtifactWithClassifier(groupId, artifactId, version, packaging,
            classifier);
    project.setArtifact(bundle);

    return project;
}

From source file:org.apache.felix.obr.plugin.ResourcesBundle.java

License:Apache License

/**
 * this method gets information form pom.xml to complete missing data from those given by user.
 * @param project project information given by maven
 * @param ebi bundle information extracted from bindex
 * @return true/*from ww  w  .j a  v a 2s .  co  m*/
 */
public boolean construct(MavenProject project, ExtractBindexInfo ebi) {

    if (ebi.getPresentationName() != null) {
        setPresentationName(ebi.getPresentationName());
        if (project.getName() != null) {
            m_logger.debug("pom property override:<presentationname> " + project.getName());
        }
    } else {
        setPresentationName(project.getName());
    }

    if (ebi.getSymbolicName() != null) {
        setSymbolicName(ebi.getSymbolicName());
        if (project.getArtifactId() != null) {
            m_logger.debug("pom property override:<symbolicname> " + project.getArtifactId());
        }
    } else {
        setSymbolicName(project.getArtifactId());
    }

    if (ebi.getVersion() != null) {
        setVersion(ebi.getVersion());
        if (project.getVersion() != null) {
            m_logger.debug("pom property override:<version> " + project.getVersion());
        }
    } else {
        setVersion(project.getVersion());
    }

    if (ebi.getId() != null) {
        setId(ebi.getId());
    }

    if (ebi.getDescription() != null) {
        setDescription(ebi.getDescription());
        if (project.getDescription() != null) {
            m_logger.debug("pom property override:<description> " + project.getDescription());
        }
    } else {
        setDescription(project.getDescription());
    }

    if (ebi.getDocumentation() != null) {
        setDocumentation(ebi.getDocumentation());
        if (project.getUrl() != null) {
            m_logger.debug("pom property override:<documentation> " + project.getUrl());
        }
    } else {
        setDocumentation(project.getUrl());
    }

    if (ebi.getSource() != null) {
        setSource(ebi.getSource());
        if (project.getScm() != null) {
            m_logger.debug("pom property override:<source> " + project.getScm());
        }
    } else {
        String src = null;
        if (project.getScm() != null) {
            src = project.getScm().getUrl();
        }
        setSource(src);
    }

    if (ebi.getLicense() != null) {
        setLicense(ebi.getLicense());
        String lic = null;
        List l = project.getLicenses();
        Iterator it = l.iterator();
        while (it.hasNext()) {
            if (it.next() != null) {
                m_logger.debug("pom property override:<license> " + lic);
                break;
            }
        }
    } else {
        String lic = null;
        List l = project.getLicenses();
        Iterator it = l.iterator();
        while (it.hasNext()) {
            lic = it.next() + ";";
        }

        setLicense(lic);
    }

    // create the first capability (ie : bundle)
    Capability capability = new Capability();
    capability.setName("bundle");
    PElement p = new PElement();
    p.setN("manifestversion");
    p.setV("2");
    capability.addP(p);

    p = new PElement();
    p.setN("presentationname");
    p.setV(getPresentationName());
    capability.addP(p);

    p = new PElement();
    p.setN("symbolicname");
    p.setV(getSymbolicName());
    capability.addP(p);

    p = new PElement();
    p.setN("version");
    p.setT("version");
    p.setV(getVersion());
    capability.addP(p);

    addCapability(capability);

    List capabilities = ebi.getCapabilities();
    for (int i = 0; i < capabilities.size(); i++) {
        addCapability((Capability) capabilities.get(i));
    }

    List requirement = ebi.getRequirement();
    for (int i = 0; i < requirement.size(); i++) {
        addRequire((Require) requirement.get(i));
    }

    // we also add the goupId
    Category category = new Category();
    category.setId(project.getGroupId());
    addCategory(category);

    return true;
}

From source file:org.apache.felix.obrplugin.ResourcesBundle.java

License:Apache License

/**
 * this method gets information form pom.xml to complete missing data from those given by user.
 * @param project project information given by maven
 * @param ebi bundle information extracted from bindex
 * @param sourcePath path to local sources
 * @param javadocPath path to local javadocs
 * @return true//from  w w w.  ja va 2s.co m
 */
public boolean construct(MavenProject project, ExtractBindexInfo ebi, String sourcePath, String javadocPath) {

    if (ebi.getPresentationName() != null) {
        setPresentationName(ebi.getPresentationName());
        if (project.getName() != null) {
            m_logger.debug("pom property override:<presentationname> " + project.getName());
        }
    } else {
        setPresentationName(project.getName());
    }

    if (ebi.getSymbolicName() != null) {
        setSymbolicName(ebi.getSymbolicName());
        if (project.getArtifactId() != null) {
            m_logger.debug("pom property override:<symbolicname> " + project.getArtifactId());
        }
    } else {
        setSymbolicName(project.getArtifactId());
    }

    if (ebi.getVersion() != null) {
        setVersion(ebi.getVersion());
        if (project.getVersion() != null) {
            m_logger.debug("pom property override:<version> " + project.getVersion());
        }
    } else {
        setVersion(project.getVersion());
    }

    if (ebi.getId() != null) {
        setId(ebi.getId());
    }

    if (ebi.getDescription() != null) {
        setDescription(ebi.getDescription());
        if (project.getDescription() != null) {
            m_logger.debug("pom property override:<description> " + project.getDescription());
        }
    } else {
        setDescription(project.getDescription());
    }

    // fallback to javadoc if no project URL
    String documentation = project.getUrl();
    if (null == documentation) {
        documentation = javadocPath;
    }

    if (ebi.getDocumentation() != null) {
        setDocumentation(ebi.getDocumentation());
        if (documentation != null) {
            m_logger.debug("pom property override:<documentation> " + documentation);
        }
    } else {
        setDocumentation(documentation);
    }

    if (ebi.getSource() != null) {
        setSource(ebi.getSource());
        if (sourcePath != null) {
            m_logger.debug("pom property override:<source> " + sourcePath);
        }
    } else {
        setSource(sourcePath);
    }

    setJavadoc(javadocPath);

    if (ebi.getLicense() != null) {
        setLicense(ebi.getLicense());
        String lic = null;
        List l = project.getLicenses();
        Iterator it = l.iterator();
        while (it.hasNext()) {
            if (it.next() != null) {
                m_logger.debug("pom property override:<license> " + lic);
                break;
            }
        }
    } else {
        String lic = null;
        List l = project.getLicenses();
        Iterator it = l.iterator();
        while (it.hasNext()) {
            lic = it.next() + ";";
        }

        setLicense(lic);
    }

    // create the first capability (ie : bundle)
    Capability capability = new Capability();
    capability.setName("bundle");
    PElement p = new PElement();
    p.setN("manifestversion");
    p.setV("2");
    capability.addP(p);

    p = new PElement();
    p.setN("presentationname");
    p.setV(getPresentationName());
    capability.addP(p);

    p = new PElement();
    p.setN("symbolicname");
    p.setV(getSymbolicName());
    capability.addP(p);

    p = new PElement();
    p.setN("version");
    p.setT("version");
    p.setV(getVersion());
    capability.addP(p);

    addCapability(capability);

    List capabilities = ebi.getCapabilities();
    for (int i = 0; i < capabilities.size(); i++) {
        addCapability((Capability) capabilities.get(i));
    }

    List requirement = ebi.getRequirement();
    for (int i = 0; i < requirement.size(); i++) {
        addRequire((Require) requirement.get(i));
    }

    List categories = ebi.getCategories();
    for (int i = 0; i < categories.size(); i++) {
        addCategory((Category) categories.get(i));
    }

    // we also add the goupId
    Category category = new Category();
    category.setId(project.getGroupId());
    addCategory(category);

    return true;
}

From source file:org.apache.hyracks.maven.license.LicenseMojo.java

License:Apache License

private String toGav(MavenProject dep) {
    return dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion();
}