Skip to content

Commit 3f0f645

Browse files
authored
Make project.properties path independent per API (#1863)
- Previously we generate all project.properties files into root which is incorrect - Now the caller API client needs to pass in package path to retrieve the corresponding version - Create project.properties for Veneer-only clients as well
1 parent 95946ef commit 3f0f645

22 files changed

Lines changed: 249 additions & 96 deletions

File tree

google-cloud-bigquery/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</parent>
1717
<properties>
1818
<site.installationModule>google-cloud-bigquery</site.installationModule>
19+
<artifact.version>${project.version}</artifact.version>
1920
</properties>
2021
<dependencies>
2122
<dependency>
@@ -61,4 +62,24 @@
6162
<scope>test</scope>
6263
</dependency>
6364
</dependencies>
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.codehaus.mojo</groupId>
69+
<artifactId>properties-maven-plugin</artifactId>
70+
<version>1.0-alpha-2</version>
71+
<executions>
72+
<execution>
73+
<phase>generate-resources</phase>
74+
<goals>
75+
<goal>write-project-properties</goal>
76+
</goals>
77+
<configuration>
78+
<outputFile>${project.build.outputDirectory}/com/google/cloud/bigquery/project.properties</outputFile>
79+
</configuration>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
</plugins>
84+
</build>
6485
</project>

google-cloud-compute/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</parent>
1616
<properties>
1717
<site.installationModule>google-cloud-compute</site.installationModule>
18+
<artifact.version>${project.version}</artifact.version>
1819
</properties>
1920
<dependencies>
2021
<dependency>
@@ -54,4 +55,24 @@
5455
<scope>test</scope>
5556
</dependency>
5657
</dependencies>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.codehaus.mojo</groupId>
62+
<artifactId>properties-maven-plugin</artifactId>
63+
<version>1.0-alpha-2</version>
64+
<executions>
65+
<execution>
66+
<phase>generate-resources</phase>
67+
<goals>
68+
<goal>write-project-properties</goal>
69+
</goals>
70+
<configuration>
71+
<outputFile>${project.build.outputDirectory}/com/google/cloud/compute/project.properties</outputFile>
72+
</configuration>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
5778
</project>

google-cloud-core/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</parent>
1616
<properties>
1717
<site.installationModule>google-cloud-core</site.installationModule>
18+
<artifact.version>${project.version}</artifact.version>
1819
</properties>
1920
<dependencies>
2021
<dependency>
@@ -171,4 +172,24 @@
171172
<scope>test</scope>
172173
</dependency>
173174
</dependencies>
175+
<build>
176+
<plugins>
177+
<plugin>
178+
<groupId>org.codehaus.mojo</groupId>
179+
<artifactId>properties-maven-plugin</artifactId>
180+
<version>1.0-alpha-2</version>
181+
<executions>
182+
<execution>
183+
<phase>generate-resources</phase>
184+
<goals>
185+
<goal>write-project-properties</goal>
186+
</goals>
187+
<configuration>
188+
<outputFile>${project.build.outputDirectory}/com/google/cloud/project.properties</outputFile>
189+
</configuration>
190+
</execution>
191+
</executions>
192+
</plugin>
193+
</plugins>
194+
</build>
174195
</project>

google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static ChannelProvider setUpChannelProvider(
216216
InstantiatingChannelProvider.Builder providerBuilder, ServiceOptions<?, ?> serviceOptions) {
217217
providerBuilder.setEndpoint(serviceOptions.getHost())
218218
.setClientLibHeader(ServiceOptions.getGoogApiClientLibName(),
219-
firstNonNull(ServiceOptions.getLibraryVersion(), ""));
219+
firstNonNull(serviceOptions.getLibraryVersion(), ""));
220220
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
221221
if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
222222
providerBuilder.setCredentialsProvider(FixedCredentialsProvider.create(scopedCredentials));

google-cloud-core/src/main/java/com/google/cloud/HttpTransportOptions.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public HttpTransportFactory getHttpTransportFactory() {
139139
* Returns a request initializer responsible for initializing requests according to service
140140
* options.
141141
*/
142-
public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions<?, ?> serviceOptions) {
142+
public HttpRequestInitializer getHttpRequestInitializer(
143+
final ServiceOptions<?, ?> serviceOptions) {
143144
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
144145
final HttpRequestInitializer delegate =
145146
scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()
@@ -158,17 +159,17 @@ public void initialize(HttpRequest httpRequest) throws IOException {
158159
}
159160

160161
HttpHeaders headers = httpRequest.getHeaders();
161-
headers.set("x-goog-api-client", getXGoogApiClientHeader());
162+
headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
162163
}
163164
};
164165
}
165166

