Skip to content

Commit b72db11

Browse files
committed
Make project.properties path be API independente
- Previously we generate all project.properties files into root which is incorrect - Now the caller API client needs to pass in artifact ID to retrieve the corresponding version - Create project.properties for Veneer-only clients as well
1 parent 78346c3 commit b72db11

30 files changed

Lines changed: 285 additions & 82 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}/properties/${project.artifactId}/project.properties</outputFile>
79+
</configuration>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
</plugins>
84+
</build>
6485
</project>

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
public class HttpBigQueryRpc implements BigQueryRpc {
6464

6565
public static final String DEFAULT_PROJECTION = "full";
66+
67+
private static final String ARTIFACT_ID = "google-cloud-bigquery";
6668
private static final String BASE_RESUMABLE_URI =
6769
"https://www.googleapis.com/upload/bigquery/v2/projects/";
6870
// see: https://cloud.google.com/bigquery/loading-data-post-request#resume-upload
@@ -73,11 +75,12 @@ public class HttpBigQueryRpc implements BigQueryRpc {
7375
public HttpBigQueryRpc(BigQueryOptions options) {
7476
HttpTransportOptions transportOptions = (HttpTransportOptions) options.getTransportOptions();
7577
HttpTransport transport = transportOptions.getHttpTransportFactory().create();
76-
HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options);
78+
HttpRequestInitializer initializer =
79+
transportOptions.getHttpRequestInitializer(options, ARTIFACT_ID);
7780
this.options = options;
7881
bigquery = new Bigquery.Builder(transport, new JacksonFactory(), initializer)
7982
.setRootUrl(options.getHost())
80-
.setApplicationName(options.getApplicationName())
83+
.setApplicationName(options.getApplicationName(ARTIFACT_ID))
8184
.build();
8285
}
8386

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}/properties/${project.artifactId}/project.properties</outputFile>
72+
</configuration>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
5778
</project>

google-cloud-compute/src/main/java/com/google/cloud/compute/spi/v1/HttpComputeRpc.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.google.api.services.compute.model.Zone;
7070
import com.google.api.services.compute.model.ZoneList;
7171
import com.google.cloud.HttpTransportOptions;
72+
import com.google.cloud.ServiceOptions;
7273
import com.google.cloud.compute.ComputeException;
7374
import com.google.cloud.compute.ComputeOptions;
7475
import com.google.common.collect.ImmutableList;
@@ -77,17 +78,19 @@
7778

7879
public class HttpComputeRpc implements ComputeRpc {
7980

81+
private static final String ARTIFACT_ID = "google-cloud-compute";
8082
private final ComputeOptions options;
8183
private final Compute compute;
8284

8385
public HttpComputeRpc(ComputeOptions options) {
8486
HttpTransportOptions transportOptions = (HttpTransportOptions) options.getTransportOptions();
8587
HttpTransport transport = transportOptions.getHttpTransportFactory().create();
86-
HttpRequestInitializer initializer = transportOptions.getHttpRequestInitializer(options);
88+
HttpRequestInitializer initializer =
89+
transportOptions.getHttpRequestInitializer(options, ARTIFACT_ID);
8790
this.options = options;
8891
compute = new Compute.Builder(transport, new JacksonFactory(), initializer)
8992
.setRootUrl(options.getHost())
90-
.setApplicationName(options.getApplicationName())
93+
.setApplicationName(ServiceOptions.getApplicationName(ARTIFACT_ID))
9194
.build();
9295
}
9396

@@ -744,6 +747,7 @@ public Operation resizeDisk(String zone, String disk, long sizeGb, Map<Option, ?
744747
}
745748
}
746749

750+
@Override
747751
public Operation createSubnetwork(String region, Subnetwork subnetwork, Map<Option, ?> options) {
748752
try {
749753
return compute.subnetworks()

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}/properties/${project.artifactId}/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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,14 @@ public UnaryCallSettings.Builder getApiCallSettings(RetrySettings retrySettings)
213213
* Returns a channel provider from the given default provider.
214214
*/
215215
public static ChannelProvider setUpChannelProvider(
216-
InstantiatingChannelProvider.Builder providerBuilder, ServiceOptions<?, ?> serviceOptions) {
216+
InstantiatingChannelProvider.Builder providerBuilder,
217+
ServiceOptions<?, ?> serviceOptions,
218+
String artifactId) {
217219
HostAndPort hostAndPort = HostAndPort.fromString(serviceOptions.getHost());
218220
providerBuilder.setServiceAddress(hostAndPort.getHostText())
219221
.setPort(hostAndPort.getPort())
220222
.setClientLibHeader(ServiceOptions.getGoogApiClientLibName(),
221-
firstNonNull(ServiceOptions.getLibraryVersion(), ""));
223+
firstNonNull(ServiceOptions.getLibraryVersion(artifactId), ""));
222224
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
223225
if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
224226
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
@@ -138,7 +138,8 @@ public HttpTransportFactory getHttpTransportFactory() {
138138
* Returns a request initializer responsible for initializing requests according to service
139139
* options.
140140
*/
141-
public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions<?, ?> serviceOptions) {
141+
public HttpRequestInitializer getHttpRequestInitializer(
142+
ServiceOptions<?, ?> serviceOptions, final String artifactId) {
142143
Credentials scopedCredentials = serviceOptions.getScopedCredentials();
143144
final HttpRequestInitializer delegate =
144145
scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()
@@ -157,17 +158,17 @@ public void initialize(HttpRequest httpRequest) throws IOException {
157158
}
158159

159160
HttpHeaders headers = httpRequest.getHeaders();
160-
headers.set("x-goog-api-client", getXGoogApiClientHeader());
161+
headers.set("x-goog-api-client", getXGoogApiClientHeader(artifactId));
161162
}
162163
};
163164
}
164165

