
Previously, the Version resource used by Version.java was compiled directly into the war, rather than being included in the java_library alongside the class that used it. The intended effect was to produce a version of "(dev)" when running from Eclipse, and an accurate version when running from the war. However, it made the build graph a little confusing, and moreover made it impossible to write a test that tests the actual format of the "git describe" output. Rearrange the build rules to include Version in the common:server library. This actually still results in "(dev)" when run from Eclipse, because there is no jar for this library in the Eclipse classpath, so looking up the resource will still fail as expected. Change-Id: I26e38a8c37aef9e1872faed1ab40d003e6dea946
67 lines
1.9 KiB
Java
67 lines
1.9 KiB
Java
// Copyright (C) 2009 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.gerrit.common;
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
|
import com.google.common.annotations.GwtIncompatible;
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@GwtIncompatible("Unemulated com.google.gerrit.common.Version")
|
|
public class Version {
|
|
private static final Logger log = LoggerFactory.getLogger(Version.class);
|
|
|
|
@VisibleForTesting static final String DEV = "(dev)";
|
|
|
|
private static final String VERSION;
|
|
|
|
public static String getVersion() {
|
|
return VERSION;
|
|
}
|
|
|
|
static {
|
|
VERSION = loadVersion();
|
|
}
|
|
|
|
private static String loadVersion() {
|
|
try (InputStream in = Version.class.getResourceAsStream("Version")) {
|
|
if (in == null) {
|
|
return DEV;
|
|
}
|
|
try (BufferedReader r = new BufferedReader(new InputStreamReader(in, UTF_8))) {
|
|
String vs = r.readLine();
|
|
if (vs != null && vs.startsWith("v")) {
|
|
vs = vs.substring(1);
|
|
}
|
|
if (vs != null && vs.isEmpty()) {
|
|
vs = null;
|
|
}
|
|
return vs;
|
|
}
|
|
} catch (IOException e) {
|
|
log.error(e.getMessage(), e);
|
|
return "(unknown version)";
|
|
}
|
|
}
|
|
|
|
private Version() {}
|
|
}
|