Skip to content

Commit d82718d

Browse files
authored
fix: grab version from the package metadata (#806)
* fix: grab version from the package metadata * test: ensure VERSION constant matches semver pattern * fix: use version from manifest, fallback to generated properties file
1 parent df21ebc commit d82718d

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

google-http-client/pom.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
</plugins>
3030
</pluginManagement>
3131
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-resources-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<goals>
38+
<goal>resources</goal>
39+
</goals>
40+
</execution>
41+
</executions>
42+
</plugin>
3243
<plugin>
3344
<artifactId>maven-javadoc-plugin</artifactId>
3445
<configuration>
@@ -55,7 +66,10 @@
5566
<plugin>
5667
<artifactId>maven-jar-plugin</artifactId>
5768
<configuration>
58-
<archive>
69+
<archive>
70+
<manifest>
71+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
72+
</manifest>
5973
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
6074
<manifestEntries>
6175
<Automatic-Module-Name>com.google.api.client</Automatic-Module-Name>
@@ -78,6 +92,13 @@
7892
</executions>
7993
</plugin>
8094
</plugins>
95+
96+
<resources>
97+
<resource>
98+
<directory>src/main/resources</directory>
99+
<filtering>true</filtering>
100+
</resource>
101+
</resources>
81102
</build>
82103
<dependencies>
83104
<dependency>

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opencensus.trace.Tracer;
3131
import java.io.IOException;
3232
import java.io.InputStream;
33+
import java.util.Properties;
3334
import java.util.concurrent.Callable;
3435
import java.util.concurrent.Executor;
3536
import java.util.concurrent.Executors;
@@ -53,7 +54,7 @@ public final class HttpRequest {
5354
*
5455
* @since 1.8
5556
*/
56-
public static final String VERSION = "1.30.0";
57+
public static final String VERSION = getVersion();
5758

5859
/**
5960
* User agent suffix for all requests.
@@ -1201,4 +1202,22 @@ private static void addSpanAttribute(Span span, String key, String value) {
12011202
span.putAttribute(key, AttributeValue.stringAttributeValue(value));
12021203
}
12031204
}
1205+
1206+
private static String getVersion() {
1207+
String version = HttpRequest.class.getPackage().getImplementationVersion();
1208+
// in a non-packaged environment (local), there's no implementation version to read
1209+
if (version == null) {
1210+
// fall back to reading from a properties file - note this value is expected to be cached
1211+
try (InputStream inputStream = HttpRequest.class.getResourceAsStream("/google-http-client.properties")) {
1212+
if (inputStream != null) {
1213+
Properties properties = new Properties();
1214+
properties.load(inputStream);
1215+
version = properties.getProperty("google-http-client.version");
1216+
}
1217+
} catch (IOException e) {
1218+
// ignore
1219+
}
1220+
}
1221+
return version;
1222+
}
12041223
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-http-client.version=${project.version}

google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.common.collect.ImmutableSet;
3333
import com.google.common.collect.Lists;
3434

35+
import java.util.regex.Pattern;
3536
import junit.framework.TestCase;
3637

3738
import java.io.ByteArrayInputStream;
@@ -1090,6 +1091,12 @@ public LowLevelHttpResponse execute() throws IOException {
10901091
}
10911092
}
10921093

1094+
public void testVersion() {
1095+
assertNotNull("version constant should not be null", HttpRequest.VERSION);
1096+
Pattern semverPattern = Pattern.compile("\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?");
1097+
assertTrue(semverPattern.matcher(HttpRequest.VERSION).matches());
1098+
}
1099+
10931100
public void testUserAgent() {
10941101
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("Google-HTTP-Java-Client"));
10951102
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("gzip"));

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@
367367
<artifactId>maven-dependency-plugin</artifactId>
368368
<version>3.1.1</version>
369369
</plugin>
370+
<plugin>
371+
<groupId>org.apache.maven.plugins</groupId>
372+
<artifactId>maven-resources-plugin</artifactId>
373+
<version>3.1.0</version>
374+
</plugin>
370375
</plugins>
371376
</pluginManagement>
372377
<plugins>

0 commit comments

Comments
 (0)