Skip to content

Commit

Permalink
Support gradle.properties version file (#5)
Browse files Browse the repository at this point in the history
The `gradle.properties` file allows for a default version while still
allowing for the version to be overridden on the command line. This is a
perfect scenario for new-release-version

For example:
Set the property only in the `gradle.properties` file (i.e. remove it
from build.gradle). Also make sure the options come before the command
(as mentioned above).

gradle.properties contents:

```
version=1.0.12
```

Version can then be overridden on the command line with:

```
gradle -Pversion=1.0.13 publish
```

Based on the StackOverflow answer
https://stackoverflow.com/a/40239092/620196
  • Loading branch information
zachwhaley authored Feb 22, 2023
1 parent 9e01a94 commit 21250ef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
29 changes: 29 additions & 0 deletions examples/java/gradle.properties/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.1.1/userguide/java_library_plugin.html
*/

plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}

repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.1-jre'

// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
1 change: 1 addition & 0 deletions examples/java/gradle.properties/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=1.3.2
23 changes: 12 additions & 11 deletions newrelver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const VersionNumberRegex = `[\.\d]+(-\w+)?`
//
// The %s is replaced with VersionNumberRegex.
const (
VersionsGradleRegexf = `(?m)project\.version\s*=\s*['"](%s)['"]$`
ConfigRegexf = `(?m)^version\s*=\s*(%s)$`
BuildGradleRegexf = `(?m)^version\s*=\s*['"](%s)['"]$`
SetupCfgRegexf = `(?m)^version\s*=\s*(%s)$`
VersionsGradleRegexf = `(?m)project\.version\s*=\s*['"](%s)['"]$`
SetupPyRegexf = `(?ms)setup\(.*\s+version\s*=\s*['"](%s)['"].*\)$`
CMakeListsTxtRegexf = `(?ms)^project\s*\(.*\s+VERSION\s+(%s).*\)$`
MakefileRegexf = `(?m)^VERSION\s*:=\s*(%s)$`
Expand All @@ -32,15 +32,16 @@ const (
type findVersion func([]byte) (string, error)

var versionFiles = map[string]findVersion{
"versions.gradle": versionMatcher(VersionsGradleRegexf, 1),
"build.gradle": versionMatcher(BuildGradleRegexf, 1),
"build.gradle.kts": versionMatcher(BuildGradleRegexf, 1),
"pom.xml": unmarshalXMLVersion,
"package.json": unmarshalJSONVersion,
"setup.cfg": versionMatcher(SetupCfgRegexf, 1),
"setup.py": versionMatcher(SetupPyRegexf, 1),
"CMakeLists.txt": versionMatcher(CMakeListsTxtRegexf, 1),
"Makefile": versionMatcher(MakefileRegexf, 1),
"gradle.properties": versionMatcher(ConfigRegexf, 1),
"build.gradle": versionMatcher(BuildGradleRegexf, 1),
"build.gradle.kts": versionMatcher(BuildGradleRegexf, 1),
"versions.gradle": versionMatcher(VersionsGradleRegexf, 1),
"pom.xml": unmarshalXMLVersion,
"package.json": unmarshalJSONVersion,
"setup.cfg": versionMatcher(ConfigRegexf, 1),
"setup.py": versionMatcher(SetupPyRegexf, 1),
"CMakeLists.txt": versionMatcher(CMakeListsTxtRegexf, 1),
"Makefile": versionMatcher(MakefileRegexf, 1),
}

func versionMatcher(regexf string, group int) findVersion {
Expand Down
11 changes: 11 additions & 0 deletions newrelver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ func TestVersionsGradle(t *testing.T) {
assert.Equal(t, "1.2.3", v.String())
}

func TestGradleProperties(t *testing.T) {
r := NewRelVer{
Dir: "examples/java/gradle.properties",
}

v, err := r.GetBaseVersion()
assert.NoError(t, err)

assert.Equal(t, "1.3.2", v.String())
}

func TestBuildGradle(t *testing.T) {
r := NewRelVer{
Dir: "examples/java/build.gradle",
Expand Down

0 comments on commit 21250ef

Please sign in to comment.