1 package org.apache.maven.plugin.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41 public abstract class AbstractDeployMojo
42 extends AbstractMojo
43 {
44
45
46 @Component
47 private ArtifactDeployer deployer;
48
49
50
51
52 @Component
53 protected ArtifactFactory artifactFactory;
54
55
56
57
58 @Component
59 ArtifactRepositoryFactory repositoryFactory;
60
61
62
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
74
75 @Parameter( defaultValue = "${settings.offline}", readonly = true )
76 private boolean offline;
77
78
79
80
81 @Parameter( property = "updateReleaseInfo", defaultValue = "false" )
82 protected boolean updateReleaseInfo;
83
84
85
86
87
88
89
90 @Parameter( property = "retryFailedDeploymentCount", defaultValue = "1" )
91 private int retryFailedDeploymentCount;
92
93
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
149
150
151
152
153
154
155
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 }