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

Commit

Permalink
[gax-java] fix: make GaxProperties.version accessible in Android (#1117)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
miraleung committed Jun 6, 2020
1 parent cf1bce8 commit ecbab4a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
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
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)) {
return version;
}
}

try (InputStream in = libraryClass.getResourceAsStream("/dependencies.properties")) {
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);
}
}
}

0 comments on commit ecbab4a

Please sign in to comment.