165-
String getXGoogApiClientHeader() {
166+
static String getXGoogApiClientHeader(String artifactId) {
166167
return String.format(
167168
"gl-java/%s %s/%s",
168169
getJavaVersion(),
169170
ServiceOptions.getGoogApiClientLibName(),
170-
ServiceOptions.getLibraryVersion());
171+
ServiceOptions.getLibraryVersion(artifactId));
171172
}
172173

173174
private static String getJavaVersion() {

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

Lines changed: 25 additions & 35 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;
@@ -52,14 +47,17 @@
5247
import java.util.Enumeration;
5348
import java.util.Locale;
5449
import java.util.Objects;
55-
import java.util.Properties;
5650
import java.util.ServiceLoader;
5751
import java.util.Set;
5852
import java.util.jar.Attributes;
5953
import java.util.jar.JarFile;
6054
import java.util.jar.Manifest;
6155
import java.util.regex.Matcher;
6256
import java.util.regex.Pattern;
57+
import org.joda.time.Duration;
58+
import org.json.JSONException;
59+
import org.json.JSONObject;
60+
import org.json.JSONTokener;
6361

6462
/**
6563
* Abstract class representing service options.
@@ -78,12 +76,11 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>,
7876
private static final String ARTIFACT_ID = "google-cloud-core";
7977
private static final String LIBRARY_NAME = "gcloud-java";
8078
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;
8479

85-
private static final String META_FILE_ROOT = "/META-INF/maven/";
86-
private static final String META_VERSION_KEY = "version";
80+
private static final String PROPERTIES_VERSION_KEY = "artifact.version";
81+
private static final String PROPERTIES_ROOT = "/properties/";
82+
private static final String PROPERTIES_FILE = "project.properties";
83+
8784
private static final RetrySettings DEFAULT_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
8885
.build();
8986
private static final RetrySettings NO_RETRY_SETTINGS = getDefaultRetrySettingsBuilder()
@@ -525,8 +522,9 @@ public TransportOptions getTransportOptions() {
525522
/**
526523
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
527524
*/
528-
public static String getApplicationName() {
529-
return APPLICATION_NAME;
525+
public static String getApplicationName(String artifactId) {
526+
String libraryVersion = getLibraryVersion(artifactId);
527+
return libraryVersion == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + libraryVersion;
530528
}
531529

532530

@@ -547,8 +545,12 @@ public static String getGoogApiClientLibName() {
547545
/**
548546
* Returns the library's version as a string.
549547
*/
550-
public static String getLibraryVersion() {
551-
return LIBRARY_VERSION;
548+
public static String getLibraryVersion(String artifactId) {
549+
String version = getPomVersion(artifactId);
550+
if (version == null) {
551+
version = getManifestVersion();
552+
}
553+
return version;
552554
}
553555

554556
protected int baseHashCode() {
@@ -618,25 +620,13 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
618620
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
619621
}
620622

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() {
623+
private static String getPomVersion(String artifactId) {
630624
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-
}
625+
String projectPropertiesPath = PROPERTIES_ROOT
626+
+ artifactId + "/"
627+
+ PROPERTIES_FILE;
628+
return PropertiesProvider.loadProperty(
629+
ServiceOptions.class, projectPropertiesPath, PROPERTIES_VERSION_KEY);
640630
} catch (Exception e) {
641631
// ignore
642632
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testBaseHashCode() {
6666
public void testHeader() {
6767
String expectedHeaderPattern = "^gl-java/.* gccl/.*";
6868
assertTrue(Pattern.compile(expectedHeaderPattern)
69-
.matcher(DEFAULT_OPTIONS.getXGoogApiClientHeader())
69+
.matcher(HttpTransportOptions.getXGoogApiClientHeader("google-cloud-core"))
7070
.find());
7171
}
7272
}

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

Lines changed: 6 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

@@ -273,7 +271,8 @@ public void testLibraryName() {
273271

274272
@Test
275273
public void testApplicationName() {
276-
assertTrue(APPLICATION_NAME_PATTERN.matcher(OPTIONS.getApplicationName()).matches());
274+
assertTrue(APPLICATION_NAME_PATTERN.matcher(
275+
OPTIONS.getApplicationName("google-cloud-core")).matches());
277276
}
278277

279278
@Test

0 commit comments

Comments
 (0)