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

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

Introduction

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

Prototype

public Artifact getArtifact() 

Source Link

Usage

From source file:org.codehaus.mojo.cobertura.InheritProject.java

License:Apache License

/**
 * Resolves the Artifact from the remote repository if nessessary. If no version is specified, it will
 * be retrieved from the dependency list or from the DependencyManagement section of the pom.
 *
 * @param project       This actual project, not the inherited one.
 * @param artifact      The artifact to be resolved; must not be null
 * @param transitive    True to resolve the artifact transitivly
 * @return              The resolved artifact; never null
 *
 * @throws MojoExecutionException   Failed to resolve artifact
 *//*from   w  w  w. j  a va2s  . co m*/
private Artifact resolveArtifact(final MavenProject project, final Artifact artifact, final boolean transitive)
        throws MojoExecutionException {
    assert artifact != null;

    try {
        if (transitive) {
            artifactResolver.resolveTransitively(Collections.singleton(artifact), project.getArtifact(),
                    project.getRemoteArtifactRepositories(), artifactRepository, artifactMetadataSource);
        } else {
            artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), artifactRepository);
        }
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to find artifact", e);
    }

    return artifact;
}

From source file:org.codehaus.mojo.gwt.AbstractGwtMojo.java

License:Apache License

private Collection<File> getJarFiles(String artifactId) throws MojoExecutionException {
    checkGwtUserVersion();//from   www  .j a  va2s  .c o  m
    Artifact rootArtifact = pluginArtifactMap.get(artifactId);

    ArtifactResolutionResult result;
    try {
        // Code shamelessly copied from exec-maven-plugin.
        MavenProject rootProject = this.projectBuilder.buildFromRepository(rootArtifact,
                this.remoteRepositories, this.localRepository);
        List<Dependency> dependencies = rootProject.getDependencies();
        Set<Artifact> dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies,
                null, null, null);
        dependencyArtifacts.add(rootProject.getArtifact());
        result = resolver.resolveTransitively(dependencyArtifacts, rootArtifact, Collections.EMPTY_MAP,
                localRepository, remoteRepositories, artifactMetadataSource, null, Collections.EMPTY_LIST);
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to resolve artifact", e);
    }

    Collection<Artifact> resolved = result.getArtifacts();
    Collection<File> files = new ArrayList<File>(resolved.size() + 1);
    files.add(rootArtifact.getFile());
    for (Artifact artifact : resolved) {
        files.add(artifact.getFile());
    }

    return files;
}

From source file:org.codehaus.mojo.license.AbstractAddThirdPartyMojo.java

License:Open Source License

protected void resolveUnsafeDependenciesFromFile(File missingLicenses) throws IOException {
    SortedSet<MavenProject> unsafeDeps = getUnsafeDependencies();
    if (missingLicenses != null && missingLicenses.exists() && missingLicenses.length() > 0) {
        // there are missing licenses available from the artifact
        SortedProperties unsafeMappings = new SortedProperties(getEncoding());

        if (missingLicenses.exists()) {
            // load the missing file
            unsafeMappings.load(missingLicenses);
        }/*w  ww  .  j a va2 s .co m*/

        Set resolvedDependencies = new HashSet();
        for (MavenProject unsafeDependency : unsafeDeps) {
            String id = MojoHelper.getArtifactId(unsafeDependency.getArtifact());

            if (unsafeMappings.containsKey(id) && StringUtils.isNotBlank(unsafeMappings.getProperty(id))) {
                // update license map
                thirdPartyTool.addLicense(licenseMap, unsafeDependency, unsafeMappings.getProperty(id));

                // remove
                resolvedDependencies.add(unsafeDependency);
            }
        }

        // remove resolvedDependencies from unsafeDeps;
        unsafeDeps.removeAll(resolvedDependencies);
    }
}

From source file:org.codehaus.mojo.license.AbstractAddThirdPartyMojo.java

License:Open Source License

