Skip to content

Commit

Permalink
fix: grab version from package metadata (#1419)
Browse files Browse the repository at this point in the history
* fix: detect version from packaging

* test: add test ensuring version constants are present

* chore: only run checkstyle on Java 8+
  • Loading branch information
chingor13 committed Nov 14, 2019
1 parent a65e99a commit a6c6dec
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 22 deletions.
21 changes: 21 additions & 0 deletions google-api-client/pom.xml
Expand Up @@ -18,6 +18,17 @@
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
Expand Down Expand Up @@ -94,6 +105,16 @@
</executions>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/properties</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
Expand Down
Expand Up @@ -16,11 +16,11 @@

import com.google.api.client.util.SecurityUtils;
import com.google.common.annotations.VisibleForTesting;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -32,11 +32,8 @@
*/
public final class GoogleUtils {

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

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

private static String getVersion() {
String version = GoogleUtils.class.getPackage().getImplementationVersion();
// in a non-packaged environment (local), there's no implementation version to read
if (version == null) {
// fall back to reading from a properties file - note this value is expected to be cached
try (InputStream inputStream =
GoogleUtils.class.getResourceAsStream("google-api-client.properties")) {
if (inputStream != null) {
Properties properties = new Properties();
properties.load(inputStream);
version = properties.getProperty("google-api-client.version");
}
} catch (IOException e) {
// ignore
}
}
return version;
}

private GoogleUtils() {}
}
@@ -0,0 +1 @@
google-api-client.version=${project.version}
Expand Up @@ -56,4 +56,12 @@ public void testVersionMatcherSnapshot() {
assertEquals(30, Integer.parseInt(matcher.group(2)));
assertEquals(3, Integer.parseInt(matcher.group(3)));
}

public void testVersion() {
Matcher matcher = GoogleUtils.VERSION_PATTERN.matcher(GoogleUtils.VERSION);
assertTrue(matcher.find());
assertNotNull(GoogleUtils.MAJOR_VERSION);
assertNotNull(GoogleUtils.MINOR_VERSION);
assertNotNull(GoogleUtils.BUGFIX_VERSION);
}
}
72 changes: 55 additions & 17 deletions pom.xml
Expand Up @@ -297,8 +297,9 @@
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.6</version>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand All @@ -325,6 +326,11 @@
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down Expand Up @@ -412,22 +418,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<suppressionsLocation>${basedir}/../checkstyle-suppressions.xml</suppressionsLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
Expand Down Expand Up @@ -546,5 +536,53 @@
</plugins>
</build>
</profile>

<!-- set project.root-directory property based on where we are -->
<profile>
<id>root-directory</id>
<activation>
<file>
<exists>checkstyle-suppressions.xml</exists>
</file>
</activation>
<properties>
<project.root-directory>.</project.root-directory>
</properties>
</profile>

<profile>
<!-- Only run checkstyle plugin on Java 8+ (checkstyle artifact only supports Java 8+) -->
<id>checkstyle-tests</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.23</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

0 comments on commit a6c6dec

Please sign in to comment.