ajoberstar/grgit

Build fails when not using git

Closed this issue · 1 comments

Overview:

Grgit silently puts a null reference in gradle.properties when it can not find a git repo. This leads to a build failure when processResources task is executed.

Error:

When building without a git repo, the following abbreviated error is shown:

Could not copy file...
Invalid filter specification for org.apache.tools.ant.filters.ReplaceTokens...
Cannot cast object {insert property file data here} with class 'java.util.HashMap' to class 'java.util.Hashtable'
due to: java.lang.NullPointerException

Inside the {insert property file data here} is the line grgit=null, which—as previously mentioned—is inserted by grgit when it can not find a git repo.

Possible Solutions:

without making any changes to the underlying logic, you can simply replace all null vales in the gradle.properties file with an empty string. This can be done by concatenating .collectEntries{ k, v -> [k, v ?: '']} to the filter call in the processResources task.
The task now looks like this:

processResources {
        filesMatching(['**/*.json', '**/*.yml']) {
            filter ReplaceTokens as Class, beginToken: '${', endToken: '}',
                    tokens: rootProject.ext.properties.collectEntries{ k, v -> [k, v ?: '']}
        }
}

This does not have any consequencies in functionaly because the resuliting plugin.yaml version field will just be concatenated with unknown as specified by versionMetadata method:

if (grgit == null) {
        return '-unknown'
}

This fix however could potentally down the line introduce errors when devolping if a propertiy is null but gets copied as an empty string.
To only replace the grgit=null entry you can use the following code:

def tokenMap = rootProject.ext.properties
        tokenMap.merge("grgit",'',(s, s2) -> s)
        filesMatching(['**/*.json', '**/*.yml']) {
            filter ReplaceTokens as Class, beginToken: '${', endToken: '}',
                    tokens: tokenMap
}

the merge function replaces the key grgit with '' if null or the original value otherwise.
Hopefully this helps anyone with the same error and please let me know if you would like me to make a PR.

(Sorry I posted this in the wrong repo)