View Javadoc

1   package org.apache.maven.artifact.deployer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import org.apache.maven.RepositoryUtils;
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.metadata.ArtifactMetadata;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
31  import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
32  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
33  import org.apache.maven.artifact.repository.metadata.MetadataBridge;
34  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35  import org.apache.maven.plugin.LegacySupport;
36  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
37  import org.codehaus.plexus.component.annotations.Component;
38  import org.codehaus.plexus.component.annotations.Requirement;
39  import org.codehaus.plexus.logging.AbstractLogEnabled;
40  import org.sonatype.aether.RepositorySystem;
41  import org.sonatype.aether.RepositorySystemSession;
42  import org.sonatype.aether.deployment.DeployRequest;
43  import org.sonatype.aether.deployment.DeployResult;
44  import org.sonatype.aether.deployment.DeploymentException;
45  import org.sonatype.aether.metadata.MergeableMetadata;
46  import org.sonatype.aether.repository.RemoteRepository;
47  import org.sonatype.aether.util.DefaultRequestTrace;
48  import org.sonatype.aether.util.artifact.SubArtifact;
49  
50  @Component( role = ArtifactDeployer.class, instantiationStrategy = "per-lookup" )
51  public class DefaultArtifactDeployer
52      extends AbstractLogEnabled
53      implements ArtifactDeployer
54  {
55  
56      @Requirement
57      private RepositorySystem repoSystem;
58  
59      @Requirement
60      private LegacySupport legacySupport;
61  
62      private Map<Object, MergeableMetadata> relatedMetadata = new ConcurrentHashMap<Object, MergeableMetadata>();
63  
64      /**
65       * @deprecated we want to use the artifact method only, and ensure artifact.file is set
66       *             correctly.
67       */
68      @Deprecated
69      public void deploy( String basedir, String finalName, Artifact artifact, ArtifactRepository deploymentRepository,
70                          ArtifactRepository localRepository )
71          throws ArtifactDeploymentException
72      {
73          String extension = artifact.getArtifactHandler().getExtension();
74          File source = new File( basedir, finalName + "." + extension );
75          deploy( source, artifact, deploymentRepository, localRepository );
76      }
77  
78      public void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
79                          ArtifactRepository localRepository )
80          throws ArtifactDeploymentException
81      {
82          RepositorySystemSession session =
83              LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
84  
85          DeployRequest request = new DeployRequest();
86  
87          request.setTrace( DefaultRequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
88  
89          org.sonatype.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
90          mainArtifact = mainArtifact.setFile( source );
91          request.addArtifact( mainArtifact );
92  
93          String versionKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
94          String snapshotKey = null;
95          if ( artifact.isSnapshot() )
96          {
97              snapshotKey = versionKey + ':' + artifact.getBaseVersion();
98              request.addMetadata( relatedMetadata.get( snapshotKey ) );
99          }
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 }