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 public class DeployMojo
54 extends AbstractDeployMojo
55 implements Contextualizable
56 {
57
58 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 if ( skip )
129 {
130 getLog().info( " skipping artifact deployement " );
131 return;
132 }
133
134 ArtifactRepository repo = getDeploymentRepository();
135
136 String protocol = repo.getProtocol();
137
138 if ( protocol.equalsIgnoreCase( "scp" ) )
139 {
140 File sshFile = new File( System.getProperty( "user.home" ), ".ssh" );
141
142 if ( !sshFile.exists() )
143 {
144 sshFile.mkdirs();
145 }
146 }
147
148
149 boolean isPomArtifact = "pom".equals( packaging );
150 if ( !isPomArtifact )
151 {
152 ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
153 artifact.addMetadata( metadata );
154 }
155
156 if ( updateReleaseInfo )
157 {
158 artifact.setRelease( true );
159 }
160
161 try
162 {
163 if ( isPomArtifact )
164 {
165 getDeployer().deploy( pomFile, artifact, repo, getLocalRepository() );
166 }
167 else
168 {
169 File file = artifact.getFile();
170
171 if ( file != null && !file.isDirectory() )
172 {
173 getDeployer().deploy( file, artifact, repo, getLocalRepository() );
174 }
175 else if ( !attachedArtifacts.isEmpty() )
176 {
177 getLog().info( "No primary artifact to deploy, deploy attached artifacts instead." );
178 }
179 else
180 {
181 String message = "The packaging for this project did not assign a file to the build artifact";
182 throw new MojoExecutionException( message );
183 }
184 }
185
186 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
187 {
188 Artifact attached = ( Artifact ) i.next();
189
190 getDeployer().deploy( attached.getFile(), attached, repo, getLocalRepository() );
191 }
192 }
193 catch ( ArtifactDeploymentException e )
194 {
195 throw new MojoExecutionException( e.getMessage(), e );
196 }
197 }
198
199 private ArtifactRepository getDeploymentRepository()
200 throws MojoExecutionException, MojoFailureException
201 {
202 if ( deploymentRepository == null && altDeploymentRepository == null )
203 {
204 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 throw new MojoExecutionException( msg );
208 }
209
210 ArtifactRepository repo = null;
211
212 if ( altDeploymentRepository != null )
213 {
214 getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
215
216 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
217
218 if ( !matcher.matches() )
219 {
220 throw new MojoFailureException( altDeploymentRepository, "Invalid syntax for repository.",
221 "Invalid syntax for alternative repository. Use \"id::layout::url\"." );
222 }
223 else
224 {
225 String id = matcher.group( 1 ).trim();
226 String layout = matcher.group( 2 ).trim();
227 String url = matcher.group( 3 ).trim();
228
229 ArtifactRepositoryLayout repoLayout;
230 try
231 {
232 repoLayout = ( ArtifactRepositoryLayout ) container.lookup( ArtifactRepositoryLayout.ROLE, layout );
233 }
234 catch ( ComponentLookupException e )
235 {
236 throw new MojoExecutionException( "Cannot find repository layout: " + layout, e );
237 }
238
239 repo = new DefaultArtifactRepository( id, url, repoLayout );
240 }
241 }
242
243 if ( repo == null )
244 {
245 repo = deploymentRepository;
246 }
247
248 return repo;
249 }
250
251 public void contextualize( Context context )
252 throws ContextException
253 {
254 this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
255 }
256 }