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.eclipse.aether.RepositorySystem;
41  import org.eclipse.aether.RepositorySystemSession;
42  import org.eclipse.aether.RequestTrace;
43  import org.eclipse.aether.deployment.DeployRequest;
44  import org.eclipse.aether.deployment.DeployResult;
45  import org.eclipse.aether.deployment.DeploymentException;
46  import org.eclipse.aether.metadata.MergeableMetadata;
47  import org.eclipse.aether.repository.RemoteRepository;
48  import org.eclipse.aether.util.artifact.SubArtifact;
49  
50  /**
51   * DefaultArtifactDeployer
52   */
53  @Component( role = ArtifactDeployer.class, instantiationStrategy = "per-lookup" )
54  public class DefaultArtifactDeployer
55      extends AbstractLogEnabled
56      implements ArtifactDeployer
57  {
58  
59      @Requirement
60      private RepositorySystem repoSystem;
61  
62      @Requirement
63      private LegacySupport legacySupport;
64  
65      private Map<Object, MergeableMetadata> relatedMetadata = new ConcurrentHashMap<>();
66  
67      /**
68       * @deprecated we want to use the artifact method only, and ensure artifact.file is set
69       *             correctly.
70       */
71      @Deprecated
72      public void deploy( String basedir, String finalName, Artifact artifact, ArtifactRepository deploymentRepository,
73                          ArtifactRepository localRepository )
74          throws ArtifactDeploymentException
75      {
76          String extension = artifact.getArtifactHandler().getExtension();
77          File source = new File( basedir, finalName + "." + extension );
78          deploy( source, artifact, deploymentRepository, localRepository );
79      }
80  
81      public void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
82                          ArtifactRepository localRepository )
83          throws ArtifactDeploymentException
84      {
85          RepositorySystemSession session =
86              LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
87  
88          DeployRequest request = new DeployRequest();
89  
90          request.setTrace( RequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
91  
92          org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
93          mainArtifact = mainArtifact.setFile( source );
94          request.addArtifact( mainArtifact );
95  
96          String versionKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
97          String snapshotKey = null;
98          if ( artifact.isSnapshot() )
99          {
100             snapshotKey = versionKey + ':' + artifact.getBaseVersion();
101             request.addMetadata( relatedMetadata.get( snapshotKey ) );
102         }
103         request.addMetadata( relatedMetadata.get( versionKey ) );
104 
105         for ( ArtifactMetadata metadata : artifact.getMetadataList() )
106         {
107             if ( metadata instanceof ProjectArtifactMetadata )
108             {
109                 org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
110                 pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
111                 request.addArtifact( pomArtifact );
112             }
113             else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
114                 || metadata instanceof ArtifactRepositoryMetadata )
115             {
116                 // eaten, handled by repo system
117             }
118             else
119             {
120                 request.addMetadata( new MetadataBridge( metadata ) );
121             }
122         }
123 
124         RemoteRepository remoteRepo = RepositoryUtils.toRepo( deploymentRepository );
125         /*
126          * NOTE: This provides backward-compat with maven-deploy-plugin:2.4 which bypasses the repository factory when
127          * using an alternative deployment location.
128          */
129         if ( deploymentRepository instanceof DefaultArtifactRepository
130             && deploymentRepository.getAuthentication() == null )
131         {
132             RemoteRepository.Builder builder = new RemoteRepository.Builder( remoteRepo );
133             builder.setAuthentication( session.getAuthenticationSelector().getAuthentication( remoteRepo ) );
134             builder.setProxy( session.getProxySelector().getProxy( remoteRepo ) );
135             remoteRepo = builder.build();
136         }
137         request.setRepository( remoteRepo );
138 
139         DeployResult result;
140         try
141         {
142             result = repoSystem.deploy( session, request );
143         }
144         catch ( DeploymentException e )
145         {
146             throw new ArtifactDeploymentException( e.getMessage(), e );
147         }
148 
149         for ( Object metadata : result.getMetadata() )
150         {
151             if ( metadata.getClass().getName().endsWith( ".internal.VersionsMetadata" ) )
152             {
153                 relatedMetadata.put( versionKey, (MergeableMetadata) metadata );
154             }
155             if ( snapshotKey != null && metadata.getClass().getName().endsWith( ".internal.RemoteSnapshotMetadata" ) )
156             {
157                 relatedMetadata.put( snapshotKey, (MergeableMetadata) metadata );
158             }
159         }
160 
161         artifact.setResolvedVersion( result.getArtifacts().iterator().next().getVersion() );
162     }
163 
164 }