Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.project.artifact;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 

Author(s):
Jason van Zyl
 
 @Component( role = ArtifactMetadataSource.class, hint = "maven" )
 public class MavenMetadataSource
     implements ArtifactMetadataSource
 {
     @Requirement
 
     @Requirement
     private ArtifactFactory repositorySystem;
 
     //TODO: This prevents a cycle in the composition which shows us another problem we need to deal with.
     //@Requirement
     private ProjectBuilder projectBuilder;
 
     @Requirement
    private PlexusContainer container;
    private Logger logger;
    private MavenMetadataCache cache;
    private LegacySupport legacySupport;
    private void injectSessionMetadataResolutionRequest request )
    {
        if ( session != null )
        {
            request.setOfflinesession.isOffline() );
            request.setForceUpdate..equalssession.getUpdatePolicy() ) );
        }
    }
    public ResolutionGroup retrieveArtifact artifactArtifactRepository localRepository,
                                     List<ArtifactRepositoryremoteRepositories )
    {
        return retrieveartifactlocalRepositoryremoteRepositoriesfalse );
    }
    public ResolutionGroup retrieveArtifact artifactArtifactRepository localRepository,
                                     List<ArtifactRepositoryremoteRepositoriesboolean resolveManagedVersions )
    {
        injectSessionrequest );
        request.setArtifactartifact );
        request.setLocalRepositorylocalRepository );
        request.setRemoteRepositoriesremoteRepositories );
        request.setResolveManagedVersionsresolveManagedVersions );
        return retrieverequest );
    }
    {
        Artifact artifact = request.getArtifact();
        //
        // If we have a system scoped artifact then we do not want any searching in local or remote repositories
        // and we want artifact resolution to only return the system scoped artifact itself.
        //
        if ( artifact.getScope() != null && artifact.getScope().equals. ) )
        {
            return new ResolutionGroupnullnullnull );
        }
        ResolutionGroup cached =
            .getartifactrequest.isResolveManagedVersions(), request.getLocalRepository(),
                       request.getRemoteRepositories() );
        if ( cached != null )
        {
            // if the POM has no file, we cached a missing artifact, only return the cached data if no update forced
            if ( !request.isForceUpdate() || hasFilecached.getPomArtifact() ) )
            {
                return cached;
            }
        }
        List<Dependencydependencies;
        List<DependencymanagedDependencies = null;
        List<ArtifactRepositorypomRepositories = null;
        Artifact pomArtifact;
        Artifact relocatedArtifact = null;
        //TODO: Not even sure this is really required as the project will be cached in the builder, we'll see this
        // is currently the biggest hotspot
        if ( artifact instanceof ArtifactWithDependencies )
        {
            pomArtifact = artifact;
            dependencies = ( (ArtifactWithDependenciesartifact ).getDependencies();
            managedDependencies = ( (ArtifactWithDependenciesartifact ).getManagedDependencies();
        }
        else
        {
            ProjectRelocation rel = retrieveRelocatedProjectartifactrequest );
            if ( rel == null )
            {
                return null;
            }
            pomArtifact = rel.pomArtifact;
            relocatedArtifact = rel.relocatedArtifact;
            if ( rel.project == null )
            {
                // When this happens we have a Maven 1.x POM, or some invalid POM. 
                // It should have never found its way into Maven 2.x repository but it did.
                dependencies = Collections.emptyList();
            }
            else
            {
                dependencies = rel.project.getDependencies();
                DependencyManagement depMngt = rel.project.getDependencyManagement();
                managedDependencies = ( depMngt != null ) ? depMngt.getDependencies() : null;
                pomRepositories = rel.project.getRemoteArtifactRepositories();
            }
        }
        Set<Artifactartifacts = Collections.<Artifact>emptySet();
        if ( !artifact.getArtifactHandler().isIncludesDependencies() )
        {
            artifacts = new LinkedHashSet<Artifact>();
            for ( Dependency dependency : dependencies )
            {
                Artifact dependencyArtifact = createDependencyArtifactdependencyartifactpomArtifact );
                if ( dependencyArtifact != null )
                {
                    artifacts.adddependencyArtifact );
                }
            }
        }
        Map<StringArtifactmanagedVersions = null;
        if ( managedDependencies != null && request.isResolveManagedVersions() )
        {
            managedVersions = new HashMap<StringArtifact>();
            for ( Dependency managedDependency : managedDependencies )
            {
                Artifact managedArtifact = createDependencyArtifactmanagedDependencynullpomArtifact );
                managedVersions.putmanagedDependency.getManagementKey(), managedArtifact );
            }
        }
        List<ArtifactRepositoryaggregatedRepositories =
            aggregateRepositoriesrequest.getRemoteRepositories(), pomRepositories );
        ResolutionGroup result =
            new ResolutionGrouppomArtifactrelocatedArtifactartifactsmanagedVersionsaggregatedRepositories );
        .putartifactrequest.isResolveManagedVersions(), request.getLocalRepository(),
                   request.getRemoteRepositories(), result );
        return result;
    }
    private boolean hasFileArtifact artifact )
    {
        return artifact != null && artifact.getFile() != null && artifact.getFile().exists();
    }
    private List<ArtifactRepositoryaggregateRepositoriesList<ArtifactRepositoryrequestRepositories,
                                                            List<ArtifactRepositorypomRepositories )
    {
        List<ArtifactRepositoryrepositories = requestRepositories;
        if ( pomRepositories != null && !pomRepositories.isEmpty() )
        {
            Map<StringArtifactRepositoryrepos = new LinkedHashMap<StringArtifactRepository>();
            for ( ArtifactRepository repo : requestRepositories )
            {
                if ( !repos.containsKeyrepo.getId() ) )
                {
                    repos.putrepo.getId(), repo );
                }
            }
            for ( ArtifactRepository repo : pomRepositories )
            {
                if ( !repos.containsKeyrepo.getId() ) )
                {
                    repos.putrepo.getId(), repo );
                }
            }
            repositories = new ArrayList<ArtifactRepository>( repos.values() );
        }
        return repositories;
    }
    private Artifact createDependencyArtifactDependency dependencyArtifact ownerArtifact pom )
    {
        try
        {
            String inheritedScope = ( owner != null ) ? owner.getScope() : null;
            ArtifactFilter inheritedFilter = ( owner != null ) ? owner.getDependencyFilter() : null;
            return createDependencyArtifactdependencyinheritedScopeinheritedFilter );
        }
        catch ( InvalidVersionSpecificationException e )
        {
            throw new ArtifactMetadataRetrievalException"Invalid version for dependency "
                + dependency.getManagementKey() + ": " + e.getMessage(), epom );
        }
    }
    private static Artifact createDependencyArtifactArtifactFactory factoryDependency dependency,
                                                      String inheritedScopeArtifactFilter inheritedFilter )
    {
        String effectiveScope = getEffectiveScopedependency.getScope(), inheritedScope );
        if ( effectiveScope == null )
        {
            return null;
        }
        VersionRange versionRange = VersionRange.createFromVersionSpecdependency.getVersion() );
        Artifact dependencyArtifact =
            factory.createDependencyArtifactdependency.getGroupId(), dependency.getArtifactId(), versionRange,
                                              dependency.getType(), dependency.getClassifier(), effectiveScope,
                                              dependency.isOptional() );
        ArtifactFilter dependencyFilter = inheritedFilter;
        if ( dependencyFilter != null && !dependencyFilter.includedependencyArtifact ) )
        {
            return null;
        }
        if ( ..equalseffectiveScope ) )
        {
            dependencyArtifact.setFilenew Filedependency.getSystemPath() ) );
        }
        dependencyArtifact.setDependencyFiltercreateDependencyFilterdependencydependencyFilter ) );
        return dependencyArtifact;
    }
    private static String getEffectiveScopeString originalScopeString inheritedScope )
    {
        String effectiveScope = .;
        if ( originalScope == null )
        {
            originalScope = .;
        }
        if ( inheritedScope == null )
        {
            // direct dependency retains its scope
            effectiveScope = originalScope;
        }
        else if ( ..equalsoriginalScope ) || ..equalsoriginalScope ) )
        {
            // test and provided are not transitive, so exclude them
            effectiveScope = null;
        }
        else if ( ..equalsoriginalScope ) )
        {
            // system scope come through unchanged...
            effectiveScope = .;
        }
        else if ( ..equalsoriginalScope ) && ..equalsinheritedScope ) )
        {
            // added to retain compile scope. Remove if you want compile inherited as runtime
            effectiveScope = .;
        }
        else if ( ..equalsinheritedScope ) )
        {
            effectiveScope = .;
        }
        else if ( ..equalsinheritedScope ) )
        {
            effectiveScope = .;
        }
        return effectiveScope;
    }
    private static ArtifactFilter createDependencyFilterDependency dependencyArtifactFilter inheritedFilter )
    {
        ArtifactFilter effectiveFilter = inheritedFilter;
        if ( !dependency.getExclusions().isEmpty() )
        {
            List<Stringexclusions = new ArrayList<String>();
            for ( Exclusion e : dependency.getExclusions() )
            {
                exclusions.adde.getGroupId() + ':' + e.getArtifactId() );
            }
            effectiveFilter = new ExcludesArtifactFilterexclusions );
            if ( inheritedFilter != null )
            {
                effectiveFilter = new AndArtifactFilter( Arrays.asListinheritedFiltereffectiveFilter ) );
            }
        }
        return effectiveFilter;
    }
    public List<ArtifactVersionretrieveAvailableVersionsArtifact artifactArtifactRepository localRepository,
                                                            List<ArtifactRepositoryremoteRepositories )
    {
        injectSessionrequest );
        request.setArtifactartifact );
        request.setLocalRepositorylocalRepository );
        request.setRemoteRepositoriesremoteRepositories );
        return retrieveAvailableVersionsrequest );
    }
    {
        RepositoryMetadata metadata = new ArtifactRepositoryMetadatarequest.getArtifact() );
        try
        {
            .resolvemetadatarequest );
        }
        catch ( RepositoryMetadataResolutionException e )
        {
            throw new ArtifactMetadataRetrievalExceptione.getMessage(), erequest.getArtifact() );
        }
        List<StringavailableVersions = request.getLocalRepository().findVersionsrequest.getArtifact() );
        return retrieveAvailableVersionsFromMetadatametadata.getMetadata(), availableVersions );
    }
                                                                                    ArtifactRepository localRepository,
                                                                                    ArtifactRepository deploymentRepository )
    {
        RepositoryMetadata metadata = new ArtifactRepositoryMetadataartifact );
        try
        {
            .resolveAlwaysmetadatalocalRepositorydeploymentRepository );
        }
        catch ( RepositoryMetadataResolutionException e )
        {
            throw new ArtifactMetadataRetrievalExceptione.getMessage(), eartifact );
        }
        List<StringavailableVersions = localRepository.findVersionsartifact );
        return retrieveAvailableVersionsFromMetadatametadata.getMetadata(), availableVersions );
    }
                                                                         List<StringavailableVersions )
    {
        Collection<Stringversions = new LinkedHashSet<String>();
        if ( ( repoMetadata != null ) && ( repoMetadata.getVersioning() != null ) )
        {
            versions.addAllrepoMetadata.getVersioning().getVersions() );
        }
        versions.addAllavailableVersions );
        List<ArtifactVersionartifactVersions = new ArrayList<ArtifactVersion>( versions.size() );
        for ( String version : versions )
        {
            artifactVersions.addnew DefaultArtifactVersionversion ) );
        }
        return artifactVersions;
    }
    // USED BY MAVEN ASSEMBLY PLUGIN
    @Deprecated
    public static Set<ArtifactcreateArtifactsArtifactFactory artifactFactoryList<Dependencydependencies,
                                                 String inheritedScopeArtifactFilter dependencyFilter,
                                                 MavenProject project )
        throws InvalidDependencyVersionException
    {
        Set<Artifactartifacts = new LinkedHashSet<Artifact>();
        for ( Dependency d : dependencies )
        {
            Artifact dependencyArtifact;
            try
            {
                dependencyArtifact = createDependencyArtifactartifactFactorydinheritedScopedependencyFilter );
            }
            catch ( InvalidVersionSpecificationException e )
            {
                throw new InvalidDependencyVersionExceptionproject.getId(), dproject.getFile(), e );
            }
            if ( dependencyArtifact != null )
            {
                artifacts.adddependencyArtifact );
            }
        }
        return artifacts;
    }
    {
        if (  != null )
        {
            return ;
        }
        try
        {
             = .lookupProjectBuilder.class );
        }
        catch ( ComponentLookupException e )
        {
            // Won't happen
        }
        return ;
    }
    private ProjectRelocation retrieveRelocatedProjectArtifact artifactMetadataResolutionRequest repositoryRequest )
    {
        MavenProject project;
        Artifact pomArtifact;
        Artifact relocatedArtifact = null;
        boolean done = false;
        do
        {
            project = null;
            pomArtifact =
                .createProjectArtifactartifact.getGroupId(),
                                                        artifact.getArtifactId(),
                                                        artifact.getVersion(), artifact.getScope() );
            if ( "pom".equalsartifact.getType() ) )
            {
                pomArtifact.setFileartifact.getFile() );
            }
            if ( ..equalsartifact.getScope() ) )
            {
                done = true;
            }
            else
            {
                try
                {
                    ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
                    configuration.setLocalRepositoryrepositoryRequest.getLocalRepository() );
                    configuration.setRemoteRepositoriesrepositoryRequest.getRemoteRepositories() );
                    configuration.setValidationLevel. );
                    configuration.setProcessPluginsfalse );
                    configuration.setRepositoryMerging.. );
                    configuration.setSystemPropertiesgetSystemProperties() );
                    configuration.setRepositorySession.getRepositorySession() );
                    project = getProjectBuilder().buildpomArtifactconfiguration ).getProject();
                }
                catch ( ProjectBuildingException e )
                {
                    ModelProblem missingParentPom = hasMissingParentPome );
                    if ( missingParentPom != null )
                    {
                        throw new ArtifactMetadataRetrievalException"Failed to process POM for "
                            + artifact.getId() + ": " + missingParentPom.getMessage(),
                                                                      missingParentPom.getException(),
                                                                      artifact );
                    }
                    String message;
                    if ( isMissingPome ) )
                    {
                        message = "Missing POM for " + artifact.getId();
                    }
                    else if ( isNonTransferrablePome ) )
                    {
                        throw new ArtifactMetadataRetrievalException"Failed to retrieve POM for "
                            + artifact.getId() + ": " + e.getCause().getMessage(), e.getCause(),
                                                                      artifact );
                    }
                    else
                    {
                        message =
                            "Invalid POM for " + artifact.getId()
                                + ", transitive dependencies (if any) will not be available"
                                + ", enable debug logging for more details";
                    }
                    if ( .isDebugEnabled() )
                    {
                        message += ": " + e.getMessage();
                    }
                    .warnmessage );
                }
                if ( project != null )
                {
                    Relocation relocation = null;
                    DistributionManagement distMgmt = project.getDistributionManagement();
                    if ( distMgmt != null )
                    {
                        relocation = distMgmt.getRelocation();
                        artifact.setDownloadUrldistMgmt.getDownloadUrl() );
                        pomArtifact.setDownloadUrldistMgmt.getDownloadUrl() );
                    }
                    if ( relocation != null )
                    {
                        if ( relocation.getGroupId() != null )
                        {
                            artifact.setGroupIdrelocation.getGroupId() );
                            relocatedArtifact = artifact;
                            project.setGroupIdrelocation.getGroupId() );
                        }
                        if ( relocation.getArtifactId() != null )
                        {
                            artifact.setArtifactIdrelocation.getArtifactId() );
                            relocatedArtifact = artifact;
                            project.setArtifactIdrelocation.getArtifactId() );
                        }
                        if ( relocation.getVersion() != null )
                        {
                            // note: see MNG-3454. This causes a problem, but fixing it may break more.
                            artifact.setVersionRange( VersionRange.createFromVersionrelocation.getVersion() ) );
                            relocatedArtifact = artifact;
                            project.setVersionrelocation.getVersion() );
                        }
                        if ( artifact.getDependencyFilter() != null
                            && !artifact.getDependencyFilter().includeartifact ) )
                        {
                            return null;
                        }
                        // MNG-2861: the artifact data has changed. If the available versions where previously
                        // retrieved, we need to update it.
                        // TODO: shouldn't the versions be merged across relocations?
                        List<ArtifactVersionavailable = artifact.getAvailableVersions();
                        if ( available != null && !available.isEmpty() )
                        {
                            MetadataResolutionRequest metadataRequest =
                                new DefaultMetadataResolutionRequestrepositoryRequest );
                            metadataRequest.setArtifactartifact );
                            available = retrieveAvailableVersionsmetadataRequest );
                            artifact.setAvailableVersionsavailable );
                        }
                        String message =
                            "\n  This artifact has been relocated to " + artifact.getGroupId() + ":"
                                + artifact.getArtifactId() + ":" + artifact.getVersion() + ".\n";
                        if ( relocation.getMessage() != null )
                        {
                            message += "  " + relocation.getMessage() + "\n";
                        }
                        if ( artifact.getDependencyTrail() != null && artifact.getDependencyTrail().size() == 1 )
                        {
                            .warn"While downloading " + pomArtifact.getGroupId() + ":"
                                + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion() + message + "\n" );
                        }
                        else
                        {
                            .debug"While downloading " + pomArtifact.getGroupId() + ":"
                                + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion() + message + "\n" );
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
                else
                {
                    done = true;
                }
            }
        }
        while ( !done );
        ProjectRelocation rel = new ProjectRelocation();
        rel.project = project;
        rel.pomArtifact = pomArtifact;
        rel.relocatedArtifact = relocatedArtifact;
        return rel;
    }
    {
        if ( e.getCause() instanceof ModelBuildingException )
        {
            ModelBuildingException mbe = (ModelBuildingExceptione.getCause();
            for ( ModelProblem problem : mbe.getProblems() )
            {
                if ( problem.getException() instanceof UnresolvableModelException )
                {
                    return problem;
                }
            }
        }
        return null;
    }
    private boolean isMissingPomException e )
    {
        if ( e.getCause() instanceof MultipleArtifactsNotFoundException )
        {
            return true;
        }
        if ( e.getCause() instanceof org.eclipse.aether.resolution.ArtifactResolutionException
            && e.getCause().getCause() instanceof ArtifactNotFoundException )
        {
            return true;
        }
        return false;
    }
    private boolean isNonTransferrablePomException e )
    {
        if ( e.getCause() instanceof ArtifactResolutionException )
        {
            return true;
        }
        if ( e.getCause() instanceof org.eclipse.aether.resolution.ArtifactResolutionException
            && !( e.getCause().getCause() instanceof ArtifactNotFoundException ) )
        {
            return true;
        }
        return false;
    }
    {
        Properties props = new Properties();
        EnvironmentUtils.addEnvVarsprops );
        props.putAll( System.getProperties() );
        return props;
    }
    private static final class ProjectRelocation
    {
        private MavenProject project;
        private Artifact pomArtifact;
        private Artifact relocatedArtifact;
    }
New to GrepCode? Check out our FAQ X