Skip to content

Commit

Permalink
Add git revision to ./bin/alluxio version
Browse files Browse the repository at this point in the history
In light of preserving behavior, the command behaves the same as in
previous versions. This change adds the -r option which can be specified
optionally so that users can see part or all of the revision output
appended to the end of the normal version string.

Added two new maven plugins to retrieve the git revision.

Closes #9930

pr-link: #9936
change-id: cid-b4a1836b31b57881a1f9ae675dd53fe4d7534270
  • Loading branch information
ZacBlanco authored and alluxio-bot committed Sep 24, 2019
1 parent 7681087 commit 2e67f3b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
34 changes: 34 additions & 0 deletions core/common/pom.xml
Expand Up @@ -145,6 +145,40 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>create-metadata</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/generated/build-metadata</outputDirectory>
<outputName>build.properties</outputName>
<revisionPropertyName>git.revision</revisionPropertyName>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.build.directory}/generated/build-metadata/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
Expand Down
Expand Up @@ -17,6 +17,8 @@
public final class ProjectConstants {
/* Project version, specified in maven property. **/
public static final String VERSION = "${project.version}";
/* The latest git revision of at the time of building**/
public static final String REVISION = "${git.revision}";
/* Hadoop version, specified in maven property. **/
public static final String HADOOP_VERSION = "${hadoop.version}";
/* Whether update check is enabled. **/
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Expand Up @@ -916,6 +916,11 @@
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand All @@ -926,6 +931,11 @@
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
Expand Down
50 changes: 49 additions & 1 deletion shell/src/main/java/alluxio/cli/Version.java
Expand Up @@ -11,8 +11,17 @@

package alluxio.cli;

import alluxio.ProjectConstants;
import alluxio.RuntimeConstants;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import javax.annotation.concurrent.ThreadSafe;

/**
Expand All @@ -21,6 +30,15 @@
@ThreadSafe
public final class Version {

private static final Option REVISION_OPT = Option.builder("r")
.longOpt("revision")
.desc("Prints the git revision along with the Alluxio version. Optionally specify the "
+ "revision length")
.optionalArg(true)
.argName("revision_length")
.hasArg(true)
.build();

private Version() {} // prevent instantiation

/**
Expand All @@ -29,6 +47,36 @@ private Version() {} // prevent instantiation
* @param args the arguments
*/
public static void main(String[] args) {
System.out.println(RuntimeConstants.VERSION);
CommandLineParser parser = new DefaultParser();
Options opts = new Options().addOption(REVISION_OPT);
try {
CommandLine cl = parser.parse(opts, args);
String revision = "";
if (cl.hasOption(REVISION_OPT.getOpt())) {
String r = cl.getOptionValue(REVISION_OPT.getOpt());
int revisionLen = r == null ? ProjectConstants.REVISION.length() :
Integer.parseInt(r);
revision = String.format("-%s", ProjectConstants.REVISION.substring(0,
Math.min(revisionLen, ProjectConstants.REVISION.length())));
}
System.out.println(String.format("%s%s", RuntimeConstants.VERSION, revision));
System.exit(0);
} catch (ParseException e) {
printUsage(opts);
} catch (NumberFormatException e) {
System.out.println(String.format("Couldn't parse <%s> as an integer",
REVISION_OPT.getValue()));
printUsage(opts);
}
System.exit(1);
}

/**
* Prints the usage of the version command.
*
* @param options the set of command line options to put in the help text
*/
public static void printUsage(Options options) {
new HelpFormatter().printHelp("version", options);
}
}

0 comments on commit 2e67f3b

Please sign in to comment.