View Javadoc

1   package org.apache.maven.plugin.deploy;
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  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.deployer.ArtifactDeployer;
27  import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
31  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  
36  /**
37   * @version $Id: AbstractDeployMojo.java 1137869 2011-06-21 06:18:10Z stephenc $
38   */
39  public abstract class AbstractDeployMojo
40      extends AbstractMojo
41  {
42      /**
43       * @component
44       */
45      private ArtifactDeployer deployer;
46  
47      /**
48       * Component used to create an artifact.
49       *
50       * @component
51       */
52      protected ArtifactFactory artifactFactory;
53  
54      /**
55       * Component used to create a repository.
56       *
57       * @component
58       */
59      ArtifactRepositoryFactory repositoryFactory;
60  
61      /**
62       * Map that contains the layouts.
63       *
64       * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
65       */
66      private Map repositoryLayouts;
67  
68      /**
69       * @parameter default-value="${localRepository}"
70       * @required
71       * @readonly
72       */
73      private ArtifactRepository localRepository;
74  
75      /**
76       * Flag whether Maven is currently in online/offline mode.
77       * 
78       * @parameter default-value="${settings.offline}"
79       * @readonly
80       */
81      private boolean offline;
82  
83      /**
84       * Parameter used to update the metadata to make the artifact as release.
85       * 
86       * @parameter expression="${updateReleaseInfo}" default-value="false"
87       */
88      protected boolean updateReleaseInfo;
89  
90      /**
91       * Parameter used to control how many times a failed deployment will be retried before giving up and failing.
92       * If a value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
93       *
94       * @parameter expression="${retryFailedDeploymentCount}" default-value="1"
95       * @since 2.7
96       */
97      private int retryFailedDeploymentCount;
98  
99      /* Setters and Getters */
100 
101     public ArtifactDeployer getDeployer()
102     {
103         return deployer;
104     }
105 
106     public void setDeployer( ArtifactDeployer deployer )
107     {
108         this.deployer = deployer;
109     }
110 
111     public ArtifactRepository getLocalRepository()
112     {
113         return localRepository;
114     }
115 
116     public void setLocalRepository( ArtifactRepository localRepository )
117     {
118         this.localRepository = localRepository;
119     }
120 
121     void failIfOffline()
122         throws MojoFailureException
123     {
124         if ( offline )
125         {
126             throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" );
127         }
128     }
129 
130     ArtifactRepositoryLayout getLayout( String id )
131         throws MojoExecutionException
132     {
133         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( id );
134 
135         if ( layout == null )
136         {
137             throw new MojoExecutionException( "Invalid repository layout: " + id );
138         }
139 
140         return layout;
141     }
142 
143     /**
144      * Deploy an artifact from a particular file.
145      *
146      * @param source the file to deploy
147      * @param artifact the artifact definition
148      * @param deploymentRepository the repository to deploy to
149      * @param localRepository the local repository to install into
150      * @throws ArtifactDeploymentException if an error occurred deploying the artifact
151      */
152     protected void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
153                            ArtifactRepository localRepository )
154         throws ArtifactDeploymentException
155     {
156         int retryFailedDeploymentCount = Math.max( 1, Math.min( 10, this.retryFailedDeploymentCount ) );
157         ArtifactDeploymentException exception = null;
158         for ( int count = 0; count < retryFailedDeploymentCount; count++ )
159         {
160             try
161             {
162                 if (count > 0)
163                 {
164                     getLog().info(
165                         "Retrying deployment attempt " + ( count + 1 ) + " of " + retryFailedDeploymentCount );
166                 }
167                 getDeployer().deploy( source, artifact, deploymentRepository, localRepository );
168                 exception = null;
169                 break;
170             }
171             catch ( ArtifactDeploymentException e )
172             {
173                 if (count + 1 < retryFailedDeploymentCount) {
174                     getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage());
175                     getLog().debug( e );
176                 }
177                 if ( exception == null )
178                 {
179                     exception = e;
180                 }
181             }
182         }
183         if ( exception != null )
184         {
185             throw exception;
186         }
187     }
188 }