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.deployment.DeployRequest;
42  import org.sonatype.aether.deployment.DeployResult;
43  import org.sonatype.aether.deployment.DeploymentException;
44  import org.sonatype.aether.metadata.MergeableMetadata;
45  import org.sonatype.aether.repository.LocalRepository;
46  import org.sonatype.aether.repository.RemoteRepository;
47  import org.sonatype.aether.util.DefaultRepositorySystemSession;
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          DefaultRepositorySystemSession session =
83              new DefaultRepositorySystemSession( legacySupport.getRepositorySession() );
84          session.setLocalRepositoryManager( LegacyLocalRepositoryManager.wrap( localRepository, repoSystem ) );
85  
86          DeployRequest request = new DeployRequest();
87  
88          org.sonatype.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
89          mainArtifact = mainArtifact.setFile( source );
90          request.addArtifact( mainArtifact );
91  
92          String versionKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
93          String snapshotKey = null;
94          if ( artifact.isSnapshot() )
95          {
96              snapshotKey = versionKey + ':' + artifact.getBaseVersion();
97              request.addMetadata( relatedMetadata.get( snapshotKey ) );
98          }
99          request.addMetadata( relatedMetadata.get( versionKey ) );
100 
101         for ( ArtifactMetadata metadata : artifact.getMetadataList() )
102         {
103             if ( metadata instanceof ProjectArtifactMetadata )
104             {
105                 org.sonatype.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
106                 pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
107                 request.addArtifact( pomArtifact );
108             }
109             else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
110                 || metadata instanceof ArtifactRepositoryMetadata )
111             {
112                 // eaten, handled by repo system
113             }
114             else
115             {
116                 request.addMetadata( new MetadataBridge( metadata ) );
117             }
118         }
119 
120         RemoteRepository remoteRepo = RepositoryUtils.toRepo( deploymentRepository );
121         /*
122          * NOTE: This provides backward-compat with maven-deploy-plugin:2.4 which bypasses the repository factory when
123          * using an alternative deployment location.
124          */
125         if ( deploymentRepository instanceof DefaultArtifactRepository
126             && deploymentRepository.getAuthentication() == null )
127         {
128             remoteRepo.setAuthentication( session.getAuthenticationSelector().getAuthentication( remoteRepo ) );
129             remoteRepo.setProxy( session.getProxySelector().getProxy( remoteRepo ) );
130         }
131         request.setRepository( remoteRepo );
132 
133         DeployResult result;
134         try
135         {
136             result = repoSystem.deploy( session, request );
137         }
138         catch ( DeploymentException e )
139         {
140             throw new ArtifactDeploymentException( e.getMessage(), e );
141         }
142 
143         for ( Object metadata : result.getMetadata() )
144         {
145             if ( metadata.getClass().getName().endsWith( ".internal.VersionsMetadata" ) )
146             {
147                 relatedMetadata.put( versionKey, (MergeableMetadata) metadata );
148             }
149             if ( snapshotKey != null && metadata.getClass().getName().endsWith( ".internal.RemoteSnapshotMetadata" ) )
150             {
151                 relatedMetadata.put( snapshotKey, (MergeableMetadata) metadata );
152             }
153         }
154 
155         artifact.setResolvedVersion( result.getArtifacts().iterator().next().getVersion() );
156     }
157 
158 }