Add standalone bazel build

Test plan:

* Build artifacts
* Deploy javamelody-deps_deploy.jar to lib folder
* Deploy javamelody.jar to plugins folder
* Start Gerrit and verify Monitoring tab is shown and functional
* Verify no database statistics are shown in Monitoring page
* Add configuration to gerrit.config to activate datasource
   interception
* Restart Gerrit and verify database statistics are now shown in
  Monitoring page
* Generate project classpath and import it in Eclipse.

Change-Id: I6b7ce8a5abcb4a03798fc1a96f31df3df8ac060f
diff --git a/.gitignore b/.gitignore
index 97e2fa7..b470f50 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,7 @@
 /.classpath
 /.idea
+/.primary_build_tool
 /.project
 /.settings
+/bazel-*
+/eclipse-out/
diff --git a/BUILD b/BUILD
index 076068f..0316bc3 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,4 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load("//tools/bzl:plugin.bzl", "gerrit_plugin", "PLUGIN_DEPS_NEVERLINK")
 
 gerrit_plugin(
     name = "javamelody",
@@ -26,8 +26,8 @@
 java_library(
     name = "javamelody-datasource-interceptor-lib",
     srcs = ["src/main/java/com/googlesource/gerrit/plugins/javamelody/MonitoringDataSourceInterceptor.java"],
-    deps = [
-        "//gerrit-plugin-api:lib-neverlink",
+    visibility = ["//visibility:public"],
+    deps = PLUGIN_DEPS_NEVERLINK + [
         "@javamelody_lib//jar",
         "@jrobin_lib//jar",
     ],
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..fa84575
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,30 @@
+workspace(name = "javamelody")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "4c6f78d8f6351fd77af32ca7871401758953ea4c",
+    #local_path = "/home/<user>/projects/bazlets",
+)
+
+#Snapshot Plugin API
+#load(
+#    "@com_googlesource_gerrit_bazlets//:gerrit_api_maven_local.bzl",
+#    "gerrit_api_maven_local",
+#)
+
+# Load snapshot Plugin API
+#gerrit_api_maven_local()
+
+# Release Plugin API
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
+    "gerrit_api",
+)
+
+# Load release Plugin 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..e14e488
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,17 @@
+NAME = "com_googlesource_gerrit_bazlets"
+
+def load_bazlets(
+    commit,
+    local_path = None
+  ):
+  if not local_path:
+      native.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 7e829b8..b78dc63 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -1,12 +1,75 @@
 Build
 =====
 
-This plugin is built with Bazel.
+This plugin is built with Bazel and two build modes are supported:
 
-Clone (or link) this plugin to the `plugins` directory of Gerrit's source tree.
+* Standalone
+* In Gerrit tree.
 
-Put the external dependency Bazel build file into the Gerrit /plugins directory,
-replacing the existing empty one.
+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-genfiles/@PLUGIN@.jar
+```
+
+Then to build the plugin dependencies execute:
+
+```
+bazel build @PLUGIN@-deps_deploy.jar
+```
+
+The output is created in
+
+```
+  bazel-bin/@PLUGIN@-deps_deploy.jar
+```
+
+Given the @PLUGIN@.jar does not contains the plugin dependencies, the @PLUGIN@.jar
+should be deployed to the `gerrit_site/plugins` folder as usual and the bazel-bin/@PLUGIN@-deps_deploy.jar should be deployed to the `gerrit_site/lib`
+folder:
+
+```
+cp bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar `$gerrit_site/plugins`
+cp bazel-bin/plugins/@PLUGIN@/@PLUGIN@-deps_deploy.jar `$gerrit_site/lib`
+```
+
+To enable the plugin database interception capabilities follow the instructions
+in [database monitoring](database-monitoring.md) page.
+
+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
+```
+
+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. Put the external dependency Bazel build file into
+the Gerrit /plugins directory, replacing the existing empty one.
 
 ```
   cd gerrit/plugins
@@ -20,7 +83,7 @@
   bazel build plugins/javamelody:javamelody
 ```
 
-Note, that the plugin dependencies with arethe [database interception](database-monitoring.md)
+Note, that the plugin dependencies with the [database interception](database-monitoring.md)
 are built separately. To do that, issue this command:
 
 ```
@@ -49,3 +112,7 @@
 
 More information about Bazel can be found in the [Gerrit
 documentation](../../../Documentation/dev-bazel.html).
+
+[Back to @PLUGIN@ documentation index][index]
+
+[index]: index.html
diff --git a/tools/bazel.rc b/tools/bazel.rc
new file mode 100644
index 0000000..4ed16cf
--- /dev/null
+++ b/tools/bazel.rc
@@ -0,0 +1,2 @@
+build --workspace_status_command=./tools/workspace-status.sh
+test --build_tests_only
diff --git a/tools/bzl/BUILD b/tools/bzl/BUILD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/bzl/BUILD
diff --git a/tools/bzl/classpath.bzl b/tools/bzl/classpath.bzl
new file mode 100644
index 0000000..dfcbe9c
--- /dev/null
+++ b/tools/bzl/classpath.bzl
@@ -0,0 +1,2 @@
+load("@com_googlesource_gerrit_bazlets//tools:classpath.bzl",
+     "classpath_collector")
diff --git a/tools/bzl/maven_jar.bzl b/tools/bzl/maven_jar.bzl
new file mode 100644
index 0000000..2eabedb
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", "maven_jar")
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..2a4dffb
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,6 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_DEPS_NEVERLINK",
+)
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..8d9271e
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,18 @@
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+load("//tools/bzl:plugin.bzl", "PLUGIN_DEPS")
+
+java_library(
+    name = "classpath",
+    testonly = 1,
+    runtime_deps = [
+        "//:javamelody-datasource-interceptor-lib",
+    ],
+)
+
+classpath_collector(
+    name = "main_classpath_collect",
+    testonly = 1,
+    deps = PLUGIN_DEPS + [
+        "//:javamelody-datasource-interceptor-lib",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..9c9ad72
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# Copyright (C) 2017 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 javamelody -r .
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..2fcb4c2
--- /dev/null
+++ b/tools/workspace-status.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+# 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.
+
+function rev() {
+  cd $1 && git describe --always --match "v[0-9].*" --dirty
+}
+
+echo STABLE_BUILD_JAVAMELODY_LABEL "$(rev .)"