Skip to content

Commit 172db01

Browse files
committed
Merge pull request #152 from apogrebnyak/feature/issue-51-update-properties-file-when-needed-only
Feature/issue 51 update properties file when needed only
2 parents ef5fc28 + 7dcbbf6 commit 172db01

File tree

1 file changed

+136
-21
lines changed

1 file changed

+136
-21
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 136 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import com.fasterxml.jackson.core.type.TypeReference;
2021
import com.fasterxml.jackson.databind.ObjectMapper;
2122
import com.google.common.annotations.VisibleForTesting;
23+
import com.google.common.base.Charsets;
2224
import com.google.common.base.Function;
2325
import com.google.common.base.Predicate;
2426
import com.google.common.base.Predicates;
2527
import com.google.common.collect.Lists;
2628
import com.google.common.io.Closeables;
2729
import com.google.common.io.Files;
30+
2831
import org.apache.maven.execution.MavenSession;
2932
import org.apache.maven.plugin.AbstractMojo;
3033
import org.apache.maven.plugin.MojoExecutionException;
3134
import org.apache.maven.project.MavenProject;
3235
import org.jetbrains.annotations.NotNull;
3336
import org.jetbrains.annotations.Nullable;
37+
3438
import pl.project13.maven.git.log.LoggerBridge;
3539
import pl.project13.maven.git.log.MavenLoggerBridge;
3640
import pl.project13.maven.git.util.PropertyManager;
3741

3842
import java.io.*;
39-
import java.nio.charset.Charset;
4043
import java.text.SimpleDateFormat;
4144
import java.util.Collections;
4245
import java.util.Date;
46+
import java.util.HashMap;
4347
import java.util.List;
48+
import java.util.Map;
4449
import java.util.Properties;
4550

4651
/**
@@ -355,7 +360,7 @@ public void execute() throws MojoExecutionException {
355360
logProperties(properties);
356361

357362
if (generateGitPropertiesFile) {
358-
generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
363+
maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
359364
}
360365

361366
if (injectAllReactorProjects) {
@@ -462,9 +467,9 @@ private boolean isOurProperty(@NotNull String keyString) {
462467
}
463468

464469
void loadBuildTimeData(@NotNull Properties properties) {
465-
Date commitDate = new Date();
470+
Date buildDate = new Date();
466471
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
467-
put(properties, BUILD_TIME, smf.format(commitDate));
472+
put(properties, BUILD_TIME, smf.format(buildDate));
468473
}
469474

470475
void loadShortDescribe(@NotNull Properties properties) {
@@ -522,26 +527,65 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
522527
jGitProvider.loadGitData(properties);
523528
}
524529

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);
530+
void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base, String propertiesFilename) throws IOException {
531+
final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
532+
final boolean isJsonFormat = "json".equalsIgnoreCase( format );
530533

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");
534+
boolean shouldGenerate = true;
535+
536+
if (gitPropsFile.exists( )) {
537+
final Properties persistedProperties;
538+
539+
try {
540+
if (isJsonFormat) {
541+
log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
542+
543+
persistedProperties = readJsonProperties( gitPropsFile );
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( );
552+
553+
final String buildTimeProperty = prefixDot + BUILD_TIME;
554+
555+
propertiesCopy.remove( buildTimeProperty );
556+
persistedProperties.remove( buildTimeProperty );
557+
558+
shouldGenerate = ! propertiesCopy.equals( persistedProperties );
559+
}
560+
catch ( CannotReadFileException ex ) {
561+
// Read has failed, regenerate file
562+
log("Cannot read properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
563+
shouldGenerate = true;
539564
}
565+
}
540566

541-
} catch (IOException ex) {
542-
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
543-
} finally {
544-
Closeables.closeQuietly(outputWriter);
567+
if (shouldGenerate) {
568+
Files.createParentDirs(gitPropsFile);
569+
Writer outputWriter = null;
570+
571+
try {
572+
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charsets.UTF_8);
573+
if (isJsonFormat) {
574+
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
575+
ObjectMapper mapper = new ObjectMapper();
576+
mapper.writeValue(outputWriter, localProperties);
577+
} else {
578+
log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
579+
localProperties.store(outputWriter, "Generated by Git-Commit-Id-Plugin");
580+
}
581+
} catch (final IOException ex) {
582+
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
583+
} finally {
584+
Closeables.closeQuietly(outputWriter);
585+
}
586+
}
587+
else {
588+
log("Properties file [", gitPropsFile.getAbsolutePath(), "] is up-to-date (for module ", project.getName(), ")...");
545589
}
546590
}
547591

@@ -579,6 +623,77 @@ private boolean directoryDoesNotExits(File fileLocation) {
579623
return !directoryExists(fileLocation);
580624
}
581625

626+
@SuppressWarnings( "resource" )
627+
static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException {
628+
final HashMap<String, Object> propertiesMap;
629+
630+
{
631+
Closeable closeable = null;
632+
633+
try {
634+
final FileInputStream fis = new FileInputStream(jsonFile);
635+
closeable = fis;
636+
637+
final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8);
638+
closeable = reader;
639+
640+
final ObjectMapper mapper = new ObjectMapper();
641+
final TypeReference<HashMap<String,Object>> mapTypeRef =
642+
new TypeReference<HashMap<String,Object>>() {};
643+
644+
propertiesMap = mapper.readValue(reader, mapTypeRef);
645+
} catch (final Exception ex) {
646+
throw new CannotReadFileException(ex);
647+
} finally {
648+
Closeables.closeQuietly(closeable);
649+
}
650+
}
651+
652+
final Properties retVal = new Properties( );
653+
654+
for(final Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
655+
retVal.setProperty(entry.getKey(), String.valueOf(entry.getValue()));
656+
}
657+
658+
return retVal;
659+
}
660+
661+
@SuppressWarnings( "resource" )
662+
static Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException {
663+
Closeable closeable = null;
664+
665+
try {
666+
final FileInputStream fis = new FileInputStream(propertiesFile);
667+
closeable = fis;
668+
669+
final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8);
670+
closeable = reader;
671+
672+
final Properties retVal = new Properties();
673+
674+
retVal.load(reader);
675+
676+
return retVal;
677+
}
678+
catch (final Exception ex) {
679+
throw new CannotReadFileException(ex);
680+
}
681+
finally {
682+
Closeables.closeQuietly(closeable);
683+
}
684+
}
685+
686+
static class CannotReadFileException
687+
extends Exception
688+
{
689+
private static final long serialVersionUID = -6290782570018307756L;
690+
691+
CannotReadFileException( Throwable cause )
692+
{
693+
super( cause );
694+
}
695+
}
696+
582697
// SETTERS FOR TESTS ----------------------------------------------------
583698

584699
public void setFormat(String format) {

0 commit comments

Comments
 (0)