From 79ce3f0e6a1e4fceee6a77f3f775d45bad72ffdf Mon Sep 17 00:00:00 2001 From: "Alexander S. Pogrebnyak" Date: Tue, 6 Jan 2015 14:38:46 -0500 Subject: [PATCH 1/2] Issue 51. Only update properties file, when git environment changes. Prevents endless build loop in Eclipse. --- .../project13/maven/git/GitCommitIdMojo.java | 139 +++++++++++++++--- 1 file changed, 119 insertions(+), 20 deletions(-) diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index ddc6e860..bdc90450 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -17,20 +17,24 @@ package pl.project13.maven.git; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; 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.execution.MavenSession; 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; @@ -40,7 +44,9 @@ import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; /** @@ -355,7 +361,7 @@ public void execute() throws MojoExecutionException { logProperties(properties); if (generateGitPropertiesFile) { - generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename); + maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename); } if (injectAllReactorProjects) { @@ -460,9 +466,9 @@ private boolean isOurProperty(@NotNull String keyString) { } void loadBuildTimeData(@NotNull Properties properties) { - Date commitDate = new Date(); + Date buildDate = new Date(); SimpleDateFormat smf = new SimpleDateFormat(dateFormat); - put(properties, BUILD_TIME, smf.format(commitDate)); + put(properties, BUILD_TIME, smf.format(buildDate)); } void loadShortDescribe(@NotNull Properties properties) { @@ -520,26 +526,58 @@ void loadGitDataWithJGit(@NotNull Properties properties) throws IOException, Moj 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); + void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base, String propertiesFilename) throws IOException { + final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename); + final boolean isJsonFormat = "json".equalsIgnoreCase( format ); - 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"); + boolean shouldGenerate = true; + + if (gitPropsFile.exists( )) { + final Properties persistedProperties; + + if (isJsonFormat) { + log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + + persistedProperties = readJsonProperties( gitPropsFile ); } + else { + log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + + persistedProperties = readProperties( gitPropsFile ); + } + + final Properties propertiesCopy = (Properties) localProperties.clone( ); - } catch (IOException ex) { - throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex); - } finally { - Closeables.closeQuietly(outputWriter); + final String buildTimeProperty = prefixDot + BUILD_TIME; + + propertiesCopy.remove( buildTimeProperty ); + persistedProperties.remove( buildTimeProperty ); + + shouldGenerate = ! propertiesCopy.equals( persistedProperties ); + } + + if (shouldGenerate) { + Files.createParentDirs(gitPropsFile); + Writer outputWriter = null; + + try { + outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charsets.UTF_8); + if (isJsonFormat) { + log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(outputWriter, localProperties); + } else { + log("Writing properties file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + localProperties.store(outputWriter, "Generated by Git-Commit-Id-Plugin"); + } + } catch (final IOException ex) { + throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex); + } finally { + Closeables.closeQuietly(outputWriter); + } + } + else { + log("Properties file [", gitPropsFile.getAbsolutePath(), "] is up-to-date (for module ", project.getName(), ")..."); } } @@ -577,6 +615,67 @@ private boolean directoryDoesNotExits(File fileLocation) { return !directoryExists(fileLocation); } + @SuppressWarnings( "resource" ) + static Properties readJsonProperties(@NotNull File jsonFile) { + final HashMap propertiesMap; + + { + Closeable closeable = null; + + try { + final FileInputStream fis = new FileInputStream(jsonFile); + closeable = fis; + + final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8); + closeable = reader; + + final ObjectMapper mapper = new ObjectMapper(); + final TypeReference> mapTypeRef = + new TypeReference>() {}; + + propertiesMap = mapper.readValue(reader, mapTypeRef); + } catch (final Exception ex) { + throw new RuntimeException("Cannot read from git properties file: " + jsonFile, ex); + } finally { + Closeables.closeQuietly(closeable); + } + } + + final Properties retVal = new Properties( ); + + for(final Map.Entry entry : propertiesMap.entrySet()) { + retVal.setProperty(entry.getKey(), String.valueOf(entry.getValue())); + } + + return retVal; + } + + @SuppressWarnings( "resource" ) + static Properties readProperties(@NotNull File propertiesFile) + { + Closeable closeable = null; + + try { + final FileInputStream fis = new FileInputStream(propertiesFile); + closeable = fis; + + final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8); + closeable = reader; + + final Properties retVal = new Properties( ); + + retVal.load(reader); + + return retVal; + } + catch (final Exception ex) { + throw new RuntimeException("Cannot read from git properties file: " + propertiesFile, ex); + } + finally { + Closeables.closeQuietly(closeable); + } + } + // SETTERS FOR TESTS ---------------------------------------------------- public void setFormat(String format) { From 7dcbbf6ddebccb14a141b863ac8f5a0a49e1bf61 Mon Sep 17 00:00:00 2001 From: "Alexander S. Pogrebnyak" Date: Tue, 6 Jan 2015 18:21:37 -0500 Subject: [PATCH 2/2] Added exception handling, when properties file cannot be read for comparison --- .../project13/maven/git/GitCommitIdMojo.java | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index bdc90450..93ce83fd 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -40,7 +40,6 @@ 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; @@ -535,25 +534,32 @@ void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base, if (gitPropsFile.exists( )) { final Properties persistedProperties; - if (isJsonFormat) { - log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + try { + if (isJsonFormat) { + log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); - persistedProperties = readJsonProperties( gitPropsFile ); - } - else { - log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + persistedProperties = readJsonProperties( gitPropsFile ); + } + else { + log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); - persistedProperties = readProperties( gitPropsFile ); - } + persistedProperties = readProperties( gitPropsFile ); + } - final Properties propertiesCopy = (Properties) localProperties.clone( ); + final Properties propertiesCopy = (Properties) localProperties.clone( ); - final String buildTimeProperty = prefixDot + BUILD_TIME; + final String buildTimeProperty = prefixDot + BUILD_TIME; - propertiesCopy.remove( buildTimeProperty ); - persistedProperties.remove( buildTimeProperty ); + propertiesCopy.remove( buildTimeProperty ); + persistedProperties.remove( buildTimeProperty ); - shouldGenerate = ! propertiesCopy.equals( persistedProperties ); + shouldGenerate = ! propertiesCopy.equals( persistedProperties ); + } + catch ( CannotReadFileException ex ) { + // Read has failed, regenerate file + log("Cannot read properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")..."); + shouldGenerate = true; + } } if (shouldGenerate) { @@ -616,7 +622,7 @@ private boolean directoryDoesNotExits(File fileLocation) { } @SuppressWarnings( "resource" ) - static Properties readJsonProperties(@NotNull File jsonFile) { + static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException { final HashMap propertiesMap; { @@ -635,7 +641,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) { propertiesMap = mapper.readValue(reader, mapTypeRef); } catch (final Exception ex) { - throw new RuntimeException("Cannot read from git properties file: " + jsonFile, ex); + throw new CannotReadFileException(ex); } finally { Closeables.closeQuietly(closeable); } @@ -651,8 +657,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) { } @SuppressWarnings( "resource" ) - static Properties readProperties(@NotNull File propertiesFile) - { + static Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException { Closeable closeable = null; try { @@ -662,20 +667,31 @@ static Properties readProperties(@NotNull File propertiesFile) final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8); closeable = reader; - final Properties retVal = new Properties( ); + final Properties retVal = new Properties(); retVal.load(reader); return retVal; } catch (final Exception ex) { - throw new RuntimeException("Cannot read from git properties file: " + propertiesFile, ex); + throw new CannotReadFileException(ex); } finally { Closeables.closeQuietly(closeable); } } + static class CannotReadFileException + extends Exception + { + private static final long serialVersionUID = -6290782570018307756L; + + CannotReadFileException( Throwable cause ) + { + super( cause ); + } + } + // SETTERS FOR TESTS ---------------------------------------------------- public void setFormat(String format) {