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

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

Introduction

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

Prototype

@Deprecated
public Set<Artifact> getDependencyArtifacts() 

Source Link

Document

Direct dependencies that this project has.

Usage

From source file:org.wildfly.maven.plugins.licenses.DependenciesResolver.java

public <R> SortedMap<String, R> loadDependenciesAndConvertThem(MavenProject project,
        MavenProjectDependenciesConfiguration configuration, ArtifactRepository localRepository,
        List<ArtifactRepository> remoteRepositories, SortedMap<String, MavenProject> cache,
        Function<MavenProject, R> convertFunction) {

    Pattern includedGroupPattern = (isEmpty(configuration.includedGroups) ? null
            : Pattern.compile(configuration.includedGroups));
    Pattern includedArtifactPattern = (isEmpty(configuration.includedArtifacts) ? null
            : Pattern.compile(configuration.includedArtifacts));

    Pattern excludedGroupPattern = (isEmpty(configuration.excludedGroups) ? null
            : Pattern.compile(configuration.excludedGroups));
    Pattern excludedArtifactPattern = (isEmpty(configuration.excludedArtifacts) ? null
            : Pattern.compile(configuration.excludedArtifacts));

    boolean matchInclusions = includedGroupPattern != null || includedArtifactPattern != null;
    boolean matchExclusions = excludedGroupPattern != null || excludedArtifactPattern != null;

    Set<?> depArtifacts;/*w  ww  .  j  a va2  s .com*/

    if (configuration.includeTransitiveDependencies) {
        // All project dependencies
        depArtifacts = project.getArtifacts();
    } else {
        // Only direct project dependencies
        depArtifacts = project.getDependencyArtifacts();
    }

    List<String> includedScopes = configuration.includedScopes;
    List<String> excludeScopes = configuration.excludedScopes;

    boolean verbose = configuration.verbose;

    SortedMap<String, R> result = new TreeMap<String, R>();
    Logger log = getLogger();

    for (Object o : depArtifacts) {
        Artifact artifact = (Artifact) o;
        String id = getArtifactId(artifact);

        if (verbose) {
            log.info("detected artifact " + id);
        }

        String scope = artifact.getScope();
        if (isNotEmptyCollection(includedScopes) && !includedScopes.contains(scope)) {
            if (verbose) {
                log.info("skip artifact " + id + " - not in included scopes");
            }
            continue;
        }

        if (excludeScopes.contains(scope)) {
            if (verbose) {
                log.info("skip artifact " + id + " - in excluded scope " + scope);
            }
            continue;
        }

        // Check if the project should be included
        boolean isToInclude = matchesIncluded(artifact, includedGroupPattern, includedArtifactPattern);
        if (!isToInclude) {
            if (verbose) {
                log.info("skip artifact " + id + " - not in included artifactId / groupId patterns");
            }
            continue;
        }
        // Check if the project should be excluded
        boolean isToExclude = matchesExcluded(artifact, excludedGroupPattern, excludedArtifactPattern);

        if (isToExclude) {
            if (verbose) {
                log.info("skip artifact " + id + " - in excluded artifactId / groupId patterns");
            }
            continue;
        }

        MavenProject depMavenProject = getDependencyMavenProject(localRepository, remoteRepositories, cache,
                verbose, artifact, log, id);
        if (depMavenProject != null) {
            // keep the project
            result.put(id, convertFunction.apply(depMavenProject));
        }
    }
    return result;
}

From source file:org.wisdom.maven.osgi.ProjectDependencies.java

License:Apache License

/**
 * Creates the project dependencies instance from a Maven Project.
 *
 * @param project the maven project//www .j av a2s.com
 */
public ProjectDependencies(MavenProject project) {
    this(project.getDependencyArtifacts(), project.getArtifacts());
}

From source file:org.wso2.maven.p2.utils.ProductFileUtils.java

License:Open Source License

