View Javadoc
1   package org.apache.maven.shared.artifact.deploy.internal;
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.util.Collection;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.metadata.ArtifactMetadata;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
30  import org.apache.maven.shared.artifact.deploy.ArtifactDeployer;
31  import org.apache.maven.shared.artifact.deploy.ArtifactDeployerException;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.sonatype.aether.RepositorySystem;
35  import org.sonatype.aether.RepositorySystemSession;
36  import org.sonatype.aether.artifact.Artifact;
37  import org.sonatype.aether.deployment.DeployRequest;
38  import org.sonatype.aether.deployment.DeploymentException;
39  import org.sonatype.aether.repository.RemoteRepository;
40  import org.sonatype.aether.util.artifact.SubArtifact;
41  
42  /**
43   * 
44   */
45  @Component( role = ArtifactDeployer.class, hint = "maven3" )
46  public class Maven30ArtifactDeployer
47      implements ArtifactDeployer
48  {
49  
50      @Requirement
51      private RepositorySystem repositorySystem;
52  
53      @Override
54      public void deploy( ProjectBuildingRequest buildingRequest,
55                          Collection<org.apache.maven.artifact.Artifact> mavenArtifacts )
56                              throws ArtifactDeployerException
57      {
58          deploy( buildingRequest, null, mavenArtifacts );
59      }
60  
61      @Override
62      public void deploy( ProjectBuildingRequest buildingRequest, ArtifactRepository remoteRepository,
63                          Collection<org.apache.maven.artifact.Artifact> mavenArtifacts )
64                              throws ArtifactDeployerException
65      {
66          // prepare request
67          DeployRequest request = new DeployRequest();
68  
69          RepositorySystemSession session =
70              (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
71  
72          RemoteRepository defaultRepository = null;
73  
74          if ( remoteRepository != null )
75          {
76              defaultRepository = getRemoteRepository( session, remoteRepository );
77          }
78  
79          // transform artifacts
80          for ( org.apache.maven.artifact.Artifact mavenArtifact : mavenArtifacts )
81          {
82              Artifact aetherArtifact =
83                  (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
84                                             org.apache.maven.artifact.Artifact.class, mavenArtifact );
85              request.addArtifact( aetherArtifact );
86  
87              RemoteRepository aetherRepository;
88              if ( remoteRepository == null )
89              {
90                  aetherRepository = getRemoteRepository( session, mavenArtifact.getRepository() );
91              }
92              else
93              {
94                  aetherRepository = defaultRepository;
95              }
96  
97              request.setRepository( aetherRepository );
98  
99              for ( ArtifactMetadata metadata : mavenArtifact.getMetadataList() )
100             {
101                 if ( metadata instanceof ProjectArtifactMetadata )
102                 {
103                     Artifact pomArtifact = new SubArtifact( aetherArtifact, "", "pom" );
104                     pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
105                     request.addArtifact( pomArtifact );
106                 }
107                 else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
108                 metadata instanceof ArtifactRepositoryMetadata )
109                 {
110                     // eaten, handled by repo system
111                 }
112                 else
113                 {
114                     // request.addMetadata( new MetadataBridge( metadata ) );
115                 }
116             }
117         }
118 
119         // deploy
120         try
121         {
122             repositorySystem.deploy( session, request );
123         }
124         catch ( DeploymentException e )
125         {
126             throw new ArtifactDeployerException( e.getMessage(), e );
127         }
128     }
129 
130     private RemoteRepository getRemoteRepository( RepositorySystemSession session, ArtifactRepository remoteRepository )
131         throws ArtifactDeployerException
132     {
133         // CHECKSTYLE_OFF: LineLength
134         RemoteRepository aetherRepo = (RemoteRepository) Invoker.invoke( RepositoryUtils.class, "toRepo",
135                                                                          org.apache.maven.artifact.repository.ArtifactRepository.class,
136                                                                          remoteRepository );
137         // CHECKSTYLE_ON: LineLength
138 
139         if ( aetherRepo.getAuthentication() == null )
140         {
141             aetherRepo.setAuthentication( session.getAuthenticationSelector().getAuthentication( aetherRepo ) );
142         }
143 
144         if ( aetherRepo.getProxy() == null )
145         {
146             aetherRepo.setProxy( session.getProxySelector().getProxy( aetherRepo ) );
147         }
148 
149         return aetherRepo;
150     }
151 }