Skip to content

Commit

Permalink
fix: grab version from the package metadata (#806)
Browse files Browse the repository at this point in the history
* fix: grab version from the package metadata

* test: ensure VERSION constant matches semver pattern

* fix: use version from manifest, fallback to generated properties file
  • Loading branch information
chingor13 authored Sep 4, 2019
1 parent df21ebc commit d82718d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
23 changes: 22 additions & 1 deletion google-http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
Expand All @@ -55,7 +66,10 @@
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Automatic-Module-Name>com.google.api.client</Automatic-Module-Name>
Expand All @@ -78,6 +92,13 @@
</executions>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.opencensus.trace.Tracer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand All @@ -53,7 +54,7 @@ public final class HttpRequest {
*
* @since 1.8
*/
public static final String VERSION = "1.30.0";
public static final String VERSION = getVersion();

/**
* User agent suffix for all requests.
Expand Down Expand Up @@ -1201,4 +1202,22 @@ private static void addSpanAttribute(Span span, String key, String value) {
span.putAttribute(key, AttributeValue.stringAttributeValue(value));
}
}

private static String getVersion() {
String version = HttpRequest.class.getPackage().getImplementationVersion();
// in a non-packaged environment (local), there's no implementation version to read
if (version == null) {
// fall back to reading from a properties file - note this value is expected to be cached
try (InputStream inputStream = HttpRequest.class.getResourceAsStream("/google-http-client.properties")) {
if (inputStream != null) {
Properties properties = new Properties();
properties.load(inputStream);
version = properties.getProperty("google-http-client.version");
}
} catch (IOException e) {
// ignore
}
}
return version;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-http-client.version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

import java.util.regex.Pattern;
import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -1090,6 +1091,12 @@ public LowLevelHttpResponse execute() throws IOException {
}
}

public void testVersion() {
assertNotNull("version constant should not be null", HttpRequest.VERSION);
Pattern semverPattern = Pattern.compile("\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?");
assertTrue(semverPattern.matcher(HttpRequest.VERSION).matches());
}

public void testUserAgent() {
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("Google-HTTP-Java-Client"));
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("gzip"));
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down

0 comments on commit d82718d

Please sign in to comment.