Implement Bazel in tree build

Remove sbt build. This change is targeting stable-2.14 branch and
assumes that gerrit core still has velocity and joda-time libraries
available.

Also remove development instruction from README.md file, as with Bazel
build tool chain support, the instructions are common to all plugins
and mentioned in dev-plugins and dev-bazel in gerrit core.

Change-Id: Ib2829cd4e9086aa82f5884c09f3cf896693ec0a1
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..5eff3a6
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,35 @@
+load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+
+gerrit_plugin(
+    name = "saml",
+    srcs = glob(["src/main/java/**/*.java"]),
+    manifest_entries = [
+        "Gerrit-PluginName: gerrit-saml-plugin",
+    ],
+    resources = glob(["src/main/resources/**"]),
+    deps = [
+        "@cryptacular//jar",
+        "@opensaml-core//jar",
+        "@opensaml-messaging-api//jar",
+        "@opensaml-messaging-impl//jar",
+        "@opensaml-profile-api//jar",
+        "@opensaml-profile-impl//jar",
+        "@opensaml-saml-api//jar",
+        "@opensaml-saml-impl//jar",
+        "@opensaml-security-api//jar",
+        "@opensaml-security-impl//jar",
+        "@opensaml-soap-api//jar",
+        "@opensaml-soap-impl//jar",
+        "@opensaml-storage-api//jar",
+        "@opensaml-xmlsec-api//jar",
+        "@opensaml-xmlsec-impl//jar",
+        "@pac4j-core//jar",
+        "@pac4j-saml//jar",
+        "@santuario-xmlsec//jar",
+        "@shibboleth-utilities//jar",
+        "@shibboleth-xmlsectool//jar",
+        "@spring-core//jar",
+        "@stax2-api//jar",
+        "@woodstox-core//jar",
+    ],
+)
diff --git a/README.md b/README.md
index e7ea1b4..5c131be 100644
--- a/README.md
+++ b/README.md
@@ -132,11 +132,3 @@
 
 Default is `UserName`
 
