Add support for Bazel in Gerrit tree build

Also remove wrapper for JGit lfs dependencies and consume them directly
from the external package.

Change-Id: I29692a82a8975e75de792778b4acc838f55ad5d8
diff --git a/BUILD b/BUILD
index 48c5d1c..89b6dc4 100644
--- a/BUILD
+++ b/BUILD
@@ -1,18 +1,34 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load("//tools/bzl:junit.bzl", "junit_tests")
+load(
+    "//tools/bzl:plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
+)
 
 gerrit_plugin(
     name = "lfs",
     srcs = glob(["src/main/java/**/*.java"]),
+    resources = glob(["src/main/resources/**/*"]),
     manifest_entries = [
         "Gerrit-PluginName: lfs",
         "Gerrit-Module: com.googlesource.gerrit.plugins.lfs.Module",
         "Gerrit-HttpModule: com.googlesource.gerrit.plugins.lfs.HttpModule",
         "Gerrit-SshModule: com.googlesource.gerrit.plugins.lfs.SshModule",
     ],
-    resources = glob(["src/main/resources/**/*"]),
     deps = [
-        "//lib/jgit:jgit-http-apache",
-        "//lib/jgit:jgit-lfs",
-        "//lib/jgit:jgit-lfs-server",
+        "@jgit_http_apache//jar",
+        "@jgit_lfs//jar",
+        "@jgit_lfs_server//jar",
+    ],
+)
+
+junit_tests(
+    name = "lfs_tests",
+    srcs = glob(["src/test/java/**/*.java"]),
+    tags = ["lfs"],
+    deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        ":lfs__plugin",
+        "@jgit_lfs//jar",
     ],
 )
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
new file mode 100644
index 0000000..0a5726e
--- /dev/null
+++ b/external_plugin_deps.bzl
@@ -0,0 +1,43 @@
+load("//tools/bzl:maven_jar.bzl", "maven_jar", "GERRIT")
+
+JGIT_VERSION = '4.6.0.201612231935-r.30-gd3148f300'
+REPO = GERRIT
+
+def external_plugin_deps():
+  maven_jar(
+    name = 'jgit_http_apache',
+    artifact = 'org.eclipse.jgit:org.eclipse.jgit.http.apache:' + JGIT_VERSION,
+    sha1 = 'b9806f94d6b548c85a9ef96ef647b0f15b64927a',
+    repository = REPO,
+    unsign = True,
+    exclude = [
+      'about.html',
+      'plugin.properties',
+    ],
+  )
+
+  maven_jar(
+    name = 'jgit_lfs',
+    artifact = 'org.eclipse.jgit:org.eclipse.jgit.lfs:' + JGIT_VERSION,
+    sha1 = '55cf48dd41732ded00d66f2f833e3b7346eb5e37',
+    src_sha1 = 'fce0bceaece4c7885ffcd0d9405a524799b40db1',
+    repository = REPO,
+    unsign = True,
+    exclude = [
+      'about.html',
+      'plugin.properties',
+    ],
+  )
+
+  maven_jar(
+    name = 'jgit_lfs_server',
+    artifact = 'org.eclipse.jgit:org.eclipse.jgit.lfs.server:' + JGIT_VERSION,
+    sha1 = '4d917afafe7888bba07607bfa7fcb06bb60fe7f1',
+    src_sha1 = '1a31427d3a2940c661a16f51d1b2f96b37511fc6',
+    repository = REPO,
+    unsign = True,
+    exclude = [
+      'about.html',
+      'plugin.properties',
+    ],
+  )
diff --git a/lib/jgit/BUILD b/lib/jgit/BUILD
deleted file mode 100644
index 44c1e3b..0000000
--- a/lib/jgit/BUILD
+++ /dev/null
@@ -1,18 +0,0 @@
-package(
-    default_visibility = ["//visibility:public"],
-)
-
-java_library(
-    name = "jgit-http-apache",
-    exports = ["@jgit_http_apache//jar"],
-)
-
-java_library(
-    name = "jgit-lfs",
-    exports = ["@jgit_lfs//jar"],
-)
-
-java_library(
-    name = "jgit-lfs-server",
-    exports = ["@jgit_lfs_server//jar"],
-)
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index d652cd3..7f44b04 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -1,74 +1,66 @@
 Build
 =====
 
-This plugin can be built with Buck.
-
-Buck
-----
+This @PLUGIN@ plugin is built with Bazel.
 
 Two build modes are supported: Standalone and in Gerrit tree.
 The standalone build mode is recommended, as this mode doesn't require
 the Gerrit tree to exist locally.
 
-
 ### Build standalone
 
-Clone bucklets library:
-
 ```
-  git clone https://gerrit.googlesource.com/bucklets
-
-```
-and link it to %PLUGIN% plugin directory:
-
-```
-  cd %PLUGIN% && ln -s ../bucklets .
-```
-
-Add link to the .buckversion file:
-
-```
-  cd %PLUGIN% && ln -s bucklets/buckversion .buckversion
-```
-
-Add link to the .watchmanconfig file:
-
-```
-  cd %PLUGIN% && ln -s bucklets/watchmanconfig .watchmanconfig
-```
-
-To build the plugin, issue the following command:
-
-```
-  buck build plugin
+  bazel build @PLUGIN@
 ```
 
 The output is created in
 
 ```
-  buck-out/gen/%PLUGIN%.jar
+  bazel-genfiles/@PLUGIN@.jar
+```
+
+To execute the tests run:
+
+```
+  bazel test :@PLUGIN@_tests
 ```
 
 ### Build in Gerrit tree
 
-Clone or link this plugin to the plugins directory of Gerrit's source
-tree, and issue the command:
+Put the external dependency Bazel build file into the Gerrit /plugins directory,
+replacing the existing empty one.
 
 ```
-  buck build plugins/%PLUGIN%
+  cd gerrit/plugins
+  rm external_plugin_deps.bzl
+  ln -s @PLUGIN@/external_plugin_deps.bzl .
+```
+
+Then issue
+
+```
+  bazel build plugins/@PLUGIN@
 ```
 
 The output is created in
 
 ```
-  buck-out/gen/plugins/%PLUGIN%/%PLUGIN%.jar
+  bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar
 ```
 
-This project can be imported into the Eclipse IDE:
+This project can be imported into the Eclipse IDE.
+Add the plugin name to the `CUSTOM_PLUGINS` set in
+Gerrit core in `tools/bzl/plugins.bzl`, and execute:
 
 ```
   ./tools/eclipse/project.py
 ```
 
+To execute the tests run:
+
+```
+  bazel test plugins/@PLUGIN@:@PLUGIN@_tests
+```
+
 How to build the Gerrit Plugin API is described in the [Gerrit
-documentation](../../../Documentation/dev-buck.html#_extension_and_plugin_api_jar_files).
+documentation](../../../Documentation/dev-bazel.html#_extension_and_plugin_api_jar_files).
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/plugin.bzl b/tools/bzl/plugin.bzl
index 9cc3569..a2e438f 100644
--- a/tools/bzl/plugin.bzl
+++ b/tools/bzl/plugin.bzl
@@ -1,5 +1,6 @@
 load(
     "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
     "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
 )
-