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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.project.ProjectBuildingRequest;
31 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
32 import org.apache.maven.shared.artifact.deploy.ArtifactDeployer;
33 import org.apache.maven.shared.artifact.deploy.ArtifactDeployerException;
34 import org.apache.maven.shared.project.NoFileAssignedException;
35 import org.apache.maven.shared.project.deploy.ProjectDeployer;
36 import org.apache.maven.shared.project.deploy.ProjectDeployerRequest;
37 import org.codehaus.plexus.component.annotations.Component;
38 import org.codehaus.plexus.component.annotations.Requirement;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49 @Component( role = ProjectDeployer.class )
50 public class DefaultProjectDeployer
51 implements ProjectDeployer
52 {
53 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectDeployer.class );
54
55 @Requirement
56 private ArtifactDeployer deployer;
57
58
59
60
61
62
63
64
65
66
67 public void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerRequest request,
68 ArtifactRepository artifactRepository )
69 throws IOException, NoFileAssignedException
70 {
71
72 Artifact artifact = request.getProject().getArtifact();
73 String packaging = request.getProject().getPackaging();
74 File pomFile = request.getProject().getFile();
75
76 List<Artifact> attachedArtifacts = request.getProject().getAttachedArtifacts();
77
78
79 boolean isPomArtifact = "pom".equals( packaging );
80 if ( !isPomArtifact )
81 {
82 ProjectArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
83 artifact.addMetadata( metadata );
84 }
85 else
86 {
87 artifact.setFile( pomFile );
88 }
89
90 if ( request.isUpdateReleaseInfo() )
91 {
92 artifact.setRelease( true );
93 }
94
95 artifact.setRepository( artifactRepository );
96
97 int retryFailedDeploymentCount = request.getRetryFailedDeploymentCount();
98
99 try
100 {
101 List<Artifact> deployableArtifacts = new ArrayList<Artifact>();
102 if ( isPomArtifact )
103 {
104 deployableArtifacts.add( artifact );
105 }
106 else
107 {
108 File file = artifact.getFile();
109
110 if ( file != null && file.isFile() )
111 {
112 deployableArtifacts.add( artifact );
113 }
114 else if ( !attachedArtifacts.isEmpty() )
115 {
116
117 throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
118 + "a main file to the project but it has attachments. Change packaging to 'pom'." );
119 }
120 else
121 {
122
123 throw new NoFileAssignedException( "The packaging for this project did not assign "
124 + "a file to the build artifact" );
125 }
126 }
127
128 for ( Artifact attached : attachedArtifacts )
129 {
130
131 try
132 {
133 attached.setRepository( artifactRepository );
134 }
135 catch ( UnsupportedOperationException e )
136 {
137 LOGGER.warn( attached.getId() + " has been attached with deprecated code, "
138 + "try to upgrade the responsible plugin" );
139 }
140
141 deployableArtifacts.add( attached );
142 }
143
144 deploy( buildingRequest, deployableArtifacts, artifactRepository, retryFailedDeploymentCount );
145 }
146 catch ( ArtifactDeployerException e )
147 {
148 throw new IllegalArgumentException( e.getMessage(), e );
149 }
150 }
151
152 private void deploy( ProjectBuildingRequest request, Collection<Artifact> artifacts,
153 ArtifactRepository deploymentRepository, int retryFailedDeploymentCount )
154 throws ArtifactDeployerException
155 {
156
157
158 int retryFailedDeploymentCounter = Math.max( 1, Math.min( 10, retryFailedDeploymentCount ) );
159 ArtifactDeployerException exception = null;
160 for ( int count = 0; count < retryFailedDeploymentCounter; count++ )
161 {
162 try
163 {
164 if ( count > 0 )
165 {
166 LOGGER.info( "Retrying deployment attempt " + ( count + 1 ) + " of "
167 + retryFailedDeploymentCounter );
168 }
169
170 deployer.deploy( request, deploymentRepository, artifacts );
171 exception = null;
172 break;
173 }
174 catch ( ArtifactDeployerException e )
175 {
176 if ( count + 1 < retryFailedDeploymentCounter )
177 {
178 LOGGER.warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
179 LOGGER.debug( e.getMessage() );
180 }
181 if ( exception == null )
182 {
183 exception = e;
184 }
185 }
186 }
187 if ( exception != null )
188 {
189 throw exception;
190 }
191 }
192
193 }