Skip to content

Commit a6c6dec

Browse files
authored
fix: grab version from package metadata (#1419)
* fix: detect version from packaging * test: add test ensuring version constants are present * chore: only run checkstyle on Java 8+
1 parent a65e99a commit a6c6dec

File tree

5 files changed

+106
-22
lines changed

5 files changed

+106
-22
lines changed

google-api-client/pom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
</extension>
1919
</extensions>
2020
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-resources-plugin</artifactId>
24+
<executions>
25+
<execution>
26+
<goals>
27+
<goal>resources</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
</plugin>
2132
<plugin>
2233
<artifactId>maven-javadoc-plugin</artifactId>
2334
<configuration>
@@ -94,6 +105,16 @@
94105
</executions>
95106
</plugin>
96107
</plugins>
108+
109+
<resources>
110+
<resource>
111+
<directory>src/main/resources</directory>
112+
</resource>
113+
<resource>
114+
<directory>src/main/properties</directory>
115+
<filtering>true</filtering>
116+
</resource>
117+
</resources>
97118
</build>
98119
<dependencies>
99120
<dependency>

google-api-client/src/main/java/com/google/api/client/googleapis/GoogleUtils.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
import com.google.api.client.util.SecurityUtils;
1818
import com.google.common.annotations.VisibleForTesting;
19-
2019
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.security.GeneralSecurityException;
2322
import java.security.KeyStore;
23+
import java.util.Properties;
2424
import java.util.regex.Matcher;
2525
import java.util.regex.Pattern;
2626

@@ -32,11 +32,8 @@
3232
*/
3333
public final class GoogleUtils {
3434

35-
// NOTE: toString() so compiler thinks it isn't a constant, so it won't inline it
36-
// {x-version-update-start:google-api-client:current}
3735
/** Current release version. */
38-
public static final String VERSION = "1.30.3".toString();
39-
// {x-version-update-end:google-api-client:current}
36+
public static final String VERSION = getVersion();
4037

4138
// NOTE: Integer instead of int so compiler thinks it isn't a constant, so it won't inline it
4239
/**
@@ -91,5 +88,24 @@ public static synchronized KeyStore getCertificateTrustStore()
9188
return certTrustStore;
9289
}
9390

91+
private static String getVersion() {
92+
String version = GoogleUtils.class.getPackage().getImplementationVersion();
93+
// in a non-packaged environment (local), there's no implementation version to read
94+
if (version == null) {
95+
// fall back to reading from a properties file - note this value is expected to be cached
96+
try (InputStream inputStream =
97+
GoogleUtils.class.getResourceAsStream("google-api-client.properties")) {
98+
if (inputStream != null) {
99+
Properties properties = new Properties();
100+
properties.load(inputStream);
101+
version = properties.getProperty("google-api-client.version");
102+
}
103+
} catch (IOException e) {
104+
// ignore
105+
}
106+
}
107+
return version;
108+
}
109+
94110
private GoogleUtils() {}
95111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-client.version=${project.version}

google-api-client/src/test/java/com/google/api/client/googleapis/GoogleUtilsTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ public void testVersionMatcherSnapshot() {
5656
assertEquals(30, Integer.parseInt(matcher.group(2)));
5757
assertEquals(3, Integer.parseInt(matcher.group(3)));
5858
}
59+
60+
public void testVersion() {
61+
Matcher matcher = GoogleUtils.VERSION_PATTERN.matcher(GoogleUtils.VERSION);
62+
assertTrue(matcher.find());
63+
assertNotNull(GoogleUtils.MAJOR_VERSION);
64+
assertNotNull(GoogleUtils.MINOR_VERSION);
65+
assertNotNull(GoogleUtils.BUGFIX_VERSION);
66+
}
5967
}

pom.xml

+55-17
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@
297297
</configuration>
298298
</plugin>
299299
<plugin>
300+
<groupId>org.apache.maven.plugins</groupId>
300301
<artifactId>maven-checkstyle-plugin</artifactId>
301-
<version>2.6</version>
302+
<version>3.1.0</version>
302303
</plugin>
303304
<plugin>
304305
<groupId>org.codehaus.mojo</groupId>
@@ -325,6 +326,11 @@
325326
<artifactId>maven-site-plugin</artifactId>
326327
<version>3.8.2</version>
327328
</plugin>
329+
<plugin>
330+
<groupId>org.apache.maven.plugins</groupId>
331+
<artifactId>maven-resources-plugin</artifactId>
332+
<version>3.1.0</version>
333+
</plugin>
328334
</plugins>
329335
</pluginManagement>
330336
<plugins>
@@ -412,22 +418,6 @@
412418
</execution>
413419
</executions>
414420
</plugin>
415-
<plugin>
416-
<groupId>org.apache.maven.plugins</groupId>
417-
<artifactId>maven-checkstyle-plugin</artifactId>
418-
<configuration>
419-
<configLocation>checkstyle.xml</configLocation>
420-
<consoleOutput>true</consoleOutput>
421-
<suppressionsLocation>${basedir}/../checkstyle-suppressions.xml</suppressionsLocation>
422-
</configuration>
423-
<executions>
424-
<execution>
425-
<goals>
426-
<goal>check</goal>
427-
</goals>
428-
</execution>
429-
</executions>
430-
</plugin>
431421
<plugin>
432422
<groupId>org.codehaus.mojo</groupId>
433423
<artifactId>findbugs-maven-plugin</artifactId>
@@ -546,5 +536,53 @@
546536
</plugins>
547537
</build>
548538
</profile>
539+
540+
<!-- set project.root-directory property based on where we are -->
541+
<profile>
542+
<id>root-directory</id>
543+
<activation>
544+
<file>
545+
<exists>checkstyle-suppressions.xml</exists>
546+
</file>
547+
</activation>
548+
<properties>
549+
<project.root-directory>.</project.root-directory>
550+
</properties>
551+
</profile>
552+
553+
<profile>
554+
<!-- Only run checkstyle plugin on Java 8+ (checkstyle artifact only supports Java 8+) -->
555+
<id>checkstyle-tests</id>
556+
<activation>
557+
<jdk>[1.8,)</jdk>
558+
</activation>
559+
<build>
560+
<plugins>
561+
<plugin>
562+
<groupId>org.apache.maven.plugins</groupId>
563+
<artifactId>maven-checkstyle-plugin</artifactId>
564+
<dependencies>
565+
<dependency>
566+
<groupId>com.puppycrawl.tools</groupId>
567+
<artifactId>checkstyle</artifactId>
568+
<version>8.23</version>
569+
</dependency>
570+
</dependencies>
571+
<configuration>
572+
<configLocation>checkstyle.xml</configLocation>
573+
<consoleOutput>true</consoleOutput>
574+
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
575+
</configuration>
576+
<executions>
577+
<execution>
578+
<goals>
579+
<goal>check</goal>
580+
</goals>
581+
</execution>
582+
</executions>
583+
</plugin>
584+
</plugins>
585+
</build>
586+
</profile>
549587
</profiles>
550588
</project>

0 commit comments

Comments
 (0)