Skip to content

Commit 3ef2887

Browse files
authored
Version info from property file (#802)
* get version from txt file * also get it from versioninfo if fails * version from property file * comment * comments
1 parent cdb7cef commit 3ef2887

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
allprojects {
2121
group = "co.elastic.clients"
22-
// Release manager provides a $VERSION. If not present, it's a local or CI snapshot build.
23-
version = System.getenv("VERSION") ?:
24-
(File(project.rootDir, "config/version.txt").readText().trim() + "-SNAPSHOT")
22+
version = (File(project.rootDir, "config/version.txt").readText().trim() + "-SNAPSHOT")
2523

2624
repositories {
2725
maven {

java-client-serverless/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ checkstyle {
3737
toolVersion = "10.16.0"
3838
}
3939

40-
// GitHub Maven repo doesn't like 1.0.0+20231031-SNAPSHOT
41-
version = "1.0.0-20231031-SNAPSHOT"
40+
version = (File(project.rootDir, "config/version-serverless.txt").readText().trim() + "-SNAPSHOT")
4241

4342
signing {
4443
sign(publishing.publications)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Licensed to Elasticsearch B.V. under one or more contributor
3+
# license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright
5+
# ownership. Elasticsearch B.V. licenses this file to you under
6+
# the Apache License, Version 2.0 (the "License"); you may
7+
# not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
version=${version}
21+
git_revision=${git_revision}

java-client/src/main/java/co/elastic/clients/transport/Version.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
package co.elastic.clients.transport;
2121

22+
import co.elastic.clients.ApiClient;
23+
2224
import javax.annotation.Nullable;
25+
import java.io.InputStream;
2326
import java.util.Objects;
27+
import java.util.Properties;
2428

2529
/**
2630
* This class represents a SemVer version, with an optional patch revision.
@@ -45,20 +49,20 @@ public static Version parse(String version) {
4549
int hyphen = version.indexOf('-');
4650
if (hyphen >= 0) {
4751
// Has prerelease. May be followed buy build information
48-
prerelease = version.substring(hyphen+1);
52+
prerelease = version.substring(hyphen + 1);
4953
version = version.substring(0, hyphen);
5054

5155
int plus = prerelease.indexOf('+');
5256
if (plus >= 0) {
53-
build = prerelease.substring(0, plus+1);
57+
build = prerelease.substring(0, plus + 1);
5458
prerelease = prerelease.substring(0, plus);
5559
}
5660
}
5761

5862
int plus = version.indexOf('+');
5963
if (plus >= 0) {
6064
// Has build information
61-
build = version.substring(0, plus+1);
65+
build = version.substring(0, plus + 1);
6266
version = version.substring(0, plus);
6367
}
6468

@@ -68,8 +72,7 @@ public static Version parse(String version) {
6872
int minor = (bits.length >= 2) ? Integer.parseInt(bits[1]) : 0;
6973
int maintenance = (bits.length >= 3) ? Integer.parseInt(bits[2]) : -1;
7074
return new Version(major, minor, maintenance, prerelease, build);
71-
}
72-
catch(NumberFormatException ex) {
75+
} catch (NumberFormatException ex) {
7376
return null;
7477
}
7578
}
@@ -78,7 +81,8 @@ public Version(int major, int minor, int maintenance, boolean isPreRelease) {
7881
this(major, minor, maintenance, isPreRelease ? "p" : null, null);
7982
}
8083

81-
public Version(int major, int minor, int maintenance, @Nullable String prerelease, @Nullable String build) {
84+
public Version(int major, int minor, int maintenance, @Nullable String prerelease,
85+
@Nullable String build) {
8286
this.major = major;
8387
this.minor = minor;
8488
this.maintenance = maintenance;
@@ -108,10 +112,10 @@ public boolean equals(Object other) {
108112
if (!(other instanceof Version)) return false;
109113
Version that = (Version) other;
110114
return (major == that.major &&
111-
minor == that.minor &&
112-
maintenance == that.maintenance &&
113-
Objects.equals(prerelease, that.prerelease) &&
114-
Objects.equals(build, that.build));
115+
minor == that.minor &&
116+
maintenance == that.maintenance &&
117+
Objects.equals(prerelease, that.prerelease) &&
118+
Objects.equals(build, that.build));
115119
}
116120

117121
@Override
@@ -146,10 +150,19 @@ public String toString() {
146150

147151
static {
148152
Version version = null;
149-
try {
150-
version = Version.parse(VersionInfo.VERSION);
151-
} catch (Exception e) {
152-
// Failed to parse version
153+
InputStream in = ApiClient.class.getResourceAsStream("version.properties");
154+
if (in != null) {
155+
Properties properties = new Properties();
156+
try {
157+
properties.load(in);
158+
String versionStr = properties.getProperty("version");
159+
if (versionStr != null) {
160+
version = Version.parse(versionStr);
161+
}
162+
} catch (Exception e) {
163+
// Failed to parse version from file, trying from VersionInfo
164+
version = Version.parse(VersionInfo.VERSION);
165+
}
153166
}
154167
VERSION = version;
155168
}

0 commit comments

Comments
 (0)