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.license.api.DefaultThirdPartyTool.java

License:Open Source License

/**
 * {@inheritDoc}//w  w w .j av  a2  s .co m
 */
public SortedProperties loadUnsafeMapping(LicenseMap licenseMap, SortedMap<String, MavenProject> artifactCache,
        String encoding, File missingFile) throws IOException {
    Map<String, MavenProject> snapshots = new HashMap<String, MavenProject>();

    //find snapshot dependencies
    for (Map.Entry<String, MavenProject> entry : artifactCache.entrySet()) {
        MavenProject mavenProject = entry.getValue();
        if (mavenProject.getVersion().endsWith(Artifact.SNAPSHOT_VERSION)) {
            snapshots.put(entry.getKey(), mavenProject);
        }
    }

    for (Map.Entry<String, MavenProject> snapshot : snapshots.entrySet()) {
        // remove invalid entries, which contain timestamps in key
        artifactCache.remove(snapshot.getKey());
        // put corrected keys/entries into artifact cache
        MavenProject mavenProject = snapshot.getValue();

        String id = MojoHelper.getArtifactId(mavenProject.getArtifact());
        artifactCache.put(id, mavenProject);

    }
    SortedSet<MavenProject> unsafeDependencies = getProjectsWithNoLicense(licenseMap, false);

    SortedProperties unsafeMappings = new SortedProperties(encoding);

    if (missingFile.exists()) {
        // there is some unsafe dependencies

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

        // load the missing file
        unsafeMappings.load(missingFile);
    }

    // get from the missing file, all unknown dependencies
    List<String> unknownDependenciesId = new ArrayList<String>();

    // coming from maven-license-plugin, we used the full g/a/v/c/t. Now we remove classifier and type
    // since GAV is good enough to qualify a license of any artifact of it...
    Map<String, String> migrateKeys = migrateMissingFileKeys(unsafeMappings.keySet());

    for (Object o : migrateKeys.keySet()) {
        String id = (String) o;
        String migratedId = migrateKeys.get(id);

        MavenProject project = artifactCache.get(migratedId);
        if (project == null) {
            // now we are sure this is a unknown dependency
            unknownDependenciesId.add(id);
        } else {
            if (!id.equals(migratedId)) {

                // migrates id to migratedId
                getLogger().info("Migrates [" + id + "] to [" + migratedId + "] in the missing file.");
                Object value = unsafeMappings.get(id);
                unsafeMappings.remove(id);
                unsafeMappings.put(migratedId, value);
            }
        }
    }

    if (!unknownDependenciesId.isEmpty()) {

        // there is some unknown dependencies in the missing file, remove them
        for (String id : unknownDependenciesId) {
            getLogger().warn(
                    "dependency [" + id + "] does not exist in project, remove it from the missing file.");
            unsafeMappings.remove(id);
        }

        unknownDependenciesId.clear();
    }

    // push back loaded dependencies
    for (Object o : unsafeMappings.keySet()) {
        String id = (String) o;

        MavenProject project = artifactCache.get(id);
        if (project == null) {
            getLogger().warn("dependency [" + id + "] does not exist in project.");
            continue;
        }

        String license = (String) unsafeMappings.get(id);

        String[] licenses = StringUtils.split(license, '|');

        if (ArrayUtils.isEmpty(licenses)) {

            // empty license means not fill, skip it
            continue;
        }

        // add license in map
        addLicense(licenseMap, project, licenses);

        // remove unknown license
        unsafeDependencies.remove(project);
    }

    if (unsafeDependencies.isEmpty()) {

        // no more unknown license in map
        licenseMap.remove(LicenseMap.UNKNOWN_LICENSE_MESSAGE);
    } else {

        // add a "with no value license" for missing dependencies
        for (MavenProject project : unsafeDependencies) {
            String id = MojoHelper.getArtifactId(project.getArtifact());
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("dependency [" + id + "] has no license, add it in the missing file.");
            }
            unsafeMappings.setProperty(id, "");
        }
    }
    return unsafeMappings;
}

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

License:Open Source License

