|
17 | 17 |
|
18 | 18 | package pl.project13.maven.git; |
19 | 19 |
|
| 20 | +import com.fasterxml.jackson.core.type.TypeReference; |
20 | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
21 | 22 | import com.google.common.annotations.VisibleForTesting; |
| 23 | +import com.google.common.base.Charsets; |
22 | 24 | import com.google.common.base.Function; |
23 | 25 | import com.google.common.base.Predicate; |
24 | 26 | import com.google.common.base.Predicates; |
25 | 27 | import com.google.common.collect.Lists; |
26 | 28 | import com.google.common.io.Closeables; |
27 | 29 | import com.google.common.io.Files; |
| 30 | + |
28 | 31 | import org.apache.maven.execution.MavenSession; |
29 | 32 | import org.apache.maven.plugin.AbstractMojo; |
30 | 33 | import org.apache.maven.plugin.MojoExecutionException; |
31 | 34 | import org.apache.maven.project.MavenProject; |
32 | 35 | import org.jetbrains.annotations.NotNull; |
33 | 36 | import org.jetbrains.annotations.Nullable; |
| 37 | + |
34 | 38 | import pl.project13.maven.git.log.LoggerBridge; |
35 | 39 | import pl.project13.maven.git.log.MavenLoggerBridge; |
36 | 40 | import pl.project13.maven.git.util.PropertyManager; |
|
40 | 44 | import java.text.SimpleDateFormat; |
41 | 45 | import java.util.Collections; |
42 | 46 | import java.util.Date; |
| 47 | +import java.util.HashMap; |
43 | 48 | import java.util.List; |
| 49 | +import java.util.Map; |
44 | 50 | import java.util.Properties; |
45 | 51 |
|
46 | 52 | /** |
@@ -355,7 +361,7 @@ public void execute() throws MojoExecutionException { |
355 | 361 | logProperties(properties); |
356 | 362 |
|
357 | 363 | if (generateGitPropertiesFile) { |
358 | | - generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename); |
| 364 | + maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename); |
359 | 365 | } |
360 | 366 |
|
361 | 367 | if (injectAllReactorProjects) { |
@@ -462,9 +468,9 @@ private boolean isOurProperty(@NotNull String keyString) { |
462 | 468 | } |
463 | 469 |
|
464 | 470 | void loadBuildTimeData(@NotNull Properties properties) { |
465 | | - Date commitDate = new Date(); |
| 471 | + Date buildDate = new Date(); |
466 | 472 | SimpleDateFormat smf = new SimpleDateFormat(dateFormat); |
467 | | - put(properties, BUILD_TIME, smf.format(commitDate)); |
| 473 | + put(properties, BUILD_TIME, smf.format(buildDate)); |
468 | 474 | } |
469 | 475 |
|
470 | 476 | void loadShortDescribe(@NotNull Properties properties) { |
@@ -522,26 +528,58 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj |
522 | 528 | jGitProvider.loadGitData(properties); |
523 | 529 | } |
524 | 530 |
|
525 | | - void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException { |
526 | | - Writer outputWriter = null; |
527 | | - File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename); |
528 | | - try { |
529 | | - Files.createParentDirs(gitPropsFile); |
| 531 | + void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base, String propertiesFilename) throws IOException { |
| 532 | + final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename); |
| 533 | + final boolean isJsonFormat = "json".equalsIgnoreCase( format ); |
530 | 534 |
|
531 | | - outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8")); |
532 | | - if ("json".equalsIgnoreCase(format)) { |
533 | | - log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
534 | | - ObjectMapper mapper = new ObjectMapper(); |
535 | | - mapper.writeValue(outputWriter, properties); |
536 | | - } else { |
537 | | - log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
538 | | - properties.store(outputWriter, "Generated by Git-Commit-Id-Plugin"); |
| 535 | + boolean shouldGenerate = true; |
| 536 | + |
| 537 | + if (gitPropsFile.exists( )) { |
| 538 | + final Properties persistedProperties; |
| 539 | + |
| 540 | + if (isJsonFormat) { |
| 541 | + log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
| 542 | + |
| 543 | + persistedProperties = readJsonProperties( gitPropsFile ); |
539 | 544 | } |
| 545 | + else { |
| 546 | + log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
| 547 | + |
| 548 | + persistedProperties = readProperties( gitPropsFile ); |
| 549 | + } |
| 550 | + |
| 551 | + final Properties propertiesCopy = (Properties) localProperties.clone( ); |
540 | 552 |
|
541 | | - } catch (IOException ex) { |
542 | | - throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex); |
543 | | - } finally { |
544 | | - Closeables.closeQuietly(outputWriter); |
| 553 | + final String buildTimeProperty = prefixDot + BUILD_TIME; |
| 554 | + |
| 555 | + propertiesCopy.remove( buildTimeProperty ); |
| 556 | + persistedProperties.remove( buildTimeProperty ); |
| 557 | + |
| 558 | + shouldGenerate = ! propertiesCopy.equals( persistedProperties ); |
| 559 | + } |
| 560 | + |
| 561 | + if (shouldGenerate) { |
| 562 | + Files.createParentDirs(gitPropsFile); |
| 563 | + Writer outputWriter = null; |
| 564 | + |
| 565 | + try { |
| 566 | + outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charsets.UTF_8); |
| 567 | + if (isJsonFormat) { |
| 568 | + log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
| 569 | + ObjectMapper mapper = new ObjectMapper(); |
| 570 | + mapper.writeValue(outputWriter, localProperties); |
| 571 | + } else { |
| 572 | + log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); |
| 573 | + localProperties.store(outputWriter, "Generated by Git-Commit-Id-Plugin"); |
| 574 | + } |
| 575 | + } catch (final IOException ex) { |
| 576 | + throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex); |
| 577 | + } finally { |
| 578 | + Closeables.closeQuietly(outputWriter); |
| 579 | + } |
| 580 | + } |
| 581 | + else { |
| 582 | + log("Properties file [", gitPropsFile.getAbsolutePath(), "] is up-to-date (for module ", project.getName(), ")..."); |
545 | 583 | } |
546 | 584 | } |
547 | 585 |
|
@@ -579,6 +617,67 @@ private boolean directoryDoesNotExits(File fileLocation) { |
579 | 617 | return !directoryExists(fileLocation); |
580 | 618 | } |
581 | 619 |
|
| 620 | + @SuppressWarnings( "resource" ) |
| 621 | + static Properties readJsonProperties(@NotNull File jsonFile) { |
| 622 | + final HashMap<String, Object> propertiesMap; |
| 623 | + |
| 624 | + { |
| 625 | + Closeable closeable = null; |
| 626 | + |
| 627 | + try { |
| 628 | + final FileInputStream fis = new FileInputStream(jsonFile); |
| 629 | + closeable = fis; |
| 630 | + |
| 631 | + final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8); |
| 632 | + closeable = reader; |
| 633 | + |
| 634 | + final ObjectMapper mapper = new ObjectMapper(); |
| 635 | + final TypeReference<HashMap<String,Object>> mapTypeRef = |
| 636 | + new TypeReference<HashMap<String,Object>>() {}; |
| 637 | + |
| 638 | + propertiesMap = mapper.readValue(reader, mapTypeRef); |
| 639 | + } catch (final Exception ex) { |
| 640 | + throw new RuntimeException("Cannot read from git properties file: " + jsonFile, ex); |
| 641 | + } finally { |
| 642 | + Closeables.closeQuietly(closeable); |
| 643 | + } |
| 644 | + } |
| 645 | + |
| 646 | + final Properties retVal = new Properties( ); |
| 647 | + |
| 648 | + for(final Map.Entry<String, Object> entry : propertiesMap.entrySet()) { |
| 649 | + retVal.setProperty(entry.getKey(), String.valueOf(entry.getValue())); |
| 650 | + } |
| 651 | + |
| 652 | + return retVal; |
| 653 | + } |
| 654 | + |
| 655 | + @SuppressWarnings( "resource" ) |
| 656 | + static Properties readProperties(@NotNull File propertiesFile) |
| 657 | + { |
| 658 | + Closeable closeable = null; |
| 659 | + |
| 660 | + try { |
| 661 | + final FileInputStream fis = new FileInputStream(propertiesFile); |
| 662 | + closeable = fis; |
| 663 | + |
| 664 | + final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8); |
| 665 | + closeable = reader; |
| 666 | + |
| 667 | + final Properties retVal = new Properties( ); |
| 668 | + |
| 669 | + retVal.load(reader); |
| 670 | + |
| 671 | + return retVal; |
| 672 | + } |
| 673 | + catch (final Exception ex) { |
| 674 | + throw new RuntimeException("Cannot read from git properties file: " + propertiesFile, ex); |
| 675 | + } |
| 676 | + finally { |
| 677 | + Closeables.closeQuietly(closeable); |
| 678 | + } |
| 679 | + } |
| 680 | + |
582 | 681 | // SETTERS FOR TESTS ---------------------------------------------------- |
583 | 682 |
|
584 | 683 | public void setFormat(String format) { |
|
0 commit comments