-## Development
-
-- Clone this repository.
-- Install sbt.
-- Edit the code.
-- Run 'sbt assembly' to build the jar.
-- Copy target/out/gerrit-saml-plugin-$VERSION.jar into $site_path/lib/
-
diff --git a/build.sbt b/build.sbt
deleted file mode 100644
index bb25ffc..0000000
--- a/build.sbt
+++ /dev/null
@@ -1,56 +0,0 @@
-name := "gerrit-saml-plugin"
-
-val GerritVersion = "2.13.12"
-
-version := GerritVersion + "-6"
-
-scalaVersion := "2.12.8"
-
-javacOptions ++= Seq("-source", "1.7", "-target", "1.7")
-
-libraryDependencies += ("com.google.gerrit" % "gerrit-plugin-api" % GerritVersion % "provided")
-
-libraryDependencies += "org.pac4j" % "pac4j-saml" % "3.4.0"
-
-resolvers += "Shibboleth" at "https://build.shibboleth.net/nexus/content/repositories/releases/"
-
-libraryDependencies ~= { _ map {
-  case m => m
-    .exclude("ch.qos.logback", "logback-classic")
-    .exclude("ch.qos.logback", "logback-core")
-    .exclude("com.google.guava", "guava")
-    .exclude("commons-codec", "commons-codec")
-    .exclude("commons-collections", "commons-collections")
-    .exclude("commons-httpclient", "commons-httpclient")
-    .exclude("commons-lang", "commons-lang")
-    .exclude("commons-logging", "commons-logging")
-    .exclude("commons-ssl", "commons-ssl")
-    .exclude("javax.servlet", "servlet-api")
-    .exclude("javax.xml", "*")
-    .exclude("junit", "*")
-    .exclude("org.apache.httpcomponents", "*")
-    .exclude("org.apache.velocity", "*")
-    .exclude("org.bouncycastle", "*")
-    .exclude("org.slf4j", "*")
-    .exclude("xalan", "xalan")
-}}
-
-assemblyMergeStrategy in assembly := {
-  case PathList("META-INF", "INDEX.LIST") => MergeStrategy.discard
-  case PathList("META-INF", "MANIFEST.MF") => MergeStrategy.discard
-  case PathList("META-INF", "NOTICE.txt") => MergeStrategy.discard
-  case PathList("META-INF", "NOTICE") => MergeStrategy.discard
-  case PathList("META-INF", "LICENSE.txt") => MergeStrategy.concat
-  case PathList("META-INF", "LICENSE") => MergeStrategy.concat
-  // Trick is here: get all the initializers concatenated...
-  case PathList("META-INF", "services", "org.opensaml.core.config.Initializer") => MergeStrategy.concat
-  case PathList("schema", v) if v.endsWith(".xsd") => MergeStrategy.first
-  case PathList("credential-criteria-registry.properties") => MergeStrategy.first
-  case PathList(xml) if xml.endsWith(".xml") => MergeStrategy.first
-  case _ => MergeStrategy.deduplicate
-}
-
-assemblyOption in assembly := (assemblyOption in assembly).value.copy(includeScala = false)
-
-assemblyOutputPath in assembly := target.value / "out" / (s"${name.value}-${version.value}.jar")
-
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
new file mode 100644
index 0000000..66cd7e5
--- /dev/null
+++ b/external_plugin_deps.bzl
@@ -0,0 +1,162 @@
+load("//tools/bzl:maven_jar.bzl", "maven_jar")
+
+SHIBBOLETH = "https://build.shibboleth.net/nexus/content/repositories/releases/"
+
+OPENSAML_VERSION = "3.4.0"
+
+PAC4J_VERSION = "3.4.0"
+
+def external_plugin_deps():
+    maven_jar(
+        name = "cryptacular",
+        artifact = "org.cryptacular:cryptacular:1.2.1",
+        sha1 = "c470bac7309ac04b0b9529bd7dcb1e0b75954f11",
+    )
+
+    maven_jar(
+        name = "opensaml-core",
+        artifact = "org.opensaml:opensaml-core:" + OPENSAML_VERSION,
+        sha1 = "a1a4607c800bebd921615ef6fdb7bcd1340452c3",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-saml-api",
+        artifact = "org.opensaml:opensaml-saml-api:" + OPENSAML_VERSION,
+        sha1 = "538e1e54e5e8160f2d284b08f1b8a7b93053e0da",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-storage-api",
+        artifact = "org.opensaml:opensaml-storage-api:" + OPENSAML_VERSION,
+        sha1 = "37beed2a755b031a57322a1c910eca8278371f4f",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-saml-impl",
+        artifact = "org.opensaml:opensaml-saml-impl:" + OPENSAML_VERSION,
+        sha1 = "06336645ec0b0fbd98a7a5e719b4c4c284a4d79f",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-soap-impl",
+        artifact = "org.opensaml:opensaml-soap-impl:" + OPENSAML_VERSION,
+        sha1 = "23f0b2732c87a34c0179584e71a52839eaf9c186",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-soap-api",
+        artifact = "org.opensaml:opensaml-soap-api:" + OPENSAML_VERSION,
+        sha1 = "830b14c47a7e3e21ed377be4c82e6f19ff5c6749",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-xmlsec-api",
+        artifact = "org.opensaml:opensaml-xmlsec-api:" + OPENSAML_VERSION,
+        sha1 = "6af095f2882bd73067860699c9b2d11f5e5d9fa2",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-xmlsec-impl",
+        artifact = "org.opensaml:opensaml-xmlsec-impl:" + OPENSAML_VERSION,
+        sha1 = "94ea339d9e63436cdf4a2247b5ef85867e66d302",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-security-api",
+        artifact = "org.opensaml:opensaml-security-api:" + OPENSAML_VERSION,
+        sha1 = "a39782c2c23abf09f9e0e6c51a33f7e658654342",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-security-impl",
+        artifact = "org.opensaml:opensaml-security-impl:" + OPENSAML_VERSION,
+        sha1 = "ee6158d53b576d6a63d3c7a0cf063c8518e75126",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-profile-api",
+        artifact = "org.opensaml:opensaml-profile-api:" + OPENSAML_VERSION,
+        sha1 = "e881c110d608f9e515681315d1509cd4d0737fa7",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-profile-impl",
+        artifact = "org.opensaml:opensaml-profile-impl:" + OPENSAML_VERSION,
+        sha1 = "312eb6bf5dd7fb34c4305299e83b24ba10814f86",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-messaging-api",
+        artifact = "org.opensaml:opensaml-messaging-api:" + OPENSAML_VERSION,
+        sha1 = "e0377b92343e8aa8a293361537d2d89d88e2b5da",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "opensaml-messaging-impl",
+        artifact = "org.opensaml:opensaml-messaging-impl:" + OPENSAML_VERSION,
+        sha1 = "4ef49243790872163c13aee1607ccb2681b39c53",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "pac4j-saml",
+        artifact = "org.pac4j:pac4j-saml:" + PAC4J_VERSION,
+        sha1 = "5d031bee53298e483ade21dd186f5f1c89c770e3",
+    )
+
+    maven_jar(
+        name = "pac4j-core",
+        artifact = "org.pac4j:pac4j-core:" + PAC4J_VERSION,
+        sha1 = "22066c85a65803ec249908e38fb3c9f7c42a568a",
+    )
+
+    maven_jar(
+        name = "shibboleth-utilities",
+        artifact = "net.shibboleth.utilities:java-support:7.4.0",
+        sha1 = "e10c137cdb5045eea2c0ccf8ac5094052eaee36b",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "shibboleth-xmlsectool",
+        artifact = "net.shibboleth.tool:xmlsectool:2.0.0",
+        sha1 = "c57f887f522c0e930341c7d86eff4d8ec9b797a1",
+        repository = SHIBBOLETH,
+    )
+
+    maven_jar(
+        name = "santuario-xmlsec",
+        artifact = "org.apache.santuario:xmlsec:2.0.10",
+        sha1 = "57865d2fbaf65f27c6cb8e909e37842e5cb87960",
+    )
+
+    maven_jar(
+        name = "spring-core",
+        artifact = "org.springframework:spring-core:5.0.2.RELEASE",
+        sha1 = "45b2958ab3fb022dd29f8b1c553ebf1c75a144aa",
+    )
+
+    maven_jar(
+        name = "stax2-api",
+        artifact = "org.codehaus.woodstox:stax2-api:3.1.4",
+        sha1 = "ac19014b1e6a7c08aad07fe114af792676b685b7",
+    )
+
+    maven_jar(
+        name = "woodstox-core",
+        artifact = "com.fasterxml.woodstox:woodstox-core:5.0.3",
+        sha1 = "10aa199207fda142eff01cd61c69244877d71770",
+    )
diff --git a/project/assembly.sbt b/project/assembly.sbt
deleted file mode 100644
index d95475f..0000000
--- a/project/assembly.sbt
+++ /dev/null
@@ -1 +0,0 @@
-addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.7")
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..ea50282
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,45 @@
+Build
+=====
+
+This plugin is built with Bazel.
+
+## 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
+  rm external_plugin_deps.bzl
+  ln -s @PLUGIN@/external_plugin_deps.bzl .
+```
+
+Then issue
+
+```
+  bazel build plugins/@PLUGIN@
+```
+
+The output is created in
+
+```
+  bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar
+```
+
+The @PLUGIN@.jar should be deployed to `gerrit_site/lib` directory:
+
+```
+ cp bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar `$gerrit_site/lib`
+```
+
+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
+```
+
+How to build the Gerrit Plugin API is described in the [Gerrit
+documentation](../../../Documentation/dev-bazel.html#_extension_and_plugin_api_jar_files).