Bazel: Add standalone build mode

Test Plan:

1. Build the plugins:

  $ bazel build :all
  INFO: Analysed target //:all (26 packages loaded).
  INFO: Found 1 target...
  Target //:all up-to-date:
    bazel-genfiles/all.zip
  INFO: Elapsed time: 1.060s, Critical Path: 0.41s

2. Verify the content is sane:

  $ unzip -t bazel-genfiles/all.zip
  Archive:  bazel-genfiles/all.zip
      testing: owners.jar               OK
      testing: owners-autoassign.jar    OK

3. Run the tests:

  $ bazel test //...
  INFO: Found 1 test target...
  Target //owners-common:test up-to-date:
    bazel-bin/owners-common/test.jar
    bazel-bin/owners-common/test
  INFO: Elapsed time: 4.941s, Critical Path: 3.18s
  INFO: Build completed successfully, 13 total actions
  //owners-common:test                           PASSED in 1.9s

4. Generate Eclipse project:

   $ tools/eclipse/project.sh
   Verify project can be imported to Eclipse and dependencies are in the
   classpath

Change-Id: I211bd5acb293a36f9c12b428fbb0a4ceb4dfc177
diff --git a/.gitignore b/.gitignore
index 560ddee..2771034 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,7 +4,8 @@
 *.iml
 *.ipr
 .idea
-
-# Maven Relase
-pom.xml.*
-release.properties
+/.classpath
+/.project
+/.settings/
+/bazel-*
+/eclipse-out/
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..b52c2c1
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,29 @@
+load("//tools:genrule2.bzl", "genrule2")
+load("//tools/bzl:plugin.bzl", "PLUGIN_TEST_DEPS")
+load("//owners-common:common.bzl", "EXTERNAL_DEPS")
+
+genrule2(
+    name = "all",
+    srcs = [
+        "//owners",
+        "//owners-autoassign",
+        "//owners-common",
+    ],
+    outs = ["all.zip"],
+    cmd = " && ".join([
+        "cp $(SRCS) $$TMP",
+        "cd $$TMP",
+        "zip -qr $$ROOT/$@ .",
+    ]),
+)
+
+java_library(
+    name = "owners_classpath_deps",
+    testonly = 1,
+    visibility = ["//visibility:public"],
+    exports = EXTERNAL_DEPS + PLUGIN_TEST_DEPS + [
+        "//owners:owners__plugin",
+        "//owners-autoassign:owners-autoassign__plugin",
+        "//owners-common:owners-common__plugin",
+    ],
+)
diff --git a/README.md b/README.md
index 72adf36..aee2d8e 100644
--- a/README.md
+++ b/README.md
@@ -39,8 +39,46 @@
 
 ## How to build
 
