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 f79093d..f07afcd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,9 @@
 *.iml
 *.ipr
 .idea
+/.classpath
+/.primary_build_tool
+/.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 7c0340a..32efd90 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/owner/owner.jar
+  bazel-genfiles/owner-autoassign/owner-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.
@@ -51,10 +89,11 @@
    $ git clone https://gerrit.googlesource.com/plugins/owners
    $ git clone https://gerrit.googlesource.com/gerrit
    $ cd gerrit/plugins
-   $ ln -s ../../owners/owners* .
+   $ ln -s ../../owners/owners .
+   $ ln -s ../../owners/owners-autoassign .
    $ ln -sf ../../owners/external_plugin_deps.bzl .
    $ cd ..
-   $ bazel test plugins/owners-common:test
+   $ ln -s ../owners/owners-common .
    $ bazel build plugins/owners plugins/owners-autoassign
 ```
 
@@ -62,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/owner/owner.jar
+  bazel-genfiles/plugins/owner-autoassign/owner-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..3e68355
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,20 @@
+workspace(name = "owners")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "930c64494c0400a3f08d3cf053c99f7d1dcb3f1c",
+    #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..b5c240c
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,17 @@
+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_standalone.bzl b/external_plugin_deps_standalone.bzl
new file mode 100644
index 0000000..5017f77
--- /dev/null
+++ b/external_plugin_deps_standalone.bzl
@@ -0,0 +1,33 @@
+load("//tools/bzl:maven_jar.bzl", "maven_jar", "GERRIT")
+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..e69de29
--- /dev/null
+++ b/lib/prolog/BUILD
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-autoassign/BUILD b/owners-autoassign/BUILD
index b2760f3..a835314 100644
--- a/owners-autoassign/BUILD
+++ b/owners-autoassign/BUILD
@@ -14,6 +14,6 @@
     ],
     resources = glob(["src/main/**/*"]),
     deps = [
-        "//plugins/owners-common",
+        "//owners-common",
     ],
 )
diff --git a/owners-common/BUILD b/owners-common/BUILD
index 8a4ea42..74064b0 100644
--- a/owners-common/BUILD
+++ b/owners-common/BUILD
@@ -1,6 +1,6 @@
 load("//tools/bzl:junit.bzl", "junit_tests")
 load("//tools/bzl:plugin.bzl", "gerrit_plugin", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS", "PLUGIN_DEPS_NEVERLINK")
-load("//plugins/owners-common:common.bzl", "EXTERNAL_DEPS")
+load("//owners-common:common.bzl", "EXTERNAL_DEPS")
 
 java_library(
     name = "owners-common",
diff --git a/owners/BUILD b/owners/BUILD
index 52343bb..a8a356d 100644
--- a/owners/BUILD
+++ b/owners/BUILD
@@ -10,7 +10,7 @@
     srcs = PROLOG_PREDICATES,
     deps = [
         "@prolog_runtime//jar:neverlink",
-        "//plugins/owners-common:owners-common",
+        "//owners-common:owners-common",
     ] + PLUGIN_DEPS_NEVERLINK,
 )
 
@@ -41,6 +41,6 @@
     deps = [
         ":gerrit-owners-predicates",
         ":gerrit-owners-prolog-rules",
-        "//plugins/owners-common",
+        "//owners-common",
     ],
 )
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..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/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..5da51cc
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", "maven_jar", "GERRIT")
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..3e8b08f
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,7 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_DEPS_NEVERLINK",
+    "PLUGIN_TEST_DEPS",
+)
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..35966f1
--- /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-AUTOASSIGN_LABEL $(rev .)