Skip to content

Commit 9590679

Browse files
Alexander S. Pogrebnyakktoso
authored andcommitted
Added exception handling, when properties file cannot be read for comparison
1 parent ddb52d0 commit 9590679

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import pl.project13.maven.git.util.PropertyManager;
4141

4242
import java.io.*;
43-
import java.nio.charset.Charset;
4443
import java.text.SimpleDateFormat;
4544
import java.util.Collections;
4645
import java.util.Date;
@@ -537,25 +536,32 @@ void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base,
537536
if (gitPropsFile.exists( )) {
538537
final Properties persistedProperties;
539538

540-
if (isJsonFormat) {
541-
log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
539+
try {
540+
if (isJsonFormat) {
541+
log("Reading exising json file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
542542

543-
persistedProperties = readJsonProperties( gitPropsFile );
544-
}
545-
else {
546-
log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
543+
persistedProperties = readJsonProperties( gitPropsFile );
544+
}
545+
else {
546+
log("Reading exising properties file [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
547547

548-
persistedProperties = readProperties( gitPropsFile );
549-
}
548+
persistedProperties = readProperties( gitPropsFile );
549+
}
550550

551-
final Properties propertiesCopy = (Properties) localProperties.clone( );
551+
final Properties propertiesCopy = (Properties) localProperties.clone( );
552552

553-
final String buildTimeProperty = prefixDot + BUILD_TIME;
553+
final String buildTimeProperty = prefixDot + BUILD_TIME;
554554

555-
propertiesCopy.remove( buildTimeProperty );
556-
persistedProperties.remove( buildTimeProperty );
555+
propertiesCopy.remove( buildTimeProperty );
556+
persistedProperties.remove( buildTimeProperty );
557557

558-
shouldGenerate = ! propertiesCopy.equals( persistedProperties );
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;
564+
}
559565
}
560566

561567
if (shouldGenerate) {
@@ -618,7 +624,7 @@ private boolean directoryDoesNotExits(File fileLocation) {
618624
}
619625

620626
@SuppressWarnings( "resource" )
621-
static Properties readJsonProperties(@NotNull File jsonFile) {
627+
static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException {
622628
final HashMap<String, Object> propertiesMap;
623629

624630
{
@@ -637,7 +643,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) {
637643

638644
propertiesMap = mapper.readValue(reader, mapTypeRef);
639645
} catch (final Exception ex) {
640-
throw new RuntimeException("Cannot read from git properties file: " + jsonFile, ex);
646+
throw new CannotReadFileException(ex);
641647
} finally {
642648
Closeables.closeQuietly(closeable);
643649
}
@@ -653,8 +659,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) {
653659
}
654660

655661
@SuppressWarnings( "resource" )
656-
static Properties readProperties(@NotNull File propertiesFile)
657-
{
662+
static Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException {
658663
Closeable closeable = null;
659664

660665
try {
@@ -664,20 +669,31 @@ static Properties readProperties(@NotNull File propertiesFile)
664669
final InputStreamReader reader = new InputStreamReader(fis, Charsets.UTF_8);
665670
closeable = reader;
666671

667-
final Properties retVal = new Properties( );
672+
final Properties retVal = new Properties();
668673

669674
retVal.load(reader);
670675

671676
return retVal;
672677
}
673678
catch (final Exception ex) {
674-
throw new RuntimeException("Cannot read from git properties file: " + propertiesFile, ex);
679+
throw new CannotReadFileException(ex);
675680
}
676681
finally {
677682
Closeables.closeQuietly(closeable);
678683
}
679684
}
680685

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+
681697
// SETTERS FOR TESTS ----------------------------------------------------
682698

683699
public void setFormat(String format) {

0 commit comments

Comments
 (0)