-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAbstractGitflowBasedRepositoryMojo.java
423 lines (366 loc) · 18.2 KB
/
AbstractGitflowBasedRepositoryMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package com.e_gineering.maven.gitflowhelper;
import static java.nio.charset.StandardCharsets.UTF_8;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.shared.utils.StringUtils;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.AuthenticationContext;
import org.eclipse.aether.repository.AuthenticationSelector;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Common configuration and plumbing (support methods) for Repository operations on Gitflow Mojo.
*/
abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflowBranchMojo {
private static final String CATALOG_HEADER = "[artifacts]";
private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundException {
Objects.requireNonNull(catalog, "catalog must not be null");
return new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), UTF_8));
}
@Parameter(property = "releaseDeploymentRepositoryId", required = true)
String releaseDeploymentRepository;
@Parameter(property = "stageDeploymentRepositoryId", required = true)
String stageDeploymentRepository;
@Parameter(property = "snapshotDeploymentRepositoryId", required = true)
String snapshotDeploymentRepository;
@Parameter(property = "otherDeployBranchPattern", required = false)
String otherDeployBranchPattern;
@Parameter(defaultValue = "+", required = true)
String otherBranchVersionDelimiter;
@Parameter(defaultValue = "${repositorySystemSession}", required = true)
RepositorySystemSession repositorySystemSession;
@Parameter(defaultValue = "${project.build.directory}", required = true)
File buildDirectory;
@Component
private RepositorySystem repositorySystem;
@Component
private EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;
@Component
private MavenProjectHelper projectHelper;
/**
* Creates a Maven ArtifactRepository for targeting deployments.
*
* @param id the repository identifier
* @return the resolved repository
*
* @throws MojoFailureException if the repository id is not defined.
*/
ArtifactRepository getDeploymentRepository(final String id) throws MojoFailureException {
return getDeploymentRepositoryOpt(id).orElseThrow(() -> new MojoFailureException("No Repository with id `" + id + "` is defined."));
}
/**
* Get an repository by the repository ID.
*
* @param id the repository identifier
* @return optional to the repository (never @Code{null})
*/
Optional<ArtifactRepository> getDeploymentRepositoryOpt(String id) {
Objects.requireNonNull(id, "A repository id must be specified.");
Optional<ArtifactRepository> repo = project.getRemoteArtifactRepositories().stream().filter(r -> r.getId().equals(id)).findFirst();
if (repo.isPresent()) {
return repo;
}
Optional<ArtifactRepository> mirroredRepo = project.getRemoteArtifactRepositories().stream()
.flatMap(r -> r.getMirroredRepositories().stream()).filter(r -> r.getId().equals(id)).findFirst();
if(mirroredRepo.isPresent()) {
ArtifactRepository artifactRepository = mirroredRepo.get();
if(artifactRepository.getAuthentication() == null) {
artifactRepository.setAuthentication(getAuthentication(artifactRepository));
}
return Optional.of(artifactRepository);
}
return Optional.empty();
}
/**
* Get the authentication object for an artifact repository
* @param repository the artifact repository
* @return der authentication object or @Code{null} when no authentication was found
*/
private org.apache.maven.artifact.repository.Authentication getAuthentication( ArtifactRepository repository )
{
AuthenticationSelector selector = repositorySystemSession.getAuthenticationSelector();
if (selector == null)
{
return null;
}
RemoteRepository repo = RepositoryUtils.toRepo(repository);
org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
if(auth == null)
{
return null;
}
RemoteRepository repoWithAuth = new RemoteRepository.Builder(repo).setAuthentication(auth).build();
AuthenticationContext authCtx = AuthenticationContext.forRepository(repositorySystemSession, repoWithAuth);
if(authCtx == null) {
return null;
}
org.apache.maven.artifact.repository.Authentication result =
new org.apache.maven.artifact.repository.Authentication(authCtx.get(AuthenticationContext.USERNAME),
authCtx.get(AuthenticationContext.PASSWORD));
result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
authCtx.close();
return result;
}
/**
* Creates and attaches an artifact containing a list of attached artifacts, each line in the file contains
* group:artifact:type:classifier:version
*/
void attachArtifactCatalog() throws MojoExecutionException {
getLog().info("Cataloging Artifacts for promotion & reattachment: " + project.getBuild().getDirectory());
File catalog = new File(buildDirectory, project.getArtifact().getArtifactId() + ".txt");
if (!catalog.delete()) {
getLog().debug("Failed to remove catalog file: " + catalog);
}
if (!buildDirectory.mkdirs()) {
getLog().debug("Failed to create build directory: " + buildDirectory);
}
PrintWriter writer = null;
try {
writer = newPrintWriter(catalog);
// add catalog header, ensuring that no zero-byte catalog is created
writer.println(CATALOG_HEADER);
if (hasCataloguableArtifacts()) {
if (hasFile(project.getArtifact())) {
catalogArtifact(writer, project.getArtifact());
} else {
getLog().info("No primary artifact to catalog, cataloging attached artifacts instead.");
}
// Iterate the attached artifacts.
for (Artifact artifact : project.getAttachedArtifacts()) {
catalogArtifact(writer, artifact);
}
} else {
getLog().info(
"No artifacts were catalogued."
);
}
getLog().info("Attaching catalog artifact: " + catalog);
projectHelper.attachArtifact(project, "txt", "catalog", catalog);
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to create catalog of artifacts", ioe);
} finally {
if (writer != null) {
writer.close();
}
}
}
private void catalogArtifact(PrintWriter writer, Artifact artifact) {
String coords = getCoordinates(artifact);
getLog().info("Cataloging: " + coords);
writer.println(coords);
}
/**
* Resolves artifacts from the given sourceRepository by first resolving and processing the artifact catalog
* created by the promote-master mojo.
*
* @param sourceRepository An ArtifactRepository to use as a RemoteRepository when supplied. Otherwise, only the local repository will be used.
* @param disableLocal if artifacts should be downloaded from a remote to an isolated repository, bypassing the 'standard' maven local repo.
*
* @throws MojoExecutionException for any unhandled maven exception
*/
void attachExistingArtifacts(@Nullable final String sourceRepository, final boolean disableLocal)
throws MojoExecutionException {
List<ArtifactRepository> remoteArtifactRepositories = new ArrayList<>();
Optional<ArtifactRepository> repo = getDeploymentRepositoryOpt(sourceRepository);
if (repo.isPresent()) {
remoteArtifactRepositories.add(repo.get());
} else {
if (disableLocal) {
throw new MojoExecutionException("Cannot resolve artifacts from 'null' repository if the local repository is also disabled.");
}
getLog().debug("Resolving existing artifacts from local repository only.");
}
List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(remoteArtifactRepositories);
// A place to store our resolved files...
List<ArtifactResult> resolvedArtifacts = new ArrayList<>();
// Use a customized repository session, setup to force a few behaviors we like.
DefaultRepositorySystemSession tempSession = new DefaultRepositorySystemSession(repositorySystemSession);
tempSession.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
File tempRepo = null;
if (disableLocal) {
getLog().info("Disabling local repository @ " + tempSession.getLocalRepository().getBasedir());
try {
tempRepo = Files.createTempDirectory("gitflow-helper-maven-plugin-repo").toFile();
getLog().info("Using temporary local repository @ " + tempRepo.getAbsolutePath());
tempSession.setLocalRepositoryManager(localRepositoryManagerFactory.newInstance(tempSession, new LocalRepository(tempRepo)));
} catch (Exception ex) {
getLog().warn("Failed to disable local repository path.", ex);
}
}
tempSession.setReadOnly();
List<ArtifactRequest> requiredArtifacts = new ArrayList<>();
// Locate our text catalog classifier file. :-)
BufferedReader reader = null;
try {
DefaultArtifact artifact = new DefaultArtifact(
project.getGroupId(), project.getArtifactId(), "catalog", "txt", project.getVersion()
);
ArtifactRequest request = new ArtifactRequest(artifact, remoteRepositories, null);
ArtifactResult catalogResult = repositorySystem.resolveArtifact(tempSession, request);
resolvedArtifacts.add(catalogResult);
if (catalogResult.isResolved()) {
// Read the file line by line...
FileInputStream fis = new FileInputStream(catalogResult.getArtifact().getFile());
InputStreamReader isr = new InputStreamReader(fis, UTF_8);
reader = new BufferedReader(isr);
String coords;
boolean firstLine = true;
while ((coords = reader.readLine()) != null) {
coords = coords.trim();
// test for catalog header for bacvkwards compatibility
if (!coords.isEmpty() && !(firstLine && CATALOG_HEADER.equals(coords))) {
// should be a reifiable GAV coordinate therefore add a new ArtifactRequest
requiredArtifacts.add(
new ArtifactRequest(new DefaultArtifact(coords), remoteRepositories, null)
);
}
firstLine = false;
}
}
} catch (ArtifactResolutionException are) {
throw new MojoExecutionException("Could not locate artifact catalog in remote repository.", are);
} catch (IOException ioe) {
throw new MojoExecutionException("Could not read artifact catalog", ioe);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
// Resolve the artifacts from the catalog (if there are any)
try {
resolvedArtifacts.addAll(repositorySystem.resolveArtifacts(tempSession, requiredArtifacts));
} catch (ArtifactResolutionException are) {
throw new MojoExecutionException("Failed to resolve the required project files from repository: " + sourceRepository, are);
}
// Get the current build artifact coordinates, so that we replace rather than re-attach.
String projectArtifactCoordinates = getCoordinates(project.getArtifact());
getLog().debug("Current Project Coordinates: " + projectArtifactCoordinates);
// For each artifactResult, copy it to the build directory,
// update the resolved artifact data to point to the new file.
// Then either set the project artifact to point to the file in the build directory, or attach the artifact.
for (ArtifactResult artifactResult : resolvedArtifacts) {
try {
FileUtils.copyFileToDirectory(artifactResult.getArtifact().getFile(), buildDirectory);
artifactResult.setArtifact(artifactResult.getArtifact().setFile(new File(buildDirectory, artifactResult.getArtifact().getFile().getName())));
if (getCoordinates(artifactResult).equals(projectArtifactCoordinates)) {
getLog().debug(" Setting primary artifact: " + artifactResult.getArtifact().getFile());
project.getArtifact().setFile(artifactResult.getArtifact().getFile());
} else {
getLog().debug(
" Attaching artifact: " + getCoordinates(artifactResult) + " "
+ artifactResult.getArtifact().getFile());
projectHelper.attachArtifact(project, artifactResult.getArtifact().getExtension(), artifactResult.getArtifact().getClassifier(), artifactResult.getArtifact().getFile());
}
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to copy resolved artifact to target directory.", ioe);
}
}
// Restore the local repository, again using reflection.
if (disableLocal) {
if (tempRepo != null) {
try {
FileUtils.deleteDirectory(tempRepo);
} catch (IOException e) {
getLog().warn("Failed to cleanup temporary repository directory: " + tempRepo);
}
}
}
}
/**
* Returns true if the project has any artifacts to be catalogued.
*
* @return true if the primary artifact has a real file part as defined by {@link #hasFile(Artifact)}, or if any
* attached artifacts are present.
*/
private boolean hasCataloguableArtifacts() {
return hasFile(project.getArtifact()) || !project.getAttachedArtifacts().isEmpty();
}
/**
* Returns true if the artifact describes a real file.
*
* @param artifact the possibly null artifact
* @return true if the artifact describes a real file, false otherwise
*/
private boolean hasFile(@Nullable Artifact artifact) {
return artifact != null
&& project.getArtifact().getFile() != null
&& project.getArtifact().getFile().exists()
&& project.getArtifact().getFile().isFile();
}
private String getCoordinates(ArtifactResult artifactResult) {
return getCoordinates(
emptyToNull(artifactResult.getArtifact().getGroupId()),
emptyToNull(artifactResult.getArtifact().getArtifactId()),
emptyToNull(artifactResult.getArtifact().getBaseVersion()),
emptyToNull(artifactResult.getArtifact().getExtension()),
emptyToNull(artifactResult.getArtifact().getClassifier())
);
}
private static String emptyToNull(final String s) {
return StringUtils.isBlank(s) ? null : s;
}
private String getCoordinates(Artifact artifact) {
getLog().debug(" Encoding Coordinates For: " + artifact);
// Get the extension according to the artifact type.
String extension = artifact.getArtifactHandler().getExtension();
return getCoordinates(
artifact.getGroupId(),
artifact.getArtifactId(),
project.getVersion(),
extension, artifact.hasClassifier() ? artifact.getClassifier() : null
);
}
private String getCoordinates(String groupId,
String artifactId,
String version,
@Nullable String extension,
@Nullable String classifier) {
Objects.requireNonNull(groupId, "groupId must not be null");
Objects.requireNonNull(artifactId, "artifactId must not be null");
Objects.requireNonNull(version, "version must not be null");
StringBuilder result = new StringBuilder();
for (String s : new String[]{groupId, artifactId, extension, classifier, version}) {
if (s != null) {
if (result.length() > 0) {
result.append(":");
}
result.append(s);
}
}
return result.toString();
}
}