Add standalone bazel build + Eclipse support

Based upon plugins/javamelody

Change-Id: I6b3686c1dce5335c9ccc9d8f1f279374aa51b229
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..dc8a4f2
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1 @@
+build --workspace_status_command="python ./tools/workspace_status.py"
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f47d0cd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+/.apt_generated
+/.classpath
+/.idea
+/.primary_build_tool
+/.project
+/.settings
+/bazel-*
+/eclipse-out/
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..a086685
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,19 @@
+workspace(name = "metrics_reporter_jmx")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "2807d30761a889187f06d99e969fb0ea2fbe5afe",
+    #local_path = "/home/<user>/projects/bazlets",
+)
+
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
+    "gerrit_api",
+)
+
+gerrit_api()
+
+load("//:external_plugin_deps.bzl", "external_plugin_deps")
+
+external_plugin_deps()
diff --git a/bazlets.bzl b/bazlets.bzl
new file mode 100644
index 0000000..f089af4
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,18 @@
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
+
+NAME = "com_googlesource_gerrit_bazlets"
+
+def load_bazlets(
+        commit,
+        local_path = None):
+    if not local_path:
+        git_repository(
+            name = NAME,
+            remote = "https://gerrit.googlesource.com/bazlets",
+            commit = commit,
+        )
+    else:
+        native.local_repository(
+            name = NAME,
+            path = local_path,
+        )
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index 1bfd046..caa70ac 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -4,6 +4,53 @@
 This plugin is built with Bazel.  To install Bazel, follow
 the instruction on: https://www.bazel.io/versions/master/docs/install.html.
 
+Two build modes are supported:
+* Standalone
+* In Gerrit tree
+
+Standalone build mode is recommended, as this mode doesn't require local Gerrit
+tree to exist.
+
+# Build standalone
+
+To build the plugin, issue the following command:
+
+```
+  bazel build @PLUGIN@.jar
+```
+
+The output is created in
+
+```
+  bazel-bin/@PLUGIN@.jar
+```
+
+To package the plugin sources run:
+
+```
+  bazel build lib@PLUGIN@__plugin-src.jar
+```
+
+The output is created in:
+
+```
+  bazel-bin/lib@PLUGIN@__plugin-src.jar
+```
+
+To execute the tests run:
+
+```
+  bazel test //...
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./tools/eclipse/project.sh
+```
+
+## Build in Gerrit tree
+
 Clone (or link) this plugin to the `plugins` directory of Gerrit's
 source tree, and issue the command:
 
diff --git a/tools/BUILD b/tools/BUILD
new file mode 100644
index 0000000..c5ed0b7
--- /dev/null
+++ b/tools/BUILD
@@ -0,0 +1 @@
+# Empty file required by Bazel
diff --git a/tools/bzl/BUILD b/tools/bzl/BUILD
new file mode 100644
index 0000000..c5ed0b7
--- /dev/null
+++ b/tools/bzl/BUILD
@@ -0,0 +1 @@
+# Empty file required by Bazel
diff --git a/tools/bzl/classpath.bzl b/tools/bzl/classpath.bzl
new file mode 100644
index 0000000..c921d01
--- /dev/null
+++ b/tools/bzl/classpath.bzl
@@ -0,0 +1,6 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:classpath.bzl",
+    _classpath_collector = "classpath_collector",
+)
+
+classpath_collector = _classpath_collector
diff --git a/tools/bzl/maven_jar.bzl b/tools/bzl/maven_jar.bzl
new file mode 100644
index 0000000..35ea8ce
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1,3 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", _maven_jar = "maven_jar")
+
+maven_jar = _maven_jar
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..a271493
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,12 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    _gerrit_plugin = "gerrit_plugin",
+    _plugin_deps = "PLUGIN_DEPS",
+    _plugin_deps_neverlink = "PLUGIN_DEPS_NEVERLINK",
+    _plugin_test_deps = "PLUGIN_TEST_DEPS",
+)
+
+gerrit_plugin = _gerrit_plugin
+PLUGIN_DEPS = _plugin_deps
+PLUGIN_DEPS_NEVERLINK = _plugin_deps_neverlink
+PLUGIN_TEST_DEPS = _plugin_test_deps
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..f1ffc2d
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,10 @@
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+load("//tools/bzl:plugin.bzl", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS")
+
+classpath_collector(
+    name = "main_classpath_collect",
+    testonly = 1,
+    deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        "//:metrics-reporter-jmx__plugin",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..a5deec4
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# Copyright (C) 2021 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.
+`bazel query @com_googlesource_gerrit_bazlets//tools/eclipse:project --output location | sed s/BUILD:.*//`project.py -n metrics-reporter-jmx -r .
diff --git a/tools/workspace_status.py b/tools/workspace_status.py
new file mode 100644
index 0000000..10d1262
--- /dev/null
+++ b/tools/workspace_status.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+# This script will be run by bazel when the build process starts to
+# generate key-value information that represents the status of the
+# workspace. The output should be like
+#
+# KEY1 VALUE1
+# KEY2 VALUE2
+#
+# If the script exits with non-zero code, it's considered as a failure
+# and the output will be discarded.
+
+from __future__ import print_function
+import subprocess
+import sys
+
+CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
+
+
+def revision():
+    try:
+        return subprocess.check_output(CMD).strip().decode("utf-8")
+    except OSError as err:
+        print('could not invoke git: %s' % err, file=sys.stderr)
+        sys.exit(1)
+    except subprocess.CalledProcessError as err:
+        print('error using git: %s' % err, file=sys.stderr)
+        sys.exit(1)
+
+
+print("STABLE_BUILD_METRICS-REPORTER-JMX_LABEL %s" % revision())