166-
String getXGoogApiClientHeader() {
167+
String getXGoogApiClientHeader(ServiceOptions<?, ?> serviceOptions) {
167168
return String.format(
168169
"gl-java/%s %s/%s",
169170
getJavaVersion(),
170171
ServiceOptions.getGoogApiClientLibName(),
171-
ServiceOptions.getLibraryVersion());
172+
serviceOptions.getLibraryVersion());
172173
}
173174

174175
private static String getJavaVersion() {

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,15 @@
2121
import static com.google.common.base.Preconditions.checkNotNull;
2222
import static java.nio.charset.StandardCharsets.UTF_8;
2323

24-
import com.google.api.gax.core.CurrentMillisClock;
2524
import com.google.api.gax.core.ApiClock;
25+
import com.google.api.gax.core.CurrentMillisClock;
26+
import com.google.api.gax.core.PropertiesProvider;
2627
import com.google.api.gax.core.RetrySettings;
2728
import com.google.auth.Credentials;
2829
import com.google.auth.oauth2.GoogleCredentials;
2930
import com.google.cloud.spi.ServiceRpcFactory;
3031
import com.google.common.collect.Iterables;
3132
import com.google.common.io.Files;
32-
33-
import org.joda.time.Duration;
34-
import org.json.JSONException;
35-
import org.json.JSONObject;
36-
import org.json.JSONTokener;
37-
3833
import java.io.BufferedReader;
3934
import java.io.File;
4035
import java.io.FileInputStream;
@@ -49,17 +44,16 @@
4944
import java.net.HttpURLConnection;
5045
import java.net.URL;
5146
import java.nio.charset.Charset;
52-
import java.util.Enumeration;
5347
import java.util.Locale;
5448
import java.util.Objects;
55-
import java.util.Properties;
5649
import java.util.ServiceLoader;
5750
import java.util.Set;
58-
import java.util.jar.Attributes;
59-
import java.util.jar.JarFile;
60-
import java.util.jar.Manifest;
6151
import java.util.regex.Matcher;
6252
import java.util.regex.Pattern;
53+
import org.joda.time.Duration;
54+
import org.json.JSONException;
55+
import org.json.JSONObject;
56+
import org.json.JSONTokener;
6357

6458
/**
6559
* Abstract class representing service options.
@@ -73,17 +67,13 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>,
7367
private static final String DEFAULT_HOST = "https://www.googleapis.com";
7468
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
7569
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
76-
private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId";
77-
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
78-
private static final String ARTIFACT_ID = "google-cloud-core";
7970
private static final String LIBRARY_NAME = "gcloud-java";
8071
private static final String X_GOOGLE_CLIENT_HEADER_NAME = "gccl";
81-
private static final String LIBRARY_VERSION = defaultLibraryVersion();
82-
private static final String APPLICATION_NAME =
83-
LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION;
8472

85-
private static final String META_FILE_ROOT = "/META-INF/maven/";
86-
private static final String META_VERSION_KEY = "version";
73+
private static final String PROPERTIES_VERSION_KEY = "artifact.version";
74+
private static final String DEFAULT_PACKAGE_PATH = "com/google/cloud";
75+
private static final String PROPERTIES_FILE = "project.properties";
76+
8777
private static final RetrySettings DEFAULT_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
8878
.build();
8979
private static final RetrySettings NO_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
@@ -525,8 +515,9 @@ public TransportOptions getTransportOptions() {
525515
/**
526516
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
527517
*/
528-
public static String getApplicationName() {
529-
return APPLICATION_NAME;
518+
public String getApplicationName() {
519+
String libraryVersion = getLibraryVersion();
520+
return libraryVersion == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + libraryVersion;
530521
}
531522

532523

@@ -547,8 +538,17 @@ public static String getGoogApiClientLibName() {
547538
/**
548539
* Returns the library's version as a string.
549540
*/
550-
public static String getLibraryVersion() {
551-
return LIBRARY_VERSION;
541+
public String getLibraryVersion() {
542+
try {
543+
String version = getVersionProperty(getPackagePath());
544+
if (version == null) {
545+
version = getVersionProperty(DEFAULT_PACKAGE_PATH);
546+
}
547+
return version;
548+
} catch (Exception e) {
549+
// ignore
550+
}
551+
return null;
552552
}
553553

554554
protected int baseHashCode() {
@@ -618,47 +618,13 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
618618
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
619619
}
620620

621-
private static String defaultLibraryVersion() {
622-
String version = getPomVersion();
623-
if (version == null) {
624-
version = getManifestVersion();
625-
}
626-
return version;
627-
}
628-
629-
private static String getPomVersion() {
630-
try {
631-
Properties properties = new Properties();
632-
String mavenPropertiesPath = META_FILE_ROOT
633-
+ ServiceOptions.class.getPackage().getName() + "/"
634-
+ ARTIFACT_ID + "/pom.properties";
635-
InputStream inputStream = ServiceOptions.class.getResourceAsStream(mavenPropertiesPath);
636-
if (inputStream != null) {
637-
properties.load(inputStream);
638-
return properties.getProperty(META_VERSION_KEY, "");
639-
}
640-
} catch (Exception e) {
641-
// ignore
642-
}
643-
return null;
621+
private String getVersionProperty(String packagePath) {
622+
String projectPropertiesPath = "/" + packagePath + "/" + PROPERTIES_FILE;
623+
return PropertiesProvider.loadProperty(
624+
ServiceOptions.class, projectPropertiesPath, PROPERTIES_VERSION_KEY);
644625
}
645626

646-
private static String getManifestVersion() {
647-
String version = null;
648-
try {
649-
Enumeration<URL> resources =
650-
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
651-
while (resources.hasMoreElements() && version == null) {
652-
Manifest manifest = new Manifest(resources.nextElement().openStream());
653-
Attributes manifestAttributes = manifest.getMainAttributes();
654-
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
655-
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
656-
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
657-
}
658-
}
659-
} catch (IOException e) {
660-
// ignore
661-
}
662-
return version;
627+
private String getPackagePath() {
628+
return this.getClass().getPackage().getName().replaceAll("\\.", "/");
663629
}
664630
}

google-cloud-core/src/test/java/com/google/cloud/HttpTransportOptionsTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ public void testBaseHashCode() {
6464

6565
@Test
6666
public void testHeader() {
67-
String expectedHeaderPattern = "^gl-java/.* gccl/.*";
67+
String expectedHeaderPattern = "^gl-java/.* gccl/0.0.0";
68+
final ServiceOptions mockOptions = EasyMock.createMock(ServiceOptions.class);
69+
EasyMock.expect(mockOptions.getLibraryVersion()).andReturn("0.0.0");
70+
EasyMock.replay(mockOptions);
6871
assertTrue(Pattern.compile(expectedHeaderPattern)
69-
.matcher(DEFAULT_OPTIONS.getXGoogApiClientHeader())
72+
.matcher(OPTIONS.getXGoogApiClientHeader(mockOptions))
7073
.find());
7174
}
7275
}

google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@
2626
import com.google.api.gax.core.CurrentMillisClock;
2727
import com.google.auth.oauth2.GoogleCredentials;
2828
import com.google.cloud.spi.ServiceRpcFactory;
29-
30-
import org.junit.Rule;
31-
import org.junit.Test;
32-
import org.junit.rules.ExpectedException;
33-
3429
import java.io.ByteArrayInputStream;
3530
import java.io.IOException;
3631
import java.io.InputStream;
3732
import java.util.Set;
3833
import java.util.regex.Pattern;
34+
import org.junit.Rule;
35+
import org.junit.Test;
36+
import org.junit.rules.ExpectedException;
3937

4038
public class ServiceOptionsTest {
4139
private static final String JSON_KEY =
@@ -98,7 +96,7 @@ public class ServiceOptionsTest {
9896
private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build();
9997
private static final String LIBRARY_NAME = "gcloud-java";
10098
private static final Pattern APPLICATION_NAME_PATTERN =
101-
Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?");
99+
Pattern.compile(LIBRARY_NAME + "/.*");
102100

103101
@Rule public ExpectedException thrown = ExpectedException.none();
104102

@@ -268,7 +266,7 @@ public void testBaseEquals() {
268266

269267
@Test
270268
public void testLibraryName() {
271-
assertEquals(LIBRARY_NAME, OPTIONS.getLibraryName());
269+
assertEquals(LIBRARY_NAME, ServiceOptions.getLibraryName());
272270
}
273271

274272
@Test

google-cloud-datastore/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</parent>
1717
<properties>
1818
<site.installationModule>google-cloud-datastore</site.installationModule>
19+
<artifact.version>${project.version}</artifact.version>
1920
</properties>
2021
<dependencies>
2122
<dependency>
@@ -59,4 +60,24 @@
5960
<scope>test</scope>
6061
</dependency>
6162
</dependencies>
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.codehaus.mojo</groupId>
67+
<artifactId>properties-maven-plugin</artifactId>
68+
<version>1.0-alpha-2</version>
69+
<executions>
70+
<execution>
71+
<phase>generate-resources</phase>
72+
<goals>
73+
<goal>write-project-properties</goal>
74+
</goals>
75+
<configuration>
76+
<outputFile>${project.build.outputDirectory}/com/google/cloud/datastore/project.properties</outputFile>
77+
</configuration>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
</plugins>
82+
</build>
6283
</project>

0 commit comments

Comments
 (0)