public static Comparator<MavenProject> getProjectComparator() {
    if (projectComparator == null) {
        projectComparator = new Comparator<MavenProject>() {
            public int compare(MavenProject o1, MavenProject o2) {

                String id1 = getArtifactId(o1.getArtifact());
                String id2 = getArtifactId(o2.getArtifact());
                return id1.compareTo(id2);
            }//from   w  w w. j av a2  s. co m
        };
    }
    return projectComparator;
}

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

License:Educational Community License

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

    Logger log = getLogger();

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

    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.DefaultThirdPartyTool.java

License:Educational Community License

/**
 * {@inheritDoc}//from   w  w  w . j  ava 2 s .  co  m
 */
@Override
public SortedProperties loadThirdPartyDescriptorsForUnsafeMapping(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().debug("Load missing file " + thirdPartyDescriptor);

                // load the missing file
                unsafeMappings.load(thirdPartyDescriptor);
            }

            for (String id : unsafeProjects.keySet()) {

                if (unsafeMappings.containsKey(id)) {

                    String license = (String) unsafeMappings.get(id);
                    if (StringUtils.isEmpty(license)) {

                        // empty license means not fill, skip it
                        continue;
                    }

                    // found a resolved unsafe dependency in the missing third party file
                    MavenProject resolvedProject = unsafeProjects.get(id);
                    unsafeDependencies.remove(resolvedProject);

                    // push back to
                    result.put(id, license.trim());

                    addLicense(licenseMap, resolvedProject, license);
                }
            }
        }
    }
    return result;
}

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

License:Educational Community License

/**
 * This does some cleanup, and basic safety checks on the license objects passed in<br>
 * 1 - Ignore system scoped dependencies<br>
 * 2 - If there are no licenses declared, use "Unknown License"<br>
 * 3 - If there is no name declared, but there is a url, use the url as a key<br>
 * 4 - If there is no name or url, use "Unknown License" as a key<br>
 *///from w ww .j a  v a 2  s .  c o m
@Override
public void addLicense(LicenseMap licenseMap, MavenProject project, List<?> licenses) {

    if (Artifact.SCOPE_SYSTEM.equals(project.getArtifact().getScope())) {
        // Do not deal with system scope dependencies
        return;
    }

    if (CollectionUtils.isEmpty(licenses)) {
        // No natively declared license was expressed in the pom for this dependency
        // Really funky use of "put"
        // This adds the dependency to the list of dependencies with an unknown license
        licenseMap.put(LicenseMap.getUnknownLicenseMessage(), project);
        return;
    }

    for (Object o : licenses) {
        String id = MojoHelper.getArtifactId(project.getArtifact());
        if (o == null) {
            getLogger().warn("could not acquire a 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().debug("No license name defined for " + id);
            licenseKey = license.getUrl();
        }

        if (StringUtils.isEmpty(licenseKey)) {
            getLogger().debug("No license url defined for " + id);
            licenseKey = LicenseMap.getUnknownLicenseMessage();
        }

        // Really funky use of "put"
        // This adds the dependency to the list of dependencies with this license
        licenseMap.put(licenseKey, project);
    }
}

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

private Artifact resolveArtifact(MavenProject project) {
    Artifact artifact = project.getArtifact();
    String key = ArtifactUtils.versionlessKey(artifact);
    artifact = (Artifact) this.project.getArtifactMap().get(key);
    return artifact;
}

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

License:Open Source License

public void addLicense(MavenProject project, List<?> licenses) {

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

        // do NOT treate system dependency
        return;//from www.  j a  v a  2s .  c  om
    }

    if (CollectionUtils.isEmpty(licenses)) {

        // no license found for the dependency
        put(getUnknownLicenseMessage(), project);
        return;
    }

    for (Object o : licenses) {
        String id;
        id = ArtifactHelper.getArtifactId(project.getArtifact());
        if (o == null) {
            getLog().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())) {
            getLog().warn("No license name defined for " + id);
            licenseKey = license.getUrl();
        }

        if (StringUtils.isEmpty(licenseKey)) {
            getLog().warn("No license url defined for " + id);
            licenseKey = getUnknownLicenseMessage();
        }
        put(licenseKey, project);
    }
}

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

License:Open Source License