protected boolean checkUnsafeDependencies() {
    SortedSet<MavenProject> unsafeDeps = getUnsafeDependencies();
    boolean unsafe = !CollectionUtils.isEmpty(unsafeDeps);
    if (unsafe) {
        Log log = getLog();/*w w w .j a  va2  s  . c  o m*/
        log.warn("There is " + unsafeDeps.size() + " dependencies with no license :");
        for (MavenProject dep : unsafeDeps) {

            // no license found for the dependency
            log.warn(" - " + MojoHelper.getArtifactId(dep.getArtifact()));
        }
    }
    return unsafe;
}

From source file:org.codehaus.mojo.license.AbstractDownloadLicensesMojo.java

License:Open Source License

/**
 * {@inheritDoc}//from ww  w  .j  a  v  a2 s .  co m
 */
public void execute() throws MojoExecutionException {

    if (offline) {

        getLog().warn("Offline flag is on, download-licenses goal is skip.");
        return;
    }
    if (isSkip()) {
        getLog().info("skip flag is on, will skip goal.");
        return;
    }

    initDirectories();

    initProxy();

    Map<String, ProjectLicenseInfo> configuredDepLicensesMap = new HashMap<String, ProjectLicenseInfo>();

    // License info from previous build
    if (licensesOutputFile.exists()) {
        loadLicenseInfo(configuredDepLicensesMap, licensesOutputFile, true);
    }

    // Manually configured license info, loaded second to override previously loaded info
    if (licensesConfigFile.exists()) {
        loadLicenseInfo(configuredDepLicensesMap, licensesConfigFile, false);
    }

    Set<MavenProject> dependencies = getDependencies();

    // The resulting list of licenses after dependency resolution
    List<ProjectLicenseInfo> depProjectLicenses = new ArrayList<ProjectLicenseInfo>();

    for (MavenProject project : dependencies) {
        Artifact artifact = project.getArtifact();
        getLog().debug("Checking licenses for project " + artifact);
        String artifactProjectId = getArtifactProjectId(artifact);
        ProjectLicenseInfo depProject;
        if (configuredDepLicensesMap.containsKey(artifactProjectId)) {
            depProject = configuredDepLicensesMap.get(artifactProjectId);
            depProject.setVersion(artifact.getVersion());
        } else {
            depProject = createDependencyProject(project);
        }
        downloadLicenses(depProject);
        depProjectLicenses.add(depProject);
    }

    try {
        LicenseSummaryWriter.writeLicenseSummary(depProjectLicenses, licensesOutputFile);
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to write license summary file: " + licensesOutputFile, e);
    }
}

From source file:org.codehaus.mojo.license.api.DefaultDependenciesTool.java

License:Open Source License

/**
 * {@inheritDoc}/*from w  ww  .  java  2s  .  c  o m*/
 */
