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