Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 5 additions & 14 deletions .github/.checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@
"http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">

<suppressions>
<suppress checks="JavadocStyle" files=".*\.java" />
<suppress checks="AbbreviationAsWordInName" files=".*\.java" />
<suppress checks="SummaryJavadoc" files=".*\.java" />
<suppress checks="MissingJavadocMethod" files=".*\.java" />
<suppress checks="MissingJavadocType" files=".*\.java" />
<suppress checks="JavadocParagraph" files=".*\.java" />
<suppress checks="JavadocMethod" files=".*\.java" />
<suppress checks="JavadocTagContinuationIndentation" files=".*\.java" />
<suppress checks="SingleLineJavadoc" files=".*\.java" />

<suppress checks="LineLength" files=".*\.java" />
<suppress checks="OperatorWrap" files=".*\.java" />
<suppress checks="ParameterName" files=".*\.java" />
<suppress checks="AvoidStarImport" files=".*\.java" />
<suppress checks="MemberName" files=".*\.java" />
<suppress checks="VariableDeclarationUsageDistance" files=".*\.java" />
<suppress checks="LocalVariableName" files=".*\.java" />
<suppress checks="MethodName" files=".*\.java" />
<suppress checks="CustomImportOrder" files=".*\.java" />
<suppress checks="OverloadMethodsDeclarationOrder" files=".*\.java" />
<suppress checks="VariableDeclarationUsageDistance" files=".*\.java" />

<suppress checks="AbbreviationAsWordInName" files="NativeAndJGitProviderTest\.java" lines="90"/>
<suppress checks="LineLength" files="GitCommitIdMojoIntegrationTest.java" />

</suppressions>

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion .github/.checkstyle/java.header
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <[email protected]>
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
<properties>
<maven-checkstyle-plugin.version>3.3.1</maven-checkstyle-plugin.version>
<!-- if you update the checkstyle version make sure you update the google_checks.xml inside the repository -->
<checkstyle.version>8.25</checkstyle.version>
<checkstyle.version>10.13.0</checkstyle.version>
<checkstyle.config.path>${basedir}/.github/.checkstyle</checkstyle.config.path>
</properties>
<build>
Expand Down Expand Up @@ -525,7 +525,10 @@
<configuration>
<configLocation>${checkstyle.config.path}/google_checks_checkstyle_${checkstyle.version}.xml</configLocation>
<suppressionsLocation>${checkstyle.config.path}/checkstyle-suppressions.xml</suppressionsLocation>
<propertyExpansion>samedir=${checkstyle.config.path}</propertyExpansion>
<propertyExpansion>
samedir=${checkstyle.config.path}
org.checkstyle.google.suppressionfilter.config=${checkstyle.config.path}/checkstyle-suppressions.xml
</propertyExpansion>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<encoding>${project.reporting.outputEncoding}</encoding>
<consoleOutput>true</consoleOutput>
Expand Down
1,806 changes: 881 additions & 925 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Large diffs are not rendered by default.

46 changes: 28 additions & 18 deletions src/main/java/pl/project13/maven/git/GitDirLocator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <[email protected]>
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -17,27 +18,31 @@

package pl.project13.maven.git;

import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Constants;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Constants;