private static String getDependencyVersion(MavenProject mavenProject, String dependency)
        throws MojoExecutionException {
    if (mavenProject == null) {
        throw new MojoExecutionException("Unable to read maven project for finding dependencies.");
    } else if (dependency == null || dependency.isEmpty()) {
        throw new MojoExecutionException("Unable to find version for invalid dependency value.");
    } else {//w w w .  j ava2s  .  c  o  m
        Artifact selectedArtifact = mavenProject.getDependencyArtifacts().stream()
                .filter(artifact -> artifact.getArtifactId().contains(dependency)).findFirst()
                .orElseThrow(() -> new MojoExecutionException("Unable to find version as there seems to be "
                        + "no pom based configuration for the provided dependency."));

        String dependencyVersion = selectedArtifact.getBaseVersion();
        if (dependencyVersion == null || dependencyVersion.isEmpty()) {
            throw new MojoExecutionException("Unable to find version for the provided dependency.");
        }
        return dependencyVersion;
    }
}

From source file:se.jguru.nazgul.tools.codestyle.enforcer.rules.RestrictImplDependencies.java

License:Apache License

/**
 * Delegate method, implemented by concrete subclasses.
 *
 * @param project The active MavenProject.
 * @param helper  The EnforcerRuleHelper instance, from which the MavenProject has been retrieved.
 * @throws RuleFailureException If the enforcer rule was not satisfied.
 *///from w ww  .j  av  a  2s .  co m
@SuppressWarnings({ "unchecked" })
@Override
protected void performValidation(final MavenProject project, final EnforcerRuleHelper helper)
        throws RuleFailureException {

    // Acquire the ProjectType
    final ProjectType projectType;
    try {
        projectType = ProjectType.getProjectType(project);
    } catch (IllegalArgumentException e) {
        throw new RuleFailureException("Incorrect project definition for " + project, e);
    }

    // Don't evaluate for ignored project types.
    if (IGNORED_PROJECT_TYPES.contains(projectType)) {
        return;
    }

    // Don't evaluate if told not to.
    if (matches(project.getGroupId(), dontEvaluateGroupIds)) {

        // Log somewhat
        helper.getLog().debug("Ignored [" + project.getGroupId() + ":" + project.getArtifactId()
                + "] since its groupId was excluded from enforcement.");
        return;

    }

    // Don't evaluate if not told to.
    if (!matches(project.getGroupId(), evaluateGroupIds)) {

        // Log somewhat
        helper.getLog().debug("Ignored [" + project.getGroupId() + ":" + project.getArtifactId()
                + "] since its groupId was not included in enforcement.");
        return;
    }

    // Acquire all project dependencies.
    for (final Artifact current : ((Set<Artifact>) project.getDependencyArtifacts())) {

        // Don't evaluate for test-scope dependencies.
        if (Artifact.SCOPE_TEST.equalsIgnoreCase(current.getScope())) {
            continue;
        }

        // Should this Artifact be evaluated?
        final boolean isIncludedInEvaluation = matches(current.getGroupId(), evaluateGroupIds);
        final boolean isNotExplicitlyExcludedFromEvaluation = !matches(current.getGroupId(),
                dontEvaluateGroupIds);
        if (isIncludedInEvaluation && isNotExplicitlyExcludedFromEvaluation) {

            final ProjectType artifactProjectType = ProjectType.getProjectType(current);
            final String prefix = "Don't use " + artifactProjectType + " dependencies ";

            if (artifactProjectType == ProjectType.IMPLEMENTATION) {
                throw new RuleFailureException(prefix + "outside of application projects.", current);
            }

            if (artifactProjectType == ProjectType.TEST) {
                throw new RuleFailureException(prefix + "in compile scope for non-test artifacts.", current);
            }

            if (artifactProjectType == ProjectType.JEE_APPLICATION
                    || artifactProjectType == ProjectType.PROOF_OF_CONCEPT) {
                throw new RuleFailureException(prefix + "in bundles.", current);
            }
        }
    }
}

From source file:se.natusoft.tools.codelicmgr.MojoUtils.java

License:Open Source License

/**
 * Updates third party license information from maven project information.
 *
 * @param thirdpartyLicenses The third party license configuration to update.
 * @param mavenProject The running maven project.
 * @param localRepository The artifact repository for the current build.
 * @param log To log to.//from   w  ww .j  a  va2  s. com
 * @return The passed configuration.
 */
