Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 136 additions & 21 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,35 @@

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;

import java.io.*;
import java.nio.charset.Charset;
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;

/**
Expand Down Expand Up @@ -355,7 +360,7 @@ public void execute() throws MojoExecutionException {
logProperties(properties);

if (generateGitPropertiesFile) {
generatePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
maybeGeneratePropertiesFile(properties, project.getBasedir(), generateGitPropertiesFilename);
}

if (injectAllReactorProjects) {
Expand Down Expand Up @@ -460,9 +465,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) {
Expand Down Expand Up @@ -520,26 +525,65 @@ 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;

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 = readProperties( gitPropsFile );
}

final Properties propertiesCopy = (Properties) localProperties.clone( );

final String buildTimeProperty = prefixDot + BUILD_TIME;

propertiesCopy.remove( buildTimeProperty );
persistedProperties.remove( buildTimeProperty );

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;
}
}

} catch (IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
} finally {
Closeables.closeQuietly(outputWriter);
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(), ")...");
}
}

Expand Down Expand Up @@ -577,6 +621,77 @@ private boolean directoryDoesNotExits(File fileLocation) {
return !directoryExists(fileLocation);
}

@SuppressWarnings( "resource" )
static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException {
final HashMap<String, Object> 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<HashMap<String,Object>> mapTypeRef =
new TypeReference<HashMap<String,Object>>() {};

propertiesMap = mapper.readValue(reader, mapTypeRef);
} catch (final Exception ex) {
throw new CannotReadFileException(ex);
} finally {
Closeables.closeQuietly(closeable);
}
}

final Properties retVal = new Properties( );

for(final Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
retVal.setProperty(entry.getKey(), String.valueOf(entry.getValue()));
}

return retVal;
}

@SuppressWarnings( "resource" )
static Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException {
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 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) {
Expand Down