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