public SortedSet<MavenProject> getUnsafeDependencies() {

    Log log = getLog();//from w  w  w  .  j  a va  2  s .  c  o m
    // get unsafe dependencies (says with no license)
    SortedSet<MavenProject> unsafeDependencies = get(getUnknownLicenseMessage());

    if (log.isDebugEnabled()) {
        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(" - " + ArtifactHelper.getArtifactId(dep.getArtifact()));
            }
        }
    }

    return unsafeDependencies;
}

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

License:Open Source License

protected SortedProperties loadUnsafeMapping(SortedMap<String, MavenProject> artifactCache, String encoding,
        File missingFile) throws IOException, ProjectBuildingException {

    SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies();

    SortedProperties unsafeMappings = new SortedProperties(encoding);

    // there is some unsafe dependencies
    if (missingFile.exists()) {

        getLog().info("Load missing file " + missingFile);

        // load the missing file
        unsafeMappings.load(missingFile);
    }/* w w  w  .  ja  v a 2s .  co m*/

    // get from the missing file, all unknown dependencies
    List<String> unknownDependenciesId = new ArrayList<String>();

    // migrate unsafe mapping (before version 3.0 we do not have the type of
    // dependency in the missing file, now we must deal with it, so check it

    List<String> migrateId = new ArrayList<String>();
    //        SortedMap<String, MavenProject> artifactCache = AbstractAddThirdPartyMojo.getArtifactCache();
    for (Object o : unsafeMappings.keySet()) {
        String id = (String) o;
        MavenProject project = artifactCache.get(id);
        if (project == null) {
            // try with the --jar type
            project = artifactCache.get(id + "--jar");
            if (project == null) {

                // now we are sure this is a unknown dependency
                unknownDependenciesId.add(id);
            } else {

                // this dependency must be migrated
                migrateId.add(id);
            }
        }
    }

    if (!unknownDependenciesId.isEmpty()) {

        // there is some unknown dependencies in the missing file, remove them
        for (String id : unknownDependenciesId) {
            getLog().warn(
                    "dependency [" + id + "] does not exists in project, remove it from the missing file.");
            unsafeMappings.remove(id);
        }

        unknownDependenciesId.clear();
    }

    if (!migrateId.isEmpty()) {

        // there is some dependencies to migrate in the missing file
        for (String id : migrateId) {
            String newId = id + "--jar";
            getLog().info("Migrate " + id + " to " + newId + " in the missing file.");
            Object value = unsafeMappings.get(id);
            unsafeMappings.remove(id);
            unsafeMappings.put(newId, value);
        }

        migrateId.clear();
    }

    // push back loaded dependencies
    for (Object o : unsafeMappings.keySet()) {
        String id = (String) o;

        MavenProject project = artifactCache.get(id);
        if (project == null) {
            getLog().warn("dependency [" + id + "] does not exists in project.");
            continue;
        }

        String license = (String) unsafeMappings.get(id);
        if (StringUtils.isEmpty(license)) {

            // empty license means not fill, skip it
            continue;
        }

        // add license in map
        License l = new License();
        l.setName(license.trim());
        l.setUrl(license.trim());

        // add license
        addLicense(project, Arrays.asList(l));

        // remove unknown license
        unsafeDependencies.remove(project);
    }

    if (unsafeDependencies.isEmpty()) {

        // no more unknown license in map
        remove(getUnknownLicenseMessage());
    } else {

        // add a "with no value license" for missing dependencies
        for (MavenProject project : unsafeDependencies) {
            String id = ArtifactHelper.getArtifactId(project.getArtifact());
            unsafeMappings.setProperty(id, "");
        }
    }
    return unsafeMappings;
}

From source file:org.codehaus.mojo.license.utils.MojoHelper.java

License:Open Source License

public static Comparator<MavenProject> newMavenProjectComparator() {
    return new Comparator<MavenProject>() {
        /**/*from   www. j a  v  a2  s. c o m*/
         * {@inheritDoc}
         */
        public int compare(MavenProject o1, MavenProject o2) {

            String id1 = getArtifactId(o1.getArtifact());
            String id2 = getArtifactId(o2.getArtifact());
            return id1.compareTo(id2);
        }
    };

}