[gax-java] fix: make GaxProperties.version accessible in Android #1117
Conversation
I tested this in an Android app and standalone Java binary, and they both work. |
Codecov Report
@@ Coverage Diff @@
## master #1117 +/- ##
=========================================
Coverage 78.65% 78.66%
+ Complexity 1170 1169 -1
=========================================
Files 204 204
Lines 5187 5189 +2
Branches 416 417 +1
=========================================
+ Hits 4080 4082 +2
- Misses 932 933 +1
+ Partials 175 174 -1
Continue to review full report at Codecov.
|
// Always read GaxProperties' version from the properties file. | ||
if (!libraryClass.equals(GaxProperties.class)) { | ||
version = getLibraryVersion(libraryClass); | ||
if (!DEFAULT_VERSION.equals(version)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
} | ||
|
||
try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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").
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge to master
Merged to master. |
build.gradle
Outdated
@@ -40,6 +40,15 @@ googleJavaFormat { | |||
exclude 'build/**' | |||
exclude 'bazel*/**' | |||
} | |||
|
|||
task generateProjectProperties { | |||
ext.outputFile = file("gax/src/main/resources/dependencies.propertoes") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propertoes -> properties
version = getLibraryVersion(libraryClass); | ||
if (!DEFAULT_VERSION.equals(version)) { | ||
return version; | ||
} | ||
} | ||
|
||
try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads dependencies.properteis
, but generateProjectProperties
writes to the file with typo dependencies.propertoes
. Looks like the file (even if it did not contain a typo) is never read (and the tests pass without the need to copy the file).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -0,0 +1 @@ | |||
version.gax=1.56.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add newline char.
Please justify the need of copying the file. What harm does including the whole file makes?
Even if we do the copy, shouldn't it be created only as build artifacts but not included in git (otherwise it is an unnecessary duplication, and confusing source artifacts created during build)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed from git.
// Source jar | ||
// ---------- | ||
|
||
task sourcesJar(type: Jar, dependsOn: classes) { | ||
dependsOn generateProjectProperties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the generated file included - in -sources jar or in .class files jar? If it is in sources jar only, then it will not be accessible as a resource during runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.class files, because it's accessible when running tests or an Android APK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, please use your judgement
Found a way to keep the generated files only in the build directory. |
The optimal solution was to generate and write a subset of |
No description provided.