-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathGitCommitIdMojo.java
More file actions
591 lines (509 loc) · 20 KB
/
GitCommitIdMojo.java
File metadata and controls
591 lines (509 loc) · 20 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
*
* git-commit-id-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.git;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.log.MavenLoggerBridge;
import pl.project13.maven.git.util.PropertyManager;
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* Goal which puts git build-time information into property files or maven's properties.
*
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
* @goal revision
* @phase initialize
* @requiresProject
* @threadSafe true
* @since 1.0
*/
@SuppressWarnings({"JavaDoc"})
public class GitCommitIdMojo extends AbstractMojo {
// these properties will be exposed to maven
public static final String BRANCH = "branch";
public static final String COMMIT_ID = "commit.id";
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
public static final String FILES_DIRTY = "commit.files.dirty";
public static final String COMMIT_DESCRIBE = "commit.id.describe";
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.describe-short";
public static final String BUILD_AUTHOR_NAME = "build.user.name";
public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
public static final String BUILD_TIME = "build.time";
public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
public static final String COMMIT_MESSAGE_FULL = "commit.message.full";
public static final String COMMIT_MESSAGE_SHORT = "commit.message.short";
public static final String COMMIT_TIME = "commit.time";
public static final String REMOTE_ORIGIN_URL = "remote.origin.url";
public static final String TAGS = "tags";
/**
* The maven project.
*
* @parameter property="project"
* @readonly
*/
@SuppressWarnings("UnusedDeclaration")
MavenProject project;
/**
* Contains the full list of projects in the reactor.
*
* @parameter property="reactorProjects"
* @readonly
*/
@SuppressWarnings("UnusedDeclaration")
private List<MavenProject> reactorProjects;
/**
* Tell git-commit-id to inject the git properties into all
* reactor projects not just the current one.
*
* For details about why you might want to skip this, read this issue: https://github.com/ktoso/maven-git-commit-id-plugin/pull/65
* Basically, injecting into all projects may slow down the build and you don't always need this feature.
*
* @parameter default-value="true"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean injectAllReactorProjects;
/**
* Specifies whether the goal runs in verbose mode.
* To be more specific, this means more info being printed out while scanning for paths and also
* it will make git-commit-id "eat it's own dog food" :-)
*
* @parameter default-value="false"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean verbose;
/**
* Specifies whether the execution in pom projects should be skipped.
* Override this value to false if you want to force the plugin to run on 'pom' packaged projects.
*
* @parameter parameter="git.skipPoms" default-value="true"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean skipPoms;
/**
* Specifies whether plugin should generate properties file.
* By default it will not generate any additional file,
* and only add properties to maven project's properties for further filtering
*
* If set to "true" properties will be fully generated with no placeholders inside.
*
* @parameter default-value="false"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean generateGitPropertiesFile;
/**
* Decide where to generate the git.properties file. By default, the ${project.build.outputDirectory}/git.properties
* file will be updated - of course you must first set generateGitPropertiesFile = true to force git-commit-id
* into generateFile mode.
*
* The path here is relative to your projects src directory.
*
* @parameter default-value="${project.build.outputDirectory}/git.properties"
*/
@SuppressWarnings("UnusedDeclaration")
private String generateGitPropertiesFilename;
/**
* The root directory of the repository we want to check
*
* @parameter default-value="${project.basedir}/.git"
*/
@SuppressWarnings("UnusedDeclaration")
private File dotGitDirectory;
/**
* Configuration for the <pre>git-describe</pre> command.
* You can modify the dirty marker, abbrev length and other options here.
*
* If not specified, default values will be used.
*
* @parameter
*/
@SuppressWarnings("UnusedDeclaration")
private GitDescribeConfig gitDescribe;
/**
* <p>
* Configure the "git.commit.id.abbrev" property to be at least of length N.
* N must be in the range of 2 to 40 (inclusive), other values will result in an Exception.
* </p>
*
* <p>
* An Abbreviated commit is a shorter version of the commit id, it is guaranteed to be unique though.
* To keep this contract, the plugin may decide to print an abbrev version that is longer than the value specified here.
* </p>
*
* <b>Example:</b>
* <p>
* You have a very big repository, yet you set this value to 2. It's very probable that you'll end up getting a 4 or 7 char
* long abbrev version of the commit id. If your repository, on the other hand, has just 4 commits, you'll probably get a 2 char long abbrev.
* </p>
*
* @parameter default-value=7
*/
@SuppressWarnings("UnusedDeclaration")
private int abbrevLength;
/**
* The format to save properties in. Valid options are "properties" (default) and "json".
*
* @parameter default-value="properties"
*/
@SuppressWarnings("UnusedDeclaration")
private String format;
/**
* The prefix to expose the properties on, for example 'git' would allow you to access '${git.branch}'
*
* @parameter default-value="git"
*/
@SuppressWarnings("UnusedDeclaration")
private String prefix;
private String prefixDot = "";
/**
* The date format to be used for any dates exported by this plugin.
* It should be a valid SimpleDateFormat string.
*
* @parameter default-value="dd.MM.yyyy '@' HH:mm:ss z"
*/
@SuppressWarnings("UnusedDeclaration")
private String dateFormat;
/**
* Specifies whether the plugin should fail if it can't find the .git directory. The default
* value is true.
*
* @parameter default-value="true"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean failOnNoGitDirectory;
/**
* By default the plugin will fail the build if unable to obtain enough data for a complete run,
* if you don't care about this - for example it's not needed during your CI builds and the CI server does weird
* things to the repository, you may want to set this value to false.
*
* Setting this value to `false`, causes the plugin to gracefully tell you "I did my best" and abort it's execution
* if unable to obtain git meta data - yet the build will continue to run (without failing).
*
* See https://github.com/ktoso/maven-git-commit-id-plugin/issues/63 for a rationale behing this flag.
*
* @parameter default-value="true"
*/
@SuppressWarnings("UnusedDeclaration")
private boolean failOnUnableToExtractRepoInfo;
/**
* By default the plugin will use a jgit implementation as a source of a information about the repository. You can
* use a native GIT executable to fetch information about the repository, witch is in most cases faster but requires
* a git executable to be installed in system.
*
* @parameter default-value="false"
* @since 2.1.9
*/
@SuppressWarnings("UnusedDeclaration")
private boolean useNativeGit;
/**
* Skip the plugin execution.
*
* @parameter default-value="false"
* @since 2.1.8
*/
@SuppressWarnings("UnusedDeclaration")
private boolean skip = false;
/**
* Can be used to exclude certain properties from being emited into the resulting file.
* May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?),
* or the email of the committer etc.
*
* Each value may be globbing, that is, you can write {@code git.commit.user.*} to exclude both, the {@code name},
* as well as {@code email} properties from being emitted into the resulting files.
*
* Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}).
*
* @parameter
* @since 2.1.9
*/
@SuppressWarnings("UnusedDeclaration")
private List<String> excludeProperties = Collections.emptyList();
/**
* The properties we store our data in and then expose them
*/
private Properties properties;
boolean runningTests = false;
@NotNull
LoggerBridge loggerBridge = new MavenLoggerBridge(getLog(), true);
public void execute() throws MojoExecutionException {
// Set the verbose setting now it should be correctly loaded from maven.
loggerBridge.setVerbose(verbose);
if (skip) {
log("skip is true, return");
return;
}
if (isPomProject(project) && skipPoms) {
log("isPomProject is true and skipPoms is true, return");
return;
}
dotGitDirectory = lookupGitDirectory();
throwWhenRequiredDirectoryNotFound(dotGitDirectory, failOnNoGitDirectory, ".git directory could not be found! Please specify a valid [dotGitDirectory] in your pom.xml");
if (dotGitDirectory != null) {
log("dotGitDirectory", dotGitDirectory.getAbsolutePath());
} else {
log("dotGitDirectory is null, aborting execution!");
return;
}
try {
properties = initProperties();
String trimmedPrefix = prefix.trim();
prefixDot = trimmedPrefix.equals("") ? "" : trimmedPrefix + ".";
loadGitData(properties);
loadBuildTimeData(properties);
loadShortDescribe(properties);
filterNot(properties, excludeProperties);
logProperties(properties);
if (generateGitPropertiesFile) {
generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
}
if (injectAllReactorProjects) {
appendPropertiesToReactorProjects(properties);
}
} catch (Exception e) {
e.printStackTrace();
handlePluginFailure(e);
}
}
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
if (exclusions == null) {
return;
}
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
@Override
public Predicate<CharSequence> apply(String exclude) {
return Predicates.containsPattern(exclude);
}
});
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
for (Predicate<CharSequence> predicate : excludePredicates) {
shouldExclude = Predicates.or(shouldExclude, predicate);
}
for (String key : properties.stringPropertyNames()) {
if (shouldExclude.apply(key)) {
loggerBridge.debug("shouldExclude.apply(" + key + ") = " + shouldExclude.apply(key));
properties.remove(key);
}
}
}
/**
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
* If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
*
* @throws MojoExecutionException which will be should be throw within an MojoException in case the
* {@code failOnUnableToExtractRepoInfo} setting was set to true.
*/
private void handlePluginFailure(Exception e) throws MojoExecutionException {
if (failOnUnableToExtractRepoInfo) {
throw new MojoExecutionException("Could not complete Mojo execution...", e);
} else {
loggerBridge.error(e.getMessage(), com.google.common.base.Throwables.getStackTraceAsString(e));
}
}
private void appendPropertiesToReactorProjects(@NotNull Properties properties) {
for (MavenProject mavenProject : reactorProjects) {
Properties mavenProperties = mavenProject.getProperties();
log(mavenProject.getName(), "] project", mavenProject.getName());
for (Object key : properties.keySet()) {
mavenProperties.put(key, properties.get(key));
}
}
}
private void throwWhenRequiredDirectoryNotFound(File dotGitDirectory, Boolean required, String message) throws MojoExecutionException {
if (required && directoryDoesNotExits(dotGitDirectory)) {
throw new MojoExecutionException(message);
}
}
/**
* Find the git directory of the currently used project.
* If it's not already specified, this method will try to find it.
*
* @return the File representation of the .git directory
*/
@VisibleForTesting File lookupGitDirectory() throws MojoExecutionException {
return new GitDirLocator(project, reactorProjects).lookupGitDirectory(dotGitDirectory);
}
private Properties initProperties() throws MojoExecutionException {
if (generateGitPropertiesFile) {
return properties = new Properties();
} else if (!runningTests) {
return properties = project.getProperties();
} else {
return properties = new Properties(); // that's ok for unit tests
}
}
private void logProperties(@NotNull Properties properties) {
for (Object key : properties.keySet()) {
String keyString = key.toString();
if (isOurProperty(keyString)) {
log("found property", keyString);
}
}
}
private boolean isOurProperty(@NotNull String keyString) {
return keyString.startsWith(prefixDot);
}
void loadBuildTimeData(@NotNull Properties properties) {
Date commitDate = new Date();
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
put(properties, BUILD_TIME, smf.format(commitDate));
}
void loadShortDescribe(@NotNull Properties properties) {
//removes git hash part from describe
String commitDescribe = properties.getProperty(prefixDot + COMMIT_DESCRIBE);
if (commitDescribe != null) {
int startPos = commitDescribe.indexOf("-g");
if (startPos > 0) {
String commitShortDescribe;
int endPos = commitDescribe.indexOf('-', startPos + 1);
if (endPos < 0) {
commitShortDescribe = commitDescribe.substring(0, startPos);
} else {
commitShortDescribe = commitDescribe.substring(0, startPos) + commitDescribe.substring(endPos);
}
put(properties, COMMIT_SHORT_DESCRIBE, commitShortDescribe);
} else {
put(properties, COMMIT_SHORT_DESCRIBE, commitDescribe);
}
}
}
void loadGitData(@NotNull Properties properties) throws IOException, MojoExecutionException {
if (useNativeGit) {
loadGitDataWithNativeGit(properties);
} else {
loadGitDataWithJGit(properties);
}
}
void loadGitDataWithNativeGit(@NotNull Properties properties) throws IOException, MojoExecutionException {
File basedir = project.getBasedir().getCanonicalFile();
NativeGitProvider nativeGitProvider = NativeGitProvider
.on(basedir)
.withLoggerBridge(loggerBridge)
.setVerbose(verbose)
.setPrefixDot(prefixDot)
.setAbbrevLength(abbrevLength)
.setDateFormat(dateFormat)
.setGitDescribe(gitDescribe);
nativeGitProvider.loadGitData(properties);
}
void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, MojoExecutionException {
JGitProvider jGitProvider = JGitProvider
.on(dotGitDirectory)
.withLoggerBridge(loggerBridge)
.setVerbose(verbose)
.setPrefixDot(prefixDot)
.setAbbrevLength(abbrevLength)
.setDateFormat(dateFormat)
.setGitDescribe(gitDescribe);
jGitProvider.loadGitData(properties);
}
void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
Writer outputWriter = null;
File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
try {
Files.createParentDirs(gitPropsFile);
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8"));
if ("json".equalsIgnoreCase(format)) {
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(outputWriter, properties);
} else {
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
properties.store(outputWriter, "Generated by Git-Commit-Id-Plugin");
}
} catch (IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
} finally {
Closeables.closeQuietly(outputWriter);
}
}
@VisibleForTesting File craftPropertiesOutputFile(File base, String propertiesFilename) {
File returnPath = new File(base, propertiesFilename);
File currentPropertiesFilepath = new File(propertiesFilename);
if (currentPropertiesFilepath.isAbsolute()) {
returnPath = currentPropertiesFilepath;
}
return returnPath;
}
boolean isPomProject(@NotNull MavenProject project) {
return project.getPackaging().equalsIgnoreCase("pom");
}
private void put(@NotNull Properties properties, String key, String value) {
String keyWithPrefix = prefixDot + key;
log(keyWithPrefix, value);
PropertyManager.putWithoutPrefix(properties, keyWithPrefix, value);
}
void log(String... parts) {
loggerBridge.log((Object[]) parts);
}
private boolean directoryExists(@Nullable File fileLocation) {
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
}
private boolean directoryDoesNotExits(File fileLocation) {
return !directoryExists(fileLocation);
}
// SETTERS FOR TESTS ----------------------------------------------------
public void setFormat(String format) {
this.format = format;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
public void setDotGitDirectory(File dotGitDirectory) {
this.dotGitDirectory = dotGitDirectory;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public Properties getProperties() {
return properties;
}
public void setGitDescribe(GitDescribeConfig gitDescribe) {
this.gitDescribe = gitDescribe;
}
public void setAbbrevLength(int abbrevLength) {
this.abbrevLength = abbrevLength;
}
public void setExcludeProperties(List<String> excludeProperties) {
this.excludeProperties = excludeProperties;
}
public void useNativeGit(boolean useNativeGit) {
this.useNativeGit = useNativeGit;
}
public LoggerBridge getLoggerBridge() {
return loggerBridge;
}
}