public SortedMap<String, MavenProject> loadProjectDependencies(MavenProject project,
        MavenProjectDependenciesConfigurator configuration, ArtifactRepository localRepository,
        List<ArtifactRepository> remoteRepositories, SortedMap<String, MavenProject> cache) {

    boolean haveNoIncludedGroups = StringUtils.isEmpty(configuration.getIncludedGroups());
    boolean haveNoIncludedArtifacts = StringUtils.isEmpty(configuration.getIncludedArtifacts());

    boolean haveExcludedGroups = StringUtils.isNotEmpty(configuration.getExcludedGroups());
    boolean haveExcludedArtifacts = StringUtils.isNotEmpty(configuration.getExcludedArtifacts());
    boolean haveExclusions = haveExcludedGroups || haveExcludedArtifacts;

    Pattern includedGroupPattern = null;
    Pattern includedArtifactPattern = null;
    Pattern excludedGroupPattern = null;
    Pattern excludedArtifactPattern = null;

    if (!haveNoIncludedGroups) {
        includedGroupPattern = Pattern.compile(configuration.getIncludedGroups());
    }
    if (!haveNoIncludedArtifacts) {
        includedArtifactPattern = Pattern.compile(configuration.getIncludedArtifacts());
    }
    if (haveExcludedGroups) {
        excludedGroupPattern = Pattern.compile(configuration.getExcludedGroups());
    }
    if (haveExcludedArtifacts) {
        excludedArtifactPattern = Pattern.compile(configuration.getExcludedArtifacts());
    }

    Set<?> depArtifacts;

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

    List<String> includedScopes = configuration.getIncludedScopes();
    List<String> excludeScopes = configuration.getExcludedScopes();

    boolean verbose = configuration.isVerbose();

    SortedMap<String, MavenProject> result = new TreeMap<String, MavenProject>();

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

        if (DefaultThirdPartyTool.LICENSE_DB_TYPE.equals(artifact.getType())) {
            // the special dependencies for license databases don't count.
            // Note that this will still see transitive deps of a license db; so using the build helper inside of another project
            // to make them will be noisy.
            continue;
        }

        String scope = artifact.getScope();
        if (CollectionUtils.isNotEmpty(includedScopes) && !includedScopes.contains(scope)) {

            // not in included scopes
            continue;
        }

        if (excludeScopes.contains(scope)) {

            // in excluded scopes
            continue;
        }

        Logger log = getLogger();

        String id = MojoHelper.getArtifactId(artifact);

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

        // Check if the project should be included
        // If there is no specified artifacts and group to include, include all
        boolean isToInclude = haveNoIncludedArtifacts && haveNoIncludedGroups
                || isIncludable(artifact, includedGroupPattern, includedArtifactPattern);

        // Check if the project should be excluded
        boolean isToExclude = isToInclude && haveExclusions
                && isExcludable(artifact, excludedGroupPattern, excludedArtifactPattern);

        if (!isToInclude || isToExclude) {
            if (verbose) {
                log.info("skip artifact " + id);
            }
            continue;
        }

        MavenProject depMavenProject = null;

        if (cache != null) {

            // try to get project from cache
            depMavenProject = cache.get(id);
        }

        if (depMavenProject != null) {
            if (verbose) {
                log.info("add dependency [" + id + "] (from cache)");
            }
        } else {
            // build project

            try {
                depMavenProject = mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories,
                        localRepository, true);
                depMavenProject.getArtifact().setScope(artifact.getScope());
            } catch (ProjectBuildingException e) {
                log.warn("Unable to obtain POM for artifact : " + artifact, e);
                continue;
            }

            if (verbose) {
                log.info("add dependency [" + id + "]");
            }
            if (cache != null) {

                // store it also in cache
                cache.put(id, depMavenProject);
            }
        }

        // keep the project
        result.put(id, depMavenProject);
    }
    return result;
}

From source file:org.codehaus.mojo.license.api.DefaultDependenciesTool.java

License:Open Source License

/**
 * {@inheritDoc}/*from w w w. j  a  v  a 2 s.c o  m*/
 */
public void loadProjectArtifacts(ArtifactRepository localRepository, List remoteRepositories,
        MavenProject project) throws DependenciesToolException

{

    if (CollectionUtils.isEmpty(project.getDependencyArtifacts())) {

        Set dependenciesArtifacts;
        try {
            dependenciesArtifacts = MavenMetadataSource.createArtifacts(artifactFactory,
                    project.getDependencies(), null, null, project);
        } catch (InvalidDependencyVersionException e) {
            throw new DependenciesToolException(e);
        }
        project.setDependencyArtifacts(dependenciesArtifacts);

        Artifact artifact = project.getArtifact();

        ArtifactResolutionResult result;
        try {
            result = artifactResolver.resolveTransitively(dependenciesArtifacts, artifact, remoteRepositories,
                    localRepository, artifactMetadataSource);
        } catch (ArtifactResolutionException e) {
            throw new DependenciesToolException(e);
        } catch (ArtifactNotFoundException e) {
            throw new DependenciesToolException(e);
        }

        project.setArtifacts(result.getArtifacts());

    }

}

From source file:org.codehaus.mojo.license.api.DefaultThirdPartyTool.java

License:Open Source License

/**
 * {@inheritDoc}//from   w w  w .  j a  v a  2  s. c o  m
 */
