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 all commits
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
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -16,3 +16,6 @@ bazel-*
# IntelliJ
.idea
out

# Auto-generated files.
gax/src/main/resources/dependencies.properties
14 changes: 13 additions & 1 deletion build.gradle
Expand Up @@ -40,6 +40,15 @@ googleJavaFormat {
exclude 'build/**'
exclude 'bazel*/**'
}

task generateProjectProperties {
ext.outputFile = file("gax/src/main/resources/dependencies.properties")
outputs.file(outputFile)
doLast {
outputFile.text = "version.gax=${project.version}"
}
}

// google-java-format-gradle-plugin:0.8 does not work with Java 1.7.
verifyGoogleJavaFormat.onlyIf { JavaVersion.current().isJava8Compatible() }

Expand Down Expand Up @@ -84,6 +93,7 @@ allprojects {
}
}
test.dependsOn verifyLicense
test.dependsOn generateProjectProperties

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
Expand Down Expand Up @@ -159,13 +169,15 @@ subprojects {

check.dependsOn jacocoTestReport


// Source jar
// ----------

task sourcesJar(type: Jar, dependsOn: classes) {
dependsOn generateProjectProperties
Copy link
Contributor

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.

Copy link
Contributor Author

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.

classifier = 'sources'
from sourceSets.main.allSource, sourceSets.test.allSource

from sourceSets.main.allSource, sourceSets.test.allSource, sourceSets.main.resources.srcDirs
exclude('**/*Test.java')
}

Expand Down
10 changes: 7 additions & 3 deletions gax/src/main/java/com/google/api/gax/core/GaxProperties.java
Expand Up @@ -57,9 +57,13 @@ 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.

Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Expand Down
57 changes: 57 additions & 0 deletions gax/src/test/java/com/google/api/gax/core/GaxPropertiesTest.java
@@ -0,0 +1,57 @@
/*
* Copyright 2020 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.core;

import static org.junit.Assert.assertTrue;

import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GaxPropertiesTest {

@Test
public void testGaxVersion() {
String gaxVersion = GaxProperties.getGaxVersion();
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(gaxVersion).find());
String[] versionComponents = gaxVersion.split("\\.");
// This test was added in version 1.56.0, so check that the major and minor numbers are greater
// than that.
int major = Integer.parseInt(versionComponents[0]);
int minor = Integer.parseInt(versionComponents[1]);

assertTrue(major >= 1);
if (major == 1) {
assertTrue(minor >= 56);
}
}
}