public static void updateThirdpartyLicenseConfigFromMavenProject(ThirdpartyLicensesConfig thirdpartyLicenses,
        MavenProject mavenProject, ArtifactRepository localRepository, Log log) {

    Set<Artifact> dependencies = mavenProject.getDependencyArtifacts();
    if (dependencies != null) {
        for (Artifact depArtifact : dependencies) {
            if (!depArtifact.getType().equals("pom") &&
            // Avoid test scope dependencies
                    !depArtifact.getScope().equals(Artifact.SCOPE_TEST) &&
                    // Avoid internal dependencies.
                    !depArtifact.getGroupId().equals(mavenProject.getArtifact().getGroupId())) {
                try {

                    PomExtractor depPom = new PomExtractor(localRepository, depArtifact);

                    ProductConfig newProdConfig = new ProductConfig();
                    newProdConfig.setName(depArtifact.getArtifactId());
                    newProdConfig.setVersion(depArtifact.getVersion());
                    newProdConfig.setWeb(depPom.getProductUrl());

                    if (depPom.getLicenseName() != null) {
                        String licName = getLicenseName(depPom.getLicenseName());
                        String licVer = getLicenseVersion(depPom.getLicenseName());

                        // Lets try the name we extracted first
                        ThirdpartyLicenseConfig tplConfig = lookupThirdpartyLicense(thirdpartyLicenses,
                                licName);

                        // That failed, try to construct an acronym of the name instead.
                        if (tplConfig == null) {
                            String altLicName = getLicenseNameAcronym(depPom.getLicenseName());
                            tplConfig = lookupThirdpartyLicense(thirdpartyLicenses, altLicName);
                            if (tplConfig != null) {
                                licName = altLicName;
                            } else {// Not among the already known third party licenses, look in license library next.
                                try {
                                    LibraryLicense libLic = LicenseLibrary.getLicense(altLicName, licVer, "");
                                    if (libLic != null && !libLic.isDownloadable()) {
                                        String type = libLic.getType();
                                        if (type != null) {
                                            licName = type;
                                        }
                                    }
                                } catch (CodeLicenseException cle) {
                                }
                            }
                        }

                        // That also failed, now we try to remove all spaces from the name.
                        if (tplConfig == null) {
                            String altLicName = getLicenseName(depPom.getLicenseName()).replace(" ", "");
                            tplConfig = lookupThirdpartyLicense(thirdpartyLicenses, altLicName);
                            if (tplConfig != null) {
                                licName = altLicName;
                            } else {// Not among the already known third party licenses, look in license library next.
                                try {
                                    LibraryLicense libLic = LicenseLibrary.getLicense(altLicName, licVer, "");
                                    if (libLic != null && !libLic.isDownloadable()) {
                                        String type = libLic.getType();
                                        if (type != null) {
                                            licName = type;
                                        }
                                    }
                                } catch (CodeLicenseException cle) {
                                }
                            }
                        }
                        // Still no go! Just create a new config and use what we have. Please note that if the
                        // license was not found among the already known third party licenses, but found in a
                        // license library the license name have been modified to the official name in the license
                        // library.
                        if (tplConfig == null) {
                            tplConfig = new ThirdpartyLicenseConfig();
                            tplConfig.setType(licName);
                            tplConfig.setVersion(licVer);
                            tplConfig.setLicenseUrl(depPom.getLicenseUrl());
                            thirdpartyLicenses.addLicense(tplConfig);
                        }

                        // Check if current artifact already exists
                        boolean artifactExists = false;
                        for (ProductConfig prodConfig : tplConfig.getProducts().getProducts()) {
                            if (prodConfig.getName().trim().toLowerCase()
                                    .equals(newProdConfig.getName().trim().toLowerCase())) {
                                artifactExists = true;
                            }
                        }

                        // If not add it.
                        if (!artifactExists) {
                            tplConfig.getProducts().addProduct(newProdConfig);
                        }
                    } else {
                        if (lookupArtifactInConfiguredThirdpartyProduct(thirdpartyLicenses,
                                depArtifact) == null) {
                            log.warn("WARNING: Artifact '" + depArtifact
                                    + "' has no license information and has not been configured "
                                    + "under the <thirdpartyLicenses><license><products> section!");
                        }
                    }

                } catch (IOException ioe) {
                    log.warn("WARNING: Failed to extract information from maven dependency: "
                            + depArtifact.getArtifactId() + "-" + depArtifact.getVersion() + " ["
                            + ioe.getMessage() + "]");
                }
            }
        }
    }
}