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.gephi.maven.BuildMetadata.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    String gephiVersion = (String) project.getProperties().get("gephi.version");
    if (gephiVersion == null) {
        throw new MojoExecutionException("The 'gephi.version' property should be defined");
    }/*from w  w w.  ja v a  2 s .  c  o m*/
    getLog().debug("Building metadata for 'gephi.version=" + gephiVersion + "'");

    if (reactorProjects != null && reactorProjects.size() > 0) {
        getLog().debug("Found " + reactorProjects.size() + " projects in reactor");
        List<MavenProject> modules = new ArrayList<MavenProject>();
        for (MavenProject proj : reactorProjects) {
            if (proj.getPackaging().equals("nbm")) {
                String gephiVersionModule = proj.getProperties().getProperty("gephi.version");

                if (gephiVersionModule.equals(gephiVersion)) {
                    getLog().debug("Found 'nbm' project '" + proj.getName() + "' with artifactId="
                            + proj.getArtifactId() + " and groupId=" + proj.getGroupId());
                    modules.add(proj);
                } else {
                    getLog().debug("Ignored project '" + proj.getName() + "' based on 'gephi.version' value '"
                            + gephiVersionModule + "'");
                }
            }
        }

        ManifestUtils manifestUtils = new ManifestUtils(sourceManifestFile, getLog());

        // Get all modules with dependencies
        Map<MavenProject, List<MavenProject>> tree = ModuleUtils.getModulesTree(modules, getLog());

        //Download previous file
        File pluginsJsonFile = new File(outputDirectory, "plugins.json");
        try {
            URL url = new URL(metadataUrl + "plugins.json");
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            connection.connect();
            InputStream stream = connection.getInputStream();
            ReadableByteChannel rbc = Channels.newChannel(stream);
            FileOutputStream fos = new FileOutputStream(pluginsJsonFile);
            long read = fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            rbc.close();
            stream.close();
            getLog().debug("Read " + read + "bytes from url '" + url + "' and write to '"
                    + pluginsJsonFile.getAbsolutePath() + "'");
        } catch (Exception e) {
            throw new MojoExecutionException("Error while downloading previous 'plugins.json'", e);
        }

        // Init json
        PluginsMetadata pluginsMetadata;
        Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
        if (pluginsJsonFile.exists()) {
            try {
                FileReader reader = new FileReader(pluginsJsonFile);
                pluginsMetadata = gson.fromJson(reader, PluginsMetadata.class);
                reader.close();
                getLog().debug("Read previous plugins.json file");
            } catch (JsonSyntaxException e) {
                throw new MojoExecutionException("Error while reading previous 'plugins.json'", e);
            } catch (JsonIOException e) {
                throw new MojoExecutionException("Error while reading previous 'plugins.json'", e);
            } catch (IOException e) {
                throw new MojoExecutionException("Error while reading previous 'plugins.json'", e);
            }
        } else {
            pluginsMetadata = new PluginsMetadata();
            pluginsMetadata.plugins = new ArrayList<PluginMetadata>();
            getLog().debug("Create plugins.json");
        }

        // Build metadata
        for (Map.Entry<MavenProject, List<MavenProject>> entry : tree.entrySet()) {
            MavenProject topPlugin = entry.getKey();
            PluginMetadata pm = new PluginMetadata();
            pm.id = topPlugin.getArtifactId();

            // Find previous
            boolean foundPrevious = false;
            for (PluginMetadata oldPm : pluginsMetadata.plugins) {
                if (oldPm.id.equals(pm.id)) {
                    pm = oldPm;
                    foundPrevious = true;
                    getLog().debug("Found matching plugin id=" + pm.id + " in previous plugins.json");
                    break;
                }
            }

            manifestUtils.readManifestMetadata(topPlugin, pm);
            pm.license = MetadataUtils.getLicenseName(topPlugin);
            pm.authors = MetadataUtils.getAuthors(topPlugin);
            pm.last_update = dateFormat.format(new Date());
            pm.readme = MetadataUtils.getReadme(topPlugin, getLog());
            pm.images = ScreenshotUtils.copyScreenshots(topPlugin,
                    new File(outputDirectory, "imgs" + File.separator + pm.id), "imgs" + "/" + pm.id + "/",
                    getLog());
            pm.homepage = MetadataUtils.getHomepage(topPlugin);
            pm.sourcecode = MetadataUtils.getSourceCode(topPlugin, getLog());

            if (pm.versions == null) {
                pm.versions = new HashMap<String, Version>();
            }
            Version v = new Version();
            v.last_update = dateFormat.format(new Date());
            v.url = gephiVersion + "/" + ModuleUtils.getModuleDownloadPath(entry.getKey(), entry.getValue(),
                    new File(outputDirectory, gephiVersion), getLog());
            pm.versions.put(gephiVersion, v);

            if (!foundPrevious) {
                pluginsMetadata.plugins.add(pm);
            }
        }

        String json = gson.toJson(pluginsMetadata);

        // Write json file
        try {
            FileWriter writer = new FileWriter(pluginsJsonFile);
            writer.append(json);
            writer.close();
        } catch (IOException ex) {
            throw new MojoExecutionException("Error while writing plugins.json file", ex);
        }
    } else {
        throw new MojoExecutionException("The project should be a reactor project");
    }
}

