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 org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.deployer.ArtifactDeploymentException; |
24 | |
import org.apache.maven.artifact.metadata.ArtifactMetadata; |
25 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
26 | |
import org.apache.maven.artifact.repository.DefaultArtifactRepository; |
27 | |
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
28 | |
import org.apache.maven.plugin.MojoExecutionException; |
29 | |
import org.apache.maven.plugin.MojoFailureException; |
30 | |
import org.apache.maven.project.artifact.ProjectArtifactMetadata; |
31 | |
import org.codehaus.plexus.PlexusConstants; |
32 | |
import org.codehaus.plexus.PlexusContainer; |
33 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
34 | |
import org.codehaus.plexus.context.Context; |
35 | |
import org.codehaus.plexus.context.ContextException; |
36 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
37 | |
|
38 | |
import java.io.File; |
39 | |
import java.util.Iterator; |
40 | |
import java.util.List; |
41 | |
import java.util.regex.Matcher; |
42 | |
import java.util.regex.Pattern; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 8 | public class DeployMojo |
54 | |
extends AbstractDeployMojo |
55 | |
implements Contextualizable |
56 | |
{ |
57 | |
|
58 | 1 | private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" ); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private Artifact artifact; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private String packaging; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private File pomFile; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private ArtifactRepository deploymentRepository; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private String altDeploymentRepository; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private List attachedArtifacts; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
private boolean updateReleaseInfo; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
private PlexusContainer container; |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
private boolean skip; |
123 | |
|
124 | |
public void execute() |
125 | |
throws MojoExecutionException, MojoFailureException |
126 | |
{ |
127 | |
|
128 | 7 | if ( skip ) |
129 | |
{ |
130 | 1 | getLog().info( " skipping artifact deployement " ); |
131 | 1 | return; |
132 | |
} |
133 | |
|
134 | 6 | ArtifactRepository repo = getDeploymentRepository(); |
135 | |
|
136 | 6 | String protocol = repo.getProtocol(); |
137 | |
|
138 | 6 | if ( protocol.equalsIgnoreCase( "scp" ) ) |
139 | |
{ |
140 | 1 | File sshFile = new File( System.getProperty( "user.home" ), ".ssh" ); |
141 | |
|
142 | 1 | if ( !sshFile.exists() ) |
143 | |
{ |
144 | 1 | sshFile.mkdirs(); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
|
149 | 6 | boolean isPomArtifact = "pom".equals( packaging ); |
150 | 6 | if ( !isPomArtifact ) |
151 | |
{ |
152 | 4 | ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile ); |
153 | 4 | artifact.addMetadata( metadata ); |
154 | |
} |
155 | |
|
156 | 6 | if ( updateReleaseInfo ) |
157 | |
{ |
158 | 3 | artifact.setRelease( true ); |
159 | |
} |
160 | |
|
161 | |
try |
162 | |
{ |
163 | 6 | if ( isPomArtifact ) |
164 | |
{ |
165 | 2 | getDeployer().deploy( pomFile, artifact, repo, getLocalRepository() ); |
166 | |
} |
167 | |
else |
168 | |
{ |
169 | 4 | File file = artifact.getFile(); |
170 | |
|
171 | 4 | if ( file != null && !file.isDirectory() ) |
172 | |
{ |
173 | 3 | getDeployer().deploy( file, artifact, repo, getLocalRepository() ); |
174 | |
} |
175 | 1 | else if ( !attachedArtifacts.isEmpty() ) |
176 | |
{ |
177 | 0 | getLog().info( "No primary artifact to deploy, deploy attached artifacts instead." ); |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 1 | String message = "The packaging for this project did not assign a file to the build artifact"; |
182 | 1 | throw new MojoExecutionException( message ); |
183 | |
} |
184 | |
} |
185 | |
|
186 | 5 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
187 | |
{ |
188 | 1 | Artifact attached = ( Artifact ) i.next(); |
189 | |
|
190 | 1 | getDeployer().deploy( attached.getFile(), attached, repo, getLocalRepository() ); |
191 | 1 | } |
192 | |
} |
193 | 0 | catch ( ArtifactDeploymentException e ) |
194 | |
{ |
195 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
196 | 5 | } |
197 | 5 | } |
198 | |
|
199 | |
private ArtifactRepository getDeploymentRepository() |
200 | |
throws MojoExecutionException, MojoFailureException |
201 | |
{ |
202 | 6 | if ( deploymentRepository == null && altDeploymentRepository == null ) |
203 | |
{ |
204 | 0 | String msg = "Deployment failed: repository element was not specified in the pom inside" |
205 | |
+ " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter"; |
206 | |
|
207 | 0 | throw new MojoExecutionException( msg ); |
208 | |
} |
209 | |
|
210 | 6 | ArtifactRepository repo = null; |
211 | |
|
212 | 6 | if ( altDeploymentRepository != null ) |
213 | |
{ |
214 | 0 | getLog().info( "Using alternate deployment repository " + altDeploymentRepository ); |
215 | |
|
216 | 0 | Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository ); |
217 | |
|
218 | 0 | if ( !matcher.matches() ) |
219 | |
{ |
220 | 0 | throw new MojoFailureException( altDeploymentRepository, "Invalid syntax for repository.", |
221 | |
"Invalid syntax for alternative repository. Use \"id::layout::url\"." ); |
222 | |
} |
223 | |
else |
224 | |
{ |
225 | 0 | String id = matcher.group( 1 ).trim(); |
226 | 0 | String layout = matcher.group( 2 ).trim(); |
227 | 0 | String url = matcher.group( 3 ).trim(); |
228 | |
|
229 | |
ArtifactRepositoryLayout repoLayout; |
230 | |
try |
231 | |
{ |
232 | 0 | repoLayout = ( ArtifactRepositoryLayout ) container.lookup( ArtifactRepositoryLayout.ROLE, layout ); |
233 | |
} |
234 | 0 | catch ( ComponentLookupException e ) |
235 | |
{ |
236 | 0 | throw new MojoExecutionException( "Cannot find repository layout: " + layout, e ); |
237 | 0 | } |
238 | |
|
239 | 0 | repo = new DefaultArtifactRepository( id, url, repoLayout ); |
240 | |
} |
241 | |
} |
242 | |
|
243 | 6 | if ( repo == null ) |
244 | |
{ |
245 | 6 | repo = deploymentRepository; |
246 | |
} |
247 | |
|
248 | 6 | return repo; |
249 | |
} |
250 | |
|
251 | |
public void contextualize( Context context ) |
252 | |
throws ContextException |
253 | |
{ |
254 | 8 | this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
255 | 8 | } |
256 | |
} |