public SortedSet<MavenProject> getProjectsWithNoLicense(LicenseMap licenseMap, boolean doLog) {

    Logger log = getLogger();

    // get unsafe dependencies (says with no license)
    SortedSet<MavenProject> unsafeDependencies = licenseMap.get(LicenseMap.UNKNOWN_LICENSE_MESSAGE);

    if (doLog) {
        if (CollectionUtils.isEmpty(unsafeDependencies)) {
            log.debug("There is no dependency with no license from poms.");
        } else {
            log.debug("There is " + unsafeDependencies.size() + " dependencies with no license from poms : ");
            for (MavenProject dep : unsafeDependencies) {

                // no license found for the dependency
                log.debug(" - " + MojoHelper.getArtifactId(dep.getArtifact()));
            }
        }
    }

    return unsafeDependencies;
}

From source file:org.codehaus.mojo.license.api.DefaultThirdPartyTool.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  ww  . j  a v a  2 s .c o  m*/
 */
public SortedProperties loadThirdPartyDescriptorsForUnsafeMapping(Set<Artifact> topLevelDependencies,
        String encoding, Collection<MavenProject> projects, SortedSet<MavenProject> unsafeDependencies,
        LicenseMap licenseMap, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
        throws ThirdPartyToolException, IOException {

    SortedProperties result = new SortedProperties(encoding);
    Map<String, MavenProject> unsafeProjects = new HashMap<String, MavenProject>();
    for (MavenProject unsafeDependency : unsafeDependencies) {
        String id = MojoHelper.getArtifactId(unsafeDependency.getArtifact());
        unsafeProjects.put(id, unsafeDependency);
    }

    for (MavenProject mavenProject : projects) {

        if (CollectionUtils.isEmpty(unsafeDependencies)) {

            // no more unsafe dependencies to find
            break;
        }

        File thirdPartyDescriptor = resolvThirdPartyDescriptor(mavenProject, localRepository,
                remoteRepositories);

        if (thirdPartyDescriptor != null && thirdPartyDescriptor.exists()
                && thirdPartyDescriptor.length() > 0) {

            if (getLogger().isInfoEnabled()) {
                getLogger().info("Detects third party descriptor " + thirdPartyDescriptor);
            }

            // there is a third party file detected form the given dependency
            SortedProperties unsafeMappings = new SortedProperties(encoding);

            if (thirdPartyDescriptor.exists()) {

                getLogger().info("Load missing file " + thirdPartyDescriptor);

                // load the missing file
                unsafeMappings.load(thirdPartyDescriptor);
            }
            resolveUnsafe(unsafeDependencies, licenseMap, unsafeProjects, unsafeMappings, result);
        }

    }
    try {
        loadGlobalLicenses(topLevelDependencies, localRepository, remoteRepositories, unsafeDependencies,
                licenseMap, unsafeProjects, result);
    } catch (ArtifactNotFoundException e) {
        throw new ThirdPartyToolException("Failed to load global licenses", e);
    } catch (ArtifactResolutionException e) {
        throw new ThirdPartyToolException("Failed to load global licenses", e);
    }
    return result;
}

From source file:org.codehaus.mojo.license.api.DefaultThirdPartyTool.java

License:Open Source License

/**
 * {@inheritDoc}/*  ww w  .  java 2s  .c  om*/
 */
public void addLicense(LicenseMap licenseMap, MavenProject project, List<?> licenses) {

    if (Artifact.SCOPE_SYSTEM.equals(project.getArtifact().getScope())) {

        // do NOT treat system dependency
        return;
    }

    if (CollectionUtils.isEmpty(licenses)) {

        // no license found for the dependency
        licenseMap.put(LicenseMap.UNKNOWN_LICENSE_MESSAGE, project);
        return;
    }

    for (Object o : licenses) {
        String id = MojoHelper.getArtifactId(project.getArtifact());
        if (o == null) {
            getLogger().warn("could not acquire the license for " + id);
            continue;
        }
        License license = (License) o;
        String licenseKey = license.getName();

        // tchemit 2010-08-29 Ano #816 Check if the License object is well formed

        if (StringUtils.isEmpty(license.getName())) {
            getLogger().warn("No license name defined for " + id);
            licenseKey = license.getUrl();
        }

        if (StringUtils.isEmpty(licenseKey)) {
            getLogger().warn("No license url defined for " + id);
            licenseKey = LicenseMap.UNKNOWN_LICENSE_MESSAGE;
        }
        licenseMap.put(licenseKey, project);
    }
}