Add standalone bazel build

Change-Id: I103a23276950812f09b2654ba8dd56e7a741b9af
diff --git a/.gitignore b/.gitignore
index 80d6257..0b6dfbc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,6 @@
 /.project
 /.settings/org.maven.ide.eclipse.prefs
 /.settings/org.eclipse.m2e.core.prefs
+/.primary_build_tool
+/bazel-*
+/eclipse-out
diff --git a/BUILD b/BUILD
index 1df8d3e..6c72d6d 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,8 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load(
+    "//tools/bzl:plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+)
 
 gerrit_plugin(
     name = "changemessage",
@@ -10,3 +14,12 @@
     ],
     resources = glob(["src/main/resources/**/*"]),
 )
+
+java_library(
+    name = "changemessage__plugin_classpath_deps",
+    testonly = 1,
+    visibility = ["//visibility:public"],
+    exports = PLUGIN_DEPS + [
+        ":changemessage__plugin",
+    ],
+)
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..830ff1a
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,26 @@
+workspace(name = "changemessage")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "42bffc66c0e92753133e4cea2debe65abc359c4d",
+    #    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()
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 ccbbb43..7c6076b 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -3,6 +3,9 @@
 
 This plugin is built with Bazel.
 
+Build in Gerrit tree
+--------------------
+
 Clone (or link) this plugin to the `plugins` directory of Gerrit's source tree.
 
 Then issue
@@ -26,3 +29,24 @@
 ```
   ./tools/eclipse/project.py
 ```
+
+Build standalone
+----------------
+
+To build the plugin, issue the following command:
+
+```
+bazel build @PLUGIN@
+```
+
+The output is created in
+
+```
+bazel-genfiles/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+./tools/eclipse/project.sh
+```
diff --git a/tools/bazel.rc b/tools/bazel.rc
new file mode 100644
index 0000000..2210511
--- /dev/null
+++ b/tools/bazel.rc
@@ -0,0 +1 @@
+build --workspace_status_command=./tools/workspace-status.sh
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..d5764f7
--- /dev/null
+++ b/tools/bzl/classpath.bzl
@@ -0,0 +1,4 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:classpath.bzl",
+    "classpath_collector",
+)
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..956dd59
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,5 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+)
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..75e5778
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,9 @@
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+
+classpath_collector(
+    name = "main_classpath_collect",
+    testonly = 1,
+    deps = [
+        "//:changemessage__plugin_classpath_deps",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..d3d33e4
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,19 @@
+#!/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 changemessage -r .
+
+
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..df718ad
--- /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_CHANGEMESSAGE_LABEL $(rev .)