-Create three symbolic links of the owners-owners, owners-common and owners-autoassign
-from the Gerrit source code /plugins directory to the subdirectories of this project.
+This plugin is built with Bazel and two build modes are supported:
+
+ * Standalone
+ * In Gerrit tree
+
+### Build standalone
+
+To build the plugin, issue the following command:
+
+```
+  bazel build :all
+```
+
+The output is created in
+
+```
+  bazel-genfiles/owners/owners.jar
+  bazel-genfiles/owners-autoassign/owners-autoassign.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
+
+Create symbolic links of the owners and owners-autoassign folders and of the
+external_plugin_deps.bzl file to the Gerrit source code /plugins directory.
+
+Create a symbolic link of the owners-common plugin to the Gerrit source code
+directory.
 
 Then build the owners and owners-autoassign plugins with the usual Gerrit
 plugin compile command.
@@ -63,3 +101,25 @@
 and does not need to be built separately being a direct dependency of the build
 process. Its resulting .jar must not be installed in gerrit plugins directory.
 
+The output is created in
+
+```
+  bazel-genfiles/plugins/owners/owners.jar
+  bazel-genfiles/plugins/owners-autoassign/owners-autoassign.jar
+```
+
+To execute the tests run:
+
+```
+  bazel test owners-common:test
+```
+
+This project can be imported into the Eclipse IDE:
+
+Add the plugin name to the `CUSTOM_PLUGINS` in Gerrit core in
+`tools/bzl/plugins.bzl` file and run:
+
+```
+  ./tools/eclipse/project.py
+```
+
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..0cfa89f
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,20 @@
+workspace(name = "owners")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "087da1f6ca119ad5c4bc443926426ddf44dcde3a",
+    #local_path = "/home/<user>/projects/bazlets",
+)
+
+# Release Plugin API
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
+    "gerrit_api",
+)
+
+gerrit_api()
+
+load(":external_plugin_deps_standalone.bzl", "external_plugin_deps_standalone")
+
+external_plugin_deps_standalone()
diff --git a/bazlets.bzl b/bazlets.bzl
new file mode 100644
index 0000000..de1b5d2
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,16 @@
+NAME = "com_googlesource_gerrit_bazlets"
+
+def load_bazlets(
+        commit = None,
+        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/external_plugin_deps.bzl b/external_plugin_deps.bzl
index 75a8801..a22be2b 100644
--- a/external_plugin_deps.bzl
+++ b/external_plugin_deps.bzl
@@ -2,33 +2,34 @@
 
 JACKSON_REV = "2.8.9"
 
-def external_plugin_deps():
+def external_plugin_deps(omit_jackson_core = True):
+    if not omit_jackson_core:
         maven_jar(
-          name = "jackson-core",
-          artifact = "com.fasterxml.jackson.core:jackson-core:" + JACKSON_REV,
-          sha1 = "82ad1c5f92f6dcc6291f5c46ebacb975eaa844de",
+            name = "jackson-core",
+            artifact = "com.fasterxml.jackson.core:jackson-core:" + JACKSON_REV,
+            sha1 = "569b1752705da98f49aabe2911cc956ff7d8ed9d",
         )
 
-        maven_jar(
-          name = "jackson-databind",
-          artifact = "com.fasterxml.jackson.core:jackson-databind:" + JACKSON_REV,
-          sha1 = "4dfca3975be3c1a98eacb829e70f02e9a71bc159",
-        )
+    maven_jar(
+        name = "jackson-databind",
+        artifact = "com.fasterxml.jackson.core:jackson-databind:" + JACKSON_REV,
+        sha1 = "4dfca3975be3c1a98eacb829e70f02e9a71bc159",
+    )
 
-        maven_jar(
-          name = "jackson-annotations",
-          artifact = "com.fasterxml.jackson.core:jackson-annotations:" + JACKSON_REV,
-          sha1 = "e0e758381a6579cb2029dace23a7209b90ac7232",
-        )
+    maven_jar(
+        name = "jackson-annotations",
+        artifact = "com.fasterxml.jackson.core:jackson-annotations:" + JACKSON_REV,
+        sha1 = "e0e758381a6579cb2029dace23a7209b90ac7232",
+    )
 
-        maven_jar(
-          name = "jackson-dataformat-yaml",
-          artifact = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" + JACKSON_REV,
-          sha1 = "607f3253c20267e385c85f60c859760a73a29e37",
-        )
+    maven_jar(
+        name = "jackson-dataformat-yaml",
+        artifact = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" + JACKSON_REV,
+        sha1 = "607f3253c20267e385c85f60c859760a73a29e37",
+    )
 
-        maven_jar(
-          name = "snakeyaml",
-          artifact = "org.yaml:snakeyaml:1.17",
-          sha1 = "7a27ea250c5130b2922b86dea63cbb1cc10a660c",
-        )
+    maven_jar(
+        name = "snakeyaml",
+        artifact = "org.yaml:snakeyaml:1.17",
+        sha1 = "7a27ea250c5130b2922b86dea63cbb1cc10a660c",
+    )
diff --git a/external_plugin_deps_standalone.bzl b/external_plugin_deps_standalone.bzl
new file mode 100644
index 0000000..b2964a5
--- /dev/null
+++ b/external_plugin_deps_standalone.bzl
@@ -0,0 +1,32 @@
+load("//tools/bzl:maven_jar.bzl", "GERRIT", "maven_jar")
+load("//:external_plugin_deps.bzl", "external_plugin_deps")
+
+PROLOG_VERS = "1.4.3"
+PROLOG_REPO = GERRIT
+
+def external_plugin_deps_standalone():
+    external_plugin_deps(omit_jackson_core = False)
+
+    maven_jar(
+        name = "prolog-runtime",
+        artifact = "com.googlecode.prolog-cafe:prolog-runtime:" + PROLOG_VERS,
+        attach_source = False,
+        repository = PROLOG_REPO,
+        sha1 = "d5206556cbc76ffeab21313ffc47b586a1efbcbb",
+    )
+
+    maven_jar(
+        name = "prolog-compiler",
+        artifact = "com.googlecode.prolog-cafe:prolog-compiler:" + PROLOG_VERS,
+        attach_source = False,
+        repository = PROLOG_REPO,
+        sha1 = "f37032cf1dec3e064427745bc59da5a12757a3b2",
+    )
+
+    maven_jar(
+        name = "prolog-io",
+        artifact = "com.googlecode.prolog-cafe:prolog-io:" + PROLOG_VERS,
+        attach_source = False,
+        repository = PROLOG_REPO,
+        sha1 = "d02b2640b26f64036b6ba2b45e4acc79281cea17",
+    )
diff --git a/lib/prolog/BUILD b/lib/prolog/BUILD
new file mode 100644
index 0000000..c5ed0b7
--- /dev/null
+++ b/lib/prolog/BUILD
@@ -0,0 +1 @@
+# Empty file required by Bazel
diff --git a/lib/prolog/prolog.bzl b/lib/prolog/prolog.bzl
new file mode 100644
index 0000000..ae46601
--- /dev/null
+++ b/lib/prolog/prolog.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//lib/prolog:prolog.bzl", "prolog_cafe_library")
diff --git a/owners-common/common.bzl b/owners-common/common.bzl
index 3c439bc..f766ff2 100644
--- a/owners-common/common.bzl
+++ b/owners-common/common.bzl
@@ -1,7 +1,7 @@
-EXTERNAL_DEPS = [ 
-    '@jackson-core//jar:neverlink',
-    '@jackson-databind//jar',
-    '@jackson-annotations//jar',
-    '@jackson-dataformat-yaml//jar',
-    '@snakeyaml//jar'
-  ]
+EXTERNAL_DEPS = [
+    "@jackson-core//jar:neverlink",
+    "@jackson-databind//jar",
+    "@jackson-annotations//jar",
+    "@jackson-dataformat-yaml//jar",
+    "@snakeyaml//jar",
+]
diff --git a/owners/BUILD b/owners/BUILD
index f428aaf..3a87774 100644
--- a/owners/BUILD
+++ b/owners/BUILD
@@ -36,5 +36,6 @@
     deps = [
         ":gerrit-owners-lib",
         ":gerrit-owners-prolog-rules",
+        "//owners-common",
     ] + EXTERNAL_DEPS,
 )
diff --git a/tools/BUILD b/tools/BUILD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/BUILD
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..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..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/junit.bzl b/tools/bzl/junit.bzl
new file mode 100644
index 0000000..3af7e58
--- /dev/null
+++ b/tools/bzl/junit.bzl
@@ -0,0 +1,4 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:junit.bzl",
+    "junit_tests",
+)
diff --git a/tools/bzl/maven_jar.bzl b/tools/bzl/maven_jar.bzl
new file mode 100644
index 0000000..43393b9
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", "GERRIT", "maven_jar")
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..d21bdf4
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,7 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "PLUGIN_DEPS",
+    "PLUGIN_DEPS_NEVERLINK",
+    "PLUGIN_TEST_DEPS",
+    "gerrit_plugin",
+)
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..a53253f
--- /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 = [
+        "//:owners_classpath_deps",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..100cc75
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,18 @@
+#!/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 owners -r .
+
+
diff --git a/tools/genrule2.bzl b/tools/genrule2.bzl
new file mode 100644
index 0000000..de66f32
--- /dev/null
+++ b/tools/genrule2.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//tools:genrule2.bzl", "genrule2")
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..83f9cdc
--- /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_OWNERS_LABEL $(rev .)