From source file:org.gephi.maven.ModuleUtils.java

License:Apache License

/**
 * Investigate modules dependencies and return a map where keys are
 * top-level modules and values are all modules that it depends on plus the
 * key module./*  w  ww  .  j a v a2 s  . c o  m*/
 *
 * @param modules list of modules
 * @param log log
 * @return map that represents the tree of modules
 */
protected static Map<MavenProject, List<MavenProject>> getModulesTree(List<MavenProject> modules, Log log) {
    Map<MavenProject, List<MavenProject>> result = new LinkedHashMap<MavenProject, List<MavenProject>>();
    for (MavenProject proj : modules) {
        List<Dependency> dependencies = proj.getDependencies();
        log.debug("Investigating the " + dependencies.size() + " dependencies of project '" + proj.getName()
                + "'");

        // Init
        List<MavenProject> deps = new ArrayList<MavenProject>();
        deps.add(proj);
        result.put(proj, deps);

        // Add all module-based dependencies
        for (Dependency d : dependencies) {
            for (MavenProject projDependency : modules) {
                if (projDependency != proj && projDependency.getArtifactId().equals(d.getArtifactId())
                        && projDependency.getGroupId().equals(d.getGroupId())
                        && projDependency.getVersion().equals(d.getVersion())) {
                    log.debug("Found a dependency that matches another module '" + proj.getName() + "' -> '"
                            + projDependency.getName() + "'");
                    deps.add(projDependency);
                }
            }
        }
    }

    // Remove modules that are entirely dependencies of others
    List<MavenProject> toBeRemoved = new ArrayList<MavenProject>();
    for (MavenProject proj : modules) {
        List<MavenProject> projDeps = result.get(proj);
        for (MavenProject proj2 : modules) {
            if (proj != proj2) {
                if (result.get(proj2).containsAll(projDeps)) {
                    log.debug("Remove '" + proj.getName()
                            + "' from list of top modules because is a dependency of '" + proj2.getName()
                            + "'");
                    toBeRemoved.add(proj);
                    break;
                }
            }
        }
    }
    for (MavenProject mp : toBeRemoved) {
        result.remove(mp);
    }

    return result;
}

From source file:org.gephi.maven.ModuleUtils.java

License:Apache License

protected static String getModuleDownloadPath(MavenProject topPlugin, List<MavenProject> modules,
        File directory, Log log) throws MojoExecutionException {
    File dest;/*from   w ww . ja  va2s  .c om*/
    if (modules.size() > 1) {
        dest = new File(directory, topPlugin.getArtifactId() + "-" + topPlugin.getVersion() + ".zip");
        log.debug("The plugin '" + topPlugin + "' is a suite, creating zip archive at '"
                + dest.getAbsolutePath() + "'");

        // Verify files exist and add to archive
        try {
            ZipArchiver archiver = new ZipArchiver();
            for (MavenProject module : modules) {

                File f = new File(directory, module.getArtifactId() + "-" + module.getVersion() + ".nbm");
                if (!f.exists()) {
                    throw new MojoExecutionException(
                            "The NBM file '" + f.getAbsolutePath() + "' can't be found");
                }
                archiver.addFile(f, f.getName());
                log.debug("  Add file '" + f.getAbsolutePath() + "' to the archive");
            }
            archiver.setCompress(false);
            archiver.setDestFile(dest);
            archiver.setForced(true);
            archiver.createArchive();
            log.info("Created ZIP archive for project '" + topPlugin.getName() + "' at '"
                    + dest.getAbsolutePath() + "'");
        } catch (IOException ex) {
            throw new MojoExecutionException(
                    "Something went wrong with the creation of the ZIP archive for project '"
                            + topPlugin.getName() + "'",
                    ex);
        } catch (ArchiverException ex) {
            throw new MojoExecutionException(
                    "Something went wrong with the creation of the ZIP archive for project '"
                            + topPlugin.getName() + "'",
                    ex);
        }
    } else {
        dest = new File(directory, topPlugin.getArtifactId() + "-" + topPlugin.getVersion() + ".nbm");
        log.debug("The plugin is not a suite, return nbm file '" + dest.getAbsolutePath() + "'");
    }
    return dest.getName();
}

