View Javadoc
1   package org.apache.maven.shared.transfer.project.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.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
31  import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
32  import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
33  import org.apache.maven.shared.transfer.project.NoFileAssignedException;
34  import org.apache.maven.shared.transfer.project.deploy.ProjectDeployer;
35  import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest;
36  import org.apache.maven.shared.transfer.repository.RepositoryManager;
37  import org.codehaus.plexus.component.annotations.Component;
38  import org.codehaus.plexus.component.annotations.Requirement;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * This will deploy a whole project into the appropriate remote repository.
44   * 
45   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a> Most of the code is
46   *         taken from maven-deploy-plugin.
47   */
48  @Component( role = ProjectDeployer.class )
49  class DefaultProjectDeployer
50      implements ProjectDeployer
51  {
52      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectDeployer.class );
53  
54      @Requirement
55      private ArtifactDeployer deployer;
56  
57      @Requirement
58      private RepositoryManager repositoryManager;
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerRequest projectDeployerRequest,
64                          ArtifactRepository artifactRepository )
65          throws NoFileAssignedException, IllegalArgumentException, ArtifactDeployerException
66      {
67          validateParameters( buildingRequest, projectDeployerRequest, artifactRepository );
68  
69          Artifact artifact = projectDeployerRequest.getProject().getArtifact();
70          String packaging = projectDeployerRequest.getProject().getPackaging();
71          File pomFile = projectDeployerRequest.getProject().getFile();
72  
73          List<Artifact> attachedArtifacts = projectDeployerRequest.getProject().getAttachedArtifacts();
74  
75          // Deploy the POM
76          boolean isPomArtifact = "pom".equals( packaging );
77          if ( isPomArtifact )
78          {
79              artifact.setFile( pomFile );
80          }
81          else
82          {
83              ProjectArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
84              artifact.addMetadata( metadata );
85          }
86  
87          // What consequence does this have?
88          // artifact.setRelease( true );
89  
90          artifact.setRepository( artifactRepository );
91  
92          int retryFailedDeploymentCount = projectDeployerRequest.getRetryFailedDeploymentCount();
93  
94          List<Artifact> deployableArtifacts = new ArrayList<>();
95          if ( isPomArtifact )
96          {
97              deployableArtifacts.add( artifact );
98          }
99          else
100         {
101             File file = artifact.getFile();
102 
103             if ( file != null && file.isFile() )
104             {
105                 deployableArtifacts.add( artifact );
106             }
107             else if ( !attachedArtifacts.isEmpty() )
108             {
109                 // TODO: Reconsider this exception? Better Exception type?
110                 throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
111                     + "a main file to the project but it has attachments. Change packaging to 'pom'." );
112             }
113             else
114             {
115                 // TODO: Reconsider this exception? Better Exception type?
116                 throw new NoFileAssignedException( "The packaging for this project did not assign "
117                     + "a file to the build artifact" );
118             }
119         }
120 
121         deployableArtifacts.addAll( attachedArtifacts );
122 
123         deploy( buildingRequest, deployableArtifacts, artifactRepository, retryFailedDeploymentCount );
124     }
125 
126     private void validateParameters( ProjectBuildingRequest buildingRequest,
127                                      ProjectDeployerRequest projectDeployerRequest,
128                                      ArtifactRepository artifactRepository )
129     {
130         if ( buildingRequest == null )
131         {
132             throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
133         }
134         if ( projectDeployerRequest == null )
135         {
136             throw new IllegalArgumentException( "The parameter projectDeployerRequest is not allowed to be null." );
137         }
138         if ( artifactRepository == null )
139         {
140             throw new IllegalArgumentException( "The parameter artifactRepository is not allowed to be null." );
141         }
142     }
143 
144     private void deploy( ProjectBuildingRequest request, Collection<Artifact> artifacts,
145                          ArtifactRepository deploymentRepository, int retryFailedDeploymentCount )
146         throws ArtifactDeployerException
147     {
148 
149         // for now retry means redeploy the complete artifacts collection
150         int retryFailedDeploymentCounter = Math.max( 1, Math.min( 10, retryFailedDeploymentCount ) );
151         ArtifactDeployerException exception = null;
152         for ( int count = 0; count < retryFailedDeploymentCounter; count++ )
153         {
154             try
155             {
156                 if ( count > 0 )
157                 {
158                     LOGGER.info( "Retrying deployment attempt " + ( count + 1 ) + " of "
159                         + retryFailedDeploymentCounter );
160                 }
161 
162                 deployer.deploy( request, deploymentRepository, artifacts );
163                 exception = null;
164                 break;
165             }
166             catch ( ArtifactDeployerException e )
167             {
168                 if ( count + 1 < retryFailedDeploymentCounter )
169                 {
170                     LOGGER.warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
171                     LOGGER.debug( e.getMessage() );
172                 }
173                 if ( exception == null )
174                 {
175                     exception = e;
176                 }
177             }
178         }
179         if ( exception != null )
180         {
181             throw exception;
182         }
183     }
184 
185 }