001    package org.apache.maven.artifact.deployer;
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    
022    import java.io.File;
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    import org.apache.maven.RepositoryUtils;
027    import org.apache.maven.artifact.Artifact;
028    import org.apache.maven.artifact.metadata.ArtifactMetadata;
029    import org.apache.maven.artifact.repository.ArtifactRepository;
030    import org.apache.maven.artifact.repository.DefaultArtifactRepository;
031    import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
032    import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
033    import org.apache.maven.artifact.repository.metadata.MetadataBridge;
034    import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
035    import org.apache.maven.plugin.LegacySupport;
036    import org.apache.maven.project.artifact.ProjectArtifactMetadata;
037    import org.codehaus.plexus.component.annotations.Component;
038    import org.codehaus.plexus.component.annotations.Requirement;
039    import org.codehaus.plexus.logging.AbstractLogEnabled;
040    import org.sonatype.aether.RepositorySystem;
041    import org.sonatype.aether.RepositorySystemSession;
042    import org.sonatype.aether.deployment.DeployRequest;
043    import org.sonatype.aether.deployment.DeployResult;
044    import org.sonatype.aether.deployment.DeploymentException;
045    import org.sonatype.aether.metadata.MergeableMetadata;
046    import org.sonatype.aether.repository.RemoteRepository;
047    import org.sonatype.aether.util.DefaultRequestTrace;
048    import org.sonatype.aether.util.artifact.SubArtifact;
049    
050    @Component( role = ArtifactDeployer.class, instantiationStrategy = "per-lookup" )
051    public class DefaultArtifactDeployer
052        extends AbstractLogEnabled
053        implements ArtifactDeployer
054    {
055    
056        @Requirement
057        private RepositorySystem repoSystem;
058    
059        @Requirement
060        private LegacySupport legacySupport;
061    
062        private Map<Object, MergeableMetadata> relatedMetadata = new ConcurrentHashMap<Object, MergeableMetadata>();
063    
064        /**
065         * @deprecated we want to use the artifact method only, and ensure artifact.file is set
066         *             correctly.
067         */
068        @Deprecated
069        public void deploy( String basedir, String finalName, Artifact artifact, ArtifactRepository deploymentRepository,
070                            ArtifactRepository localRepository )
071            throws ArtifactDeploymentException
072        {
073            String extension = artifact.getArtifactHandler().getExtension();
074            File source = new File( basedir, finalName + "." + extension );
075            deploy( source, artifact, deploymentRepository, localRepository );
076        }
077    
078        public void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
079                            ArtifactRepository localRepository )
080            throws ArtifactDeploymentException
081        {
082            RepositorySystemSession session =
083                LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
084    
085            DeployRequest request = new DeployRequest();
086    
087            request.setTrace( DefaultRequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
088    
089            org.sonatype.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
090            mainArtifact = mainArtifact.setFile( source );
091            request.addArtifact( mainArtifact );
092    
093            String versionKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
094            String snapshotKey = null;
095            if ( artifact.isSnapshot() )
096            {
097                snapshotKey = versionKey + ':' + artifact.getBaseVersion();
098                request.addMetadata( relatedMetadata.get( snapshotKey ) );
099            }
100            request.addMetadata( relatedMetadata.get( versionKey ) );
101    
102            for ( ArtifactMetadata metadata : artifact.getMetadataList() )
103            {
104                if ( metadata instanceof ProjectArtifactMetadata )
105                {
106                    org.sonatype.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
107                    pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
108                    request.addArtifact( pomArtifact );
109                }
110                else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
111                    || metadata instanceof ArtifactRepositoryMetadata )
112                {
113                    // eaten, handled by repo system
114                }
115                else
116                {
117                    request.addMetadata( new MetadataBridge( metadata ) );
118                }
119            }
120    
121            RemoteRepository remoteRepo = RepositoryUtils.toRepo( deploymentRepository );
122            /*
123             * NOTE: This provides backward-compat with maven-deploy-plugin:2.4 which bypasses the repository factory when
124             * using an alternative deployment location.
125             */
126            if ( deploymentRepository instanceof DefaultArtifactRepository
127                && deploymentRepository.getAuthentication() == null )
128            {
129                remoteRepo.setAuthentication( session.getAuthenticationSelector().getAuthentication( remoteRepo ) );
130                remoteRepo.setProxy( session.getProxySelector().getProxy( remoteRepo ) );
131            }
132            request.setRepository( remoteRepo );
133    
134            DeployResult result;
135            try
136            {
137                result = repoSystem.deploy( session, request );
138            }
139            catch ( DeploymentException e )
140            {
141                throw new ArtifactDeploymentException( e.getMessage(), e );
142            }
143    
144            for ( Object metadata : result.getMetadata() )
145            {
146                if ( metadata.getClass().getName().endsWith( ".internal.VersionsMetadata" ) )
147                {
148                    relatedMetadata.put( versionKey, (MergeableMetadata) metadata );
149                }
150                if ( snapshotKey != null && metadata.getClass().getName().endsWith( ".internal.RemoteSnapshotMetadata" ) )
151                {
152                    relatedMetadata.put( snapshotKey, (MergeableMetadata) metadata );
153                }
154            }
155    
156            artifact.setResolvedVersion( result.getArtifacts().iterator().next().getVersion() );
157        }
158    
159    }