From source file:org.gephi.maven.Validate.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    manifestUtils = new ManifestUtils(sourceManifestFile, getLog());
    if (reactorProjects != null && reactorProjects.size() > 0) {
        getLog().debug("Found " + reactorProjects.size() + " projects in reactor");
        List<MavenProject> modules = new ArrayList<MavenProject>();
        for (MavenProject proj : reactorProjects) {
            if (proj.getPackaging().equals("nbm")) {
                getLog().debug("Found 'nbm' project '" + proj.getName() + "' with artifactId="
                        + proj.getArtifactId() + " and groupId=" + proj.getGroupId());
                modules.add(proj);/*from   ww w  .jav a2s.  com*/
            }
        }

        if (modules.isEmpty()) {
            throw new MojoExecutionException(
                    "No 'nbm' modules have been detected, make sure to add folders to <modules> into pom");
        } else if (modules.size() == 1) {
            // Unique NBM module
            executeSingleModuleProject(modules.get(0));
        } else {
            executeMultiModuleProject(modules);
        }
    } else {
        throw new MojoExecutionException("The project should be a reactor project");
    }
}

From source file:org.glassfish.jersey.tools.plugins.GenerateJerseyModuleListMojo.java

License:Open Source License

private StringBuilder processModuleList(String categoryCaption, String categoryId,
        List<MavenProject> projectsInCategory) {

    StringBuilder categoryContent = new StringBuilder();
    // Output table header for the category
    categoryContent.append(configuration.getTableHeader().replace(CATEGORY_CAPTION_PLACEHOLDER, categoryCaption)
            .replace(CATEGORY_GROUP_ID_PLACEHOLDER, categoryId));

    // Sort projects in each category alphabetically
    Collections.sort(projectsInCategory, new Comparator<MavenProject>() {
        @Override/*from w ww  . ja v  a 2 s . co m*/
        public int compare(MavenProject o1, MavenProject o2) {
            return o1.getArtifactId().compareTo(o2.getArtifactId());
        }
    });

    // Output projects in a category
    for (MavenProject project : projectsInCategory) {
        // skip the "parent" type projects
        if (project.getArtifactId().equals("project")) {
            continue;
        }

        String linkPrefix = getLinkPath(project);

        categoryContent
                .append(configuration.getTableRow().replace(MODULE_NAME_PLACEHOLDER, project.getArtifactId())
                        .replace(MODULE_DESCRIPTION_PLACEHOLDER, project.getDescription())
                        .replace(MODULE_LINK_PATH_PLACEHOLDER, linkPrefix + project.getArtifactId()));
    }

    categoryContent.append(configuration.getTableFooter());
    return categoryContent;
}

From source file:org.glassfish.jersey.tools.plugins.GenerateJerseyModuleListMojo.java

License:Open Source License

/**
 * Build the project-info link path by including all the artifactId up to (excluding) the root parent
 * @param project project for which the path should be determined.
 * @return path consisting of hierarchically nested maven artifact IDs. Used for referencing to the project-info on java.net
 *///  w  ww.  j a  v  a2s  . c  om
private String getLinkPath(MavenProject project) {
    String path = "";
    MavenProject parent = project.getParent();
    while (parent != null && !(parent.getArtifactId().equals("project")
            && parent.getGroupId().equals("org.glassfish.jersey"))) {
        path = parent.getArtifactId() + "/" + path;
        parent = parent.getParent();
    }
    return path;
}

