Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: grab version from the package metadata #806

Merged
merged 3 commits into from Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion google-http-client/pom.xml
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
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;
}
}
@@ -0,0 +1 @@
google-http-client.version=${project.version}
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
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