/**
* This class encapsulates logic to locate a valid .git directory of the currently used project.
* If it's not already specified, this logic will try to find it.
* This class encapsulates logic to locate a valid .git directory of the currently used project. If
* it's not already specified, this logic will try to find it.
*/
public class GitDirLocator {
final MavenProject mavenProject;
final List<MavenProject> reactorProjects;

/**
* Constructor to encapsulates all references required to locate a valid .git directory
*
* @param mavenProject The currently used (maven) project.
* @param reactorProjects The list of reactor projects (sub-projects) of the current (maven) project.
* @param reactorProjects The list of reactor projects (sub-projects) of the current (maven)
* project.
*/
public GitDirLocator(MavenProject mavenProject, List<MavenProject> reactorProjects) {
this.mavenProject = mavenProject;
Expand All @@ -46,10 +51,12 @@ public GitDirLocator(MavenProject mavenProject, List<MavenProject> reactorProjec

/**
* Attempts to lookup a valid .git directory of the currently used project.
* @param manuallyConfiguredDir A user has the ability to configure a git-directory with the {@code dotGitDirectory}
* configuration setting. By default it should be simply {@code ${project.basedir}/.git}
* @return A valid .git directory, or {@code null} if none could be found under the user specified location or within
* the project or it's reactor projects.
*
* @param manuallyConfiguredDir A user has the ability to configure a git-directory with the
* {@code dotGitDirectory} configuration setting. By default it should be simply {@code
* ${project.basedir}/.git}
* @return A valid .git directory, or {@code null} if none could be found under the user specified
* location or within the project or it's reactor projects.
*/
@Nullable
public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) {
Expand Down Expand Up @@ -142,8 +149,8 @@ private File processGitDirFile(@Nonnull File file) {
}

/**
* If the file looks like the location of a worktree, return the .git folder of the git repository of the worktree.
* If not, return the file as is.
* If the file looks like the location of a worktree, return the .git folder of the git repository
* of the worktree. If not, return the file as is.
*/
static File resolveWorktree(File fileLocation) {
Path parent = fileLocation.toPath().getParent();
Expand All @@ -158,8 +165,11 @@ static File resolveWorktree(File fileLocation) {

/**
* Helper method to validate that the specified {@code File} is an existing directory.
* @param fileLocation The {@code File} that should be checked if it's actually an existing directory.
* @return {@code true} if the specified {@code File} is an existing directory, {@false} otherwise.
*
* @param fileLocation The {@code File} that should be checked if it's actually an existing
* directory.
* @return {@code true} if the specified {@code File} is an existing directory, {@false}
* otherwise.
*/
private static boolean isExistingDirectory(@Nullable File fileLocation) {
return fileLocation != null && fileLocation.exists() && fileLocation.isDirectory();
Expand Down
115 changes: 85 additions & 30 deletions src/main/java/pl/project13/maven/git/PropertiesReplacer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <[email protected]>
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -17,39 +18,44 @@

package pl.project13.maven.git;

import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import pl.project13.core.log.LogInterface;

import java.util.*;
import java.util.regex.Pattern;

/**
* This class encapsulates logic to perform property replacements.
* For a use-case refer to https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/317.
* This class encapsulates logic to perform property replacements. For a use-case refer to
* https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/317.
*/
public class PropertiesReplacer {
private final LogInterface log;
private final PluginParameterExpressionEvaluator expressionEvaluator;

/**
* Constructor to encapsulates all references required to perform property replacements.
*
* @param log The logger to log any messages
* @param expressionEvaluator Maven's PluginParameterExpressionEvaluator
* (see https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/413 why it's needed)
* @param expressionEvaluator Maven's PluginParameterExpressionEvaluator (see
* https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/413 why it's needed)
*/
public PropertiesReplacer(LogInterface log, PluginParameterExpressionEvaluator expressionEvaluator) {
public PropertiesReplacer(
LogInterface log, PluginParameterExpressionEvaluator expressionEvaluator) {
this.log = log;
this.expressionEvaluator = expressionEvaluator;
}

/**
* Method that performs the actual property replacement.
*
* @param properties all properties that are being generated by the plugin
* @param replacementProperties list of all replacement actions to perform
*/
public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
public void performReplacement(
Properties properties, List<ReplacementProperty> replacementProperties) {
if ((replacementProperties != null) && (properties != null)) {
for (ReplacementProperty replacementProperty: replacementProperties) {
for (ReplacementProperty replacementProperty : replacementProperties) {
String propertyKey = replacementProperty.getProperty();
if (propertyKey == null) {
performReplacementOnAllGeneratedProperties(properties, replacementProperty);
Expand All @@ -60,63 +66,112 @@ public void performReplacement(Properties properties, List<ReplacementProperty>
}
}

private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) {
private void performReplacementOnAllGeneratedProperties(
Properties properties, ReplacementProperty replacementProperty) {
for (String propertyName : properties.stringPropertyNames()) {
String content = properties.getProperty(propertyName);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
if ((replacementProperty.getPropertyOutputSuffix() != null)
&& (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyName + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info("apply replace on property " + propertyName + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
log.info(
"apply replace on property "
+ propertyName
+ " and save to "
+ newPropertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
} else {
properties.setProperty(propertyName, result);
log.info("apply replace on property " + propertyName + ": original value '" + content + "' with '" + result + "'");
log.info(
"apply replace on property "
+ propertyName
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
}
}
}

private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
private void performReplacementOnSingleProperty(
Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
String content = properties.getProperty(propertyKey);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
if ((replacementProperty.getPropertyOutputSuffix() != null)
&& (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
log.info(
"apply replace on property "
+ propertyKey
+ " and save to "
+ newPropertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
} else {
properties.setProperty(propertyKey, result);
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
log.info(
"apply replace on property "
+ propertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
}
}

private String performReplacement(ReplacementProperty replacementProperty, String content) {
String evaluationContent = content;
if (evaluationContent == null || evaluationContent.isEmpty() || replacementProperty.isForceValueEvaluation()) {
if (evaluationContent == null
|| evaluationContent.isEmpty()
|| replacementProperty.isForceValueEvaluation()) {
evaluationContent = replacementProperty.getValue();
}
String result = "";
try {
result = Optional
.ofNullable(expressionEvaluator.evaluate(evaluationContent))
.map(x -> x.toString()).orElse(evaluationContent);
result =
Optional.ofNullable(expressionEvaluator.evaluate(evaluationContent))
.map(x -> x.toString())
.orElse(evaluationContent);
} catch (Exception e) {
log.error("Something went wrong performing the replacement.", e);
}
if (replacementProperty != null) {
result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
result =
performTransformationRules(
replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
if (replacementProperty.isRegex()) {
result = replaceRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
result =
replaceRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
} else {
result = replaceNonRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
result =
replaceNonRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
}
result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.AFTER);
result =
performTransformationRules(
replacementProperty, result, TransformationRule.ApplyEnum.AFTER);
}
return result;
}

private String performTransformationRules(ReplacementProperty replacementProperty, String content, TransformationRule.ApplyEnum forRule) {
private String performTransformationRules(
ReplacementProperty replacementProperty,
String content,
TransformationRule.ApplyEnum forRule) {
String result = content;
if ((replacementProperty.getTransformationRules() != null) && (!replacementProperty.getTransformationRules().isEmpty())) {
for (TransformationRule transformationRule: replacementProperty.getTransformationRules()) {
if ((replacementProperty.getTransformationRules() != null)
&& (!replacementProperty.getTransformationRules().isEmpty())) {
for (TransformationRule transformationRule : replacementProperty.getTransformationRules()) {
if (transformationRule.getApplyRule().equals(forRule)) {
result = transformationRule.getActionRule().perform(result);
}
Expand Down
Loading