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