diff --git a/google-http-client/pom.xml b/google-http-client/pom.xml index f748898e4..9694723f4 100644 --- a/google-http-client/pom.xml +++ b/google-http-client/pom.xml @@ -29,6 +29,17 @@ + + org.apache.maven.plugins + maven-resources-plugin + + + + resources + + + + maven-javadoc-plugin @@ -55,7 +66,10 @@ maven-jar-plugin - + + + true + ${project.build.outputDirectory}/META-INF/MANIFEST.MF com.google.api.client @@ -78,6 +92,13 @@ + + + + src/main/resources + true + + diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java b/google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java index c53c10e07..f6cd29ef4 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java @@ -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; @@ -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. @@ -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; + } } diff --git a/google-http-client/src/main/resources/google-http-client.properties b/google-http-client/src/main/resources/google-http-client.properties new file mode 100644 index 000000000..a69f45fa8 --- /dev/null +++ b/google-http-client/src/main/resources/google-http-client.properties @@ -0,0 +1 @@ +google-http-client.version=${project.version} \ No newline at end of file diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java index 66b6449eb..a76b63850 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java @@ -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; @@ -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")); diff --git a/pom.xml b/pom.xml index 74d26649c..1c5dcbbc0 100644 --- a/pom.xml +++ b/pom.xml @@ -367,6 +367,11 @@ maven-dependency-plugin 3.1.1 + + org.apache.maven.plugins + maven-resources-plugin + 3.1.0 +