From source file:org.guvnor.ala.build.maven.executor.MavenBuildExecConfigExecutor.java

License:Apache License

@Override
public Optional<BinaryConfig> apply(final MavenBuild mavenBuild,
        final MavenBuildExecConfig mavenBuildExecConfig) {

    final Project project = mavenBuild.getProject();

    final MavenProject mavenProject = build(project, mavenBuild.getGoals(), mavenBuild.getProperties());

    final Path path = FileSystems.getFileSystem(URI.create("file://default"))
            .getPath(project.getTempDir() + "/target/" + project.getExpectedBinary());

    final MavenBinary binary = new MavenProjectBinaryImpl(path, project, mavenProject.getGroupId(),
            mavenProject.getArtifactId(), mavenProject.getVersion());

    buildRegistry.registerBinary(binary);
    return Optional.of(binary);
}

From source file:org.guvnor.common.services.project.backend.server.ModuleRepositoryResolverImpl.java

License:Apache License

@Override
public Set<MavenRepositoryMetadata> getRepositoriesResolvingArtifact(final String pomXML,
        final MavenRepositoryMetadata... filter) {
    GAVPreferences gavPreferences = gavPreferencesProvider.get();
    gavPreferences.load();/*from  w w  w. j  ava  2s.c o m*/
    if (gavPreferences.isConflictingGAVCheckDisabled()) {
        return Collections.EMPTY_SET;
    }

    final InputStream pomStream = new ByteArrayInputStream(pomXML.getBytes(StandardCharsets.UTF_8));
    final MavenProject mavenProject = MavenProjectLoader.parseMavenPom(pomStream);
    final GAV gav = new GAV(mavenProject.getGroupId(), mavenProject.getArtifactId(), mavenProject.getVersion());

    final Set<MavenRepositoryMetadata> repositoriesResolvingArtifact = new HashSet<MavenRepositoryMetadata>();
    repositoriesResolvingArtifact.addAll(getRepositoriesResolvingArtifact(gav, mavenProject));

    //Filter results if necessary
    if (filter != null && filter.length > 0) {
        repositoriesResolvingArtifact.retainAll(Arrays.asList(filter));
    }

    return repositoriesResolvingArtifact;
}

From source file:org.guvnor.common.services.project.backend.server.ProjectRepositoryResolverImpl.java

License:Apache License

@Override
public Set<MavenRepositoryMetadata> getRepositoriesResolvingArtifact(final String pomXML,
        final MavenRepositoryMetadata... filter) {
    if (isCheckConflictingGAVDisabled) {
        return Collections.EMPTY_SET;
    }// w ww .ja va  2 s. c  om
    final InputStream pomStream = new ByteArrayInputStream(pomXML.getBytes(StandardCharsets.UTF_8));
    final MavenProject mavenProject = MavenProjectLoader.parseMavenPom(pomStream);
    final GAV gav = new GAV(mavenProject.getGroupId(), mavenProject.getArtifactId(), mavenProject.getVersion());

    final Set<MavenRepositoryMetadata> repositoriesResolvingArtifact = new HashSet<MavenRepositoryMetadata>();
    repositoriesResolvingArtifact.addAll(getRepositoriesResolvingArtifact(gav, mavenProject));

    //Filter results if necessary
    if (filter != null && filter.length > 0) {
        repositoriesResolvingArtifact.retainAll(Arrays.asList(filter));
    }

    return repositoriesResolvingArtifact;
}

From source file:org.guvnor.common.services.project.backend.server.RepositoryResolverTestUtils.java

License:Apache License

/**
 * Install a Maven Project to the local Maven Repository
 * @param mavenProject//from   ww  w . j av  a 2  s .c  om
 * @param pomXml
 */
public static void installArtifact(final MavenProject mavenProject, final String pomXml) {
    final ReleaseId releaseId = new ReleaseIdImpl(mavenProject.getGroupId(), mavenProject.getArtifactId(),
            mavenProject.getVersion());

    final Aether aether = new Aether(mavenProject);
    final MavenRepository mavenRepository = new MavenRepository(aether) {
        //Nothing to override, just a sub-class to expose Constructor
    };

    mavenRepository.deployArtifact(releaseId, "content".getBytes(), pomXml.getBytes());
}