Skip to content

Commit ddb52d0

Browse files
Alexander S. Pogrebnyakktoso
authored andcommitted
Issue 51. Only update properties file, when git environment changes.
Prevents endless build loop in Eclipse.
1 parent 9677d3d commit ddb52d0

File tree

1 file changed

+119
-20
lines changed

1 file changed

+119
-20
lines changed

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

Lines changed: 119 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
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;
@@ -40,7 +44,9 @@
4044
import java.text.SimpleDateFormat;
4145
import java.util.Collections;
4246
import java.util.Date;
47+
import java.util.HashMap;
4348
import java.util.List;
49+
import java.util.Map;
4450
import java.util.Properties;
4551

4652
/**
@@ -355,7 +361,7 @@ public void execute() throws MojoExecutionException {
355361
logProperties(properties);
356362

357363
if (generateGitPropertiesFile) {
358-
generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
364+
maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
359365
}
360366

361367
if (injectAllReactorProjects) {
@@ -462,9 +468,9 @@ private boolean isOurProperty(@NotNull String keyString) {
462468
}
463469

464470
void loadBuildTimeData(@NotNull Properties properties) {
465-
Date commitDate = new Date();
471+
Date buildDate = new Date();
466472
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
467-
put(properties, BUILD_TIME, smf.format(commitDate));
473+
put(properties, BUILD_TIME, smf.format(buildDate));
468474
}
469475

470476
void loadShortDescribe(@NotNull Properties properties) {
@@ -522,26 +528,58 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj
522528
jGitProvider.loadGitData(properties);
523529
}
524530

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 );
530534

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 );
539544
}
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( );
540552

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(), ")...");
545583
}
546584
}
547585

@@ -579,6 +617,67 @@ private boolean directoryDoesNotExits(File fileLocation) {
579617
return !directoryExists(fileLocation);
580618
}
581619

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+
582681
// SETTERS FOR TESTS ----------------------------------------------------
583682

584683
public void setFormat(String format) {

0 commit comments

Comments
 (0)