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

Commit ecbab4a

Browse files
authored
[gax-java] fix: make GaxProperties.version accessible in Android (#1117)
* fix: make GaxProperties.version accessible in Android * fix: add GaxPropertiesTest, generate dependencies.properties * fix: typos, remove untracked files * fix: move generated properties to a buildDir * fix: revert back to second-best approach to fix CI builds
1 parent cf1bce8 commit ecbab4a

4 files changed

Lines changed: 80 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ bazel-*
1616
# IntelliJ
1717
.idea
1818
out
19+
20+
# Auto-generated files.
21+
gax/src/main/resources/dependencies.properties

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ googleJavaFormat {
4040
exclude 'build/**'
4141
exclude 'bazel*/**'
4242
}
43+
44+
task generateProjectProperties {
45+
ext.outputFile = file("gax/src/main/resources/dependencies.properties")
46+
outputs.file(outputFile)
47+
doLast {
48+
outputFile.text = "version.gax=${project.version}"
49+
}
50+
}
51+
4352
// google-java-format-gradle-plugin:0.8 does not work with Java 1.7.
4453
verifyGoogleJavaFormat.onlyIf { JavaVersion.current().isJava8Compatible() }
4554

@@ -84,6 +93,7 @@ allprojects {
8493
}
8594
}
8695
test.dependsOn verifyLicense
96+
test.dependsOn generateProjectProperties
8797

8898
gradle.projectsEvaluated {
8999
tasks.withType(JavaCompile) {
@@ -159,13 +169,15 @@ subprojects {
159169

160170
check.dependsOn jacocoTestReport
161171

172+
162173
// Source jar
163174
// ----------
164175

165176
task sourcesJar(type: Jar, dependsOn: classes) {
177+
dependsOn generateProjectProperties
166178
classifier = 'sources'
167-
from sourceSets.main.allSource, sourceSets.test.allSource
168179

180+
from sourceSets.main.allSource, sourceSets.test.allSource, sourceSets.main.resources.srcDirs
169181
exclude('**/*Test.java')
170182
}
171183

gax/src/main/java/com/google/api/gax/core/GaxProperties.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ public static String getLibraryVersion(Class<?> libraryClass) {
5757
* method are expected to be cached.
5858
*/
5959
public static String getLibraryVersion(Class<?> libraryClass, String propertyName) {
60-
String version = getLibraryVersion(libraryClass);
61-
if (!DEFAULT_VERSION.equals(version)) {
62-
return version;
60+
String version = null;
61+
// Always read GaxProperties' version from the properties file.
62+
if (!libraryClass.equals(GaxProperties.class)) {
63+
version = getLibraryVersion(libraryClass);
64+
if (!DEFAULT_VERSION.equals(version)) {
65+
return version;
66+
}
6367
}
6468

6569
try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
package com.google.api.gax.core;
31+
32+
import static org.junit.Assert.assertTrue;
33+
34+
import java.util.regex.Pattern;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
39+
@RunWith(JUnit4.class)
40+
public class GaxPropertiesTest {
41+
42+
@Test
43+
public void testGaxVersion() {
44+
String gaxVersion = GaxProperties.getGaxVersion();
45+
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(gaxVersion).find());
46+
String[] versionComponents = gaxVersion.split("\\.");
47+
// This test was added in version 1.56.0, so check that the major and minor numbers are greater
48+
// than that.
49+
int major = Integer.parseInt(versionComponents[0]);
50+
int minor = Integer.parseInt(versionComponents[1]);
51+
52+
assertTrue(major >= 1);
53+
if (major == 1) {
54+
assertTrue(minor >= 56);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)