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