Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

[gax-java] fix: make GaxProperties.version accessible in Android #1117

Merged
merged 6 commits into from Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -164,7 +164,7 @@ subprojects {

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource, sourceSets.test.allSource
from sourceSets.main.allSource, sourceSets.test.allSource, sourceSets.main.resources.srcDirs
}

// JavaDoc
Expand Down
12 changes: 8 additions & 4 deletions gax/src/main/java/com/google/api/gax/core/GaxProperties.java
Expand Up @@ -57,12 +57,16 @@ public static String getLibraryVersion(Class<?> libraryClass) {
* method are expected to be cached.
*/
public static String getLibraryVersion(Class<?> libraryClass, String propertyName) {
String version = getLibraryVersion(libraryClass);
if (!DEFAULT_VERSION.equals(version)) {
return version;
String version = null;
// Always read GaxProperties' version from the properties file.
if (!libraryClass.equals(GaxProperties.class)) {
version = getLibraryVersion(libraryClass);
if (!DEFAULT_VERSION.equals(version)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the if? seems we can return unconditionally here if it is the default version. Or do you want to read from gradle.properties instead if this is the default version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this seems applicable to any generic library class (i.e. keep reading if the version is empty, aka DEFAULT_VERSION), I was trying to keep the non-GaxProperties logic the same.

return version;
}
}

try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Most probably this will break execution as a bazel artifact (i.e. when a client library is build with bazel and executed from bazel context). In any case, introducing another properties file will duplicate version information in two places. Plus, the version in dependencies.properties is automatically bumped by Yoshi's tools during releases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to this PR in google-http-java-client. Note that dependencies.properties was never being pulled into the Android JAR. This file is also suboptimal to include since it includes every dependency, which we don't necessarily want gax-external developers to parse.

On the other hand, gradle.properties is a standard artifact that is expected to be provided in many libraries and works on both Android and plain Java binaries.

Copy link
Contributor

@vam-google vam-google Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr; Please strongly consider just making dependencies.properties work in android env.

google-http-java-client is not used via bazel, that is why it does not matter for it. Also it does not have dependencies.properites.

Note, dependencies.properties is used in any scenario (with or without bazel). It is the source of truth for the version.

As for gradle.properties, it makes gax-java as java artifact depend on its build system (gradle, which is one of many) during runtime. The same artifact can be built by maven, for example, which makes gradle.properties an alien thing there. I guess it is also not even included in bazel artfiact unlike dependencies.properties (https://github.com/googleapis/gax-java/blob/master/gax/BUILD.bazel#L43). If you run bazel test //... for gax-java, does it work (most probably it will not)?

In any case, most importantly, this value is not updated automatically so after the next release it will start reporting wrong version number. There is TODO, but it is not sufficient (after all this PR fixes one relatively small issue, so introducing a future issue with it is not "cost efficient").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we shouldn't include all of dependencies.properties, so here's a workaround. I'm now generating a new dependencies.properties that pulls in version.gax at build time. This still works with bazel test //..., ./gradlew test, Android, and plain Java binaries.

We also did not have a GaxPropertiesTest before, so I've added one.

try (InputStream in = libraryClass.getResourceAsStream("/gradle.properties")) {
if (in != null) {
Properties props = new Properties();
props.load(in);
Expand Down
2 changes: 2 additions & 0 deletions gax/src/main/resources/gradle.properties
@@ -0,0 +1,2 @@
# TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync)
version.gax = "1.56.0"