001package org.apache.maven.project;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025
026import org.apache.maven.artifact.InvalidRepositoryException;
027import org.apache.maven.artifact.repository.ArtifactRepository;
028import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
029import org.apache.maven.model.DeploymentRepository;
030import org.apache.maven.model.Repository;
031import org.apache.maven.plugin.LegacySupport;
032import org.apache.maven.repository.RepositorySystem;
033import org.codehaus.plexus.PlexusContainer;
034import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
035import org.eclipse.aether.RepositorySystemSession;
036
037// This class needs to stick around because it was exposed the the remote resources plugin started using it instead of
038// getting the repositories from the project.
039
040@Deprecated
041public final class ProjectUtils
042{
043
044    private ProjectUtils()
045    {
046    }
047
048    public static List<ArtifactRepository> buildArtifactRepositories( List<Repository> repositories,
049                                                                      ArtifactRepositoryFactory artifactRepositoryFactory,
050                                                                      PlexusContainer c )
051        throws InvalidRepositoryException
052    {
053
054        List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
055
056        for ( Repository r : repositories )
057        {
058            remoteRepositories.add( buildArtifactRepository( r, artifactRepositoryFactory, c ) );
059        }
060
061        return remoteRepositories;
062    }
063
064    public static ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo,
065                                                                        ArtifactRepositoryFactory artifactRepositoryFactory,
066                                                                        PlexusContainer c )
067        throws InvalidRepositoryException
068    {
069        return buildArtifactRepository( repo, artifactRepositoryFactory, c );
070    }
071
072    public static ArtifactRepository buildArtifactRepository( Repository repo,
073                                                              ArtifactRepositoryFactory artifactRepositoryFactory,
074                                                              PlexusContainer c )
075        throws InvalidRepositoryException
076    {
077        RepositorySystem repositorySystem = rs( c );
078        RepositorySystemSession session = rss( c );
079
080        ArtifactRepository repository = repositorySystem.buildArtifactRepository( repo );
081
082        if ( session != null )
083        {
084            repositorySystem.injectMirror( session, Arrays.asList( repository ) );
085            repositorySystem.injectProxy( session, Arrays.asList( repository ) );
086            repositorySystem.injectAuthentication( session, Arrays.asList( repository ) );
087        }
088
089        return repository;
090    }
091
092    private static RepositorySystem rs( PlexusContainer c )
093    {
094        try
095        {
096            return c.lookup( RepositorySystem.class );
097        }
098        catch ( ComponentLookupException e )
099        {
100            throw new IllegalStateException( e );
101        }
102    }
103
104    private static RepositorySystemSession rss( PlexusContainer c )
105    {
106        try
107        {
108            LegacySupport legacySupport = c.lookup( LegacySupport.class );
109
110            return legacySupport.getRepositorySession();
111        }
112        catch ( ComponentLookupException e )
113        {
114            throw new IllegalStateException( e );
115        }
116    }
117
118}