1 package org.apache.maven.shared.project.deploy.internal;
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.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
43
44
45
46
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
59
60
61
62
63
64
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
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
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
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
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
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 }