Consume global-refdb directly from source

global-refdb is a pure Bazel build and can now be directly built
and consumed from source instead of downloading it from Maven
central.

Bug: Issue 297440085
Change-Id: Ida423b145b8606c5eb2c106f7e0be1e715a9b981
diff --git a/BUILD b/BUILD
index f9cfcee..d95dc33 100644
--- a/BUILD
+++ b/BUILD
@@ -19,9 +19,9 @@
     resources = glob(["src/main/resources/**/*"]),
     deps = [
         ":events-broker-neverlink",
+        ":global-refdb-neverlink",
         ":pull-replication-neverlink",
         ":replication-neverlink",
-        "@global-refdb//jar:neverlink",
     ],
 )
 
@@ -43,6 +43,12 @@
     exports = ["//plugins/events-broker"],
 )
 
+java_library(
+    name = "global-refdb-neverlink",
+    neverlink = 1,
+    exports = ["//plugins/global-refdb"],
+)
+
 junit_tests(
     name = "multi_site_tests",
     srcs = glob(["src/test/java/**/*.java"]),
@@ -62,8 +68,8 @@
     visibility = ["//visibility:public"],
     exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
         ":multi-site__plugin",
-        "@global-refdb//jar",
         "//plugins/events-broker",
+        "//plugins/global-refdb",
         "//plugins/pull-replication",
         "//plugins/replication",
     ],
@@ -100,7 +106,6 @@
         "//plugins/multi-site",
         "//plugins/multi-site:e2e_multi_site_test_dir",
         "//plugins/multi-site:e2e_multi_site_setup_local_env_dir",
-        "external_plugin_deps.bzl",
     ] + glob(["setup_local_env/**/*"]) + glob(["e2e-tests/**/*"]),
     tags = [
         "e2e-multi-site",
diff --git a/Jenkinsfile b/Jenkinsfile
index 5b02057..6ef43af 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,4 +1,4 @@
 pluginPipeline(formatCheckId: 'gerritforge:multi-site-format-47168e90078b0b3f11401610930e82830e76bff7',
                buildCheckId: 'gerritforge:multi-site-47168e90078b0b3f11401610930e82830e76bff7',
                extraPlugins: [ 'pull-replication' ],
-               extraModules: [ 'events-broker' ])
+               extraModules: [ 'events-broker', 'global-refdb' ])
diff --git a/README.md b/README.md
index dcae980..f683f4e 100644
--- a/README.md
+++ b/README.md
@@ -40,8 +40,6 @@
 
 cd gerrit/plugins
 ln -s ../../multi-site .
-rm external_plugin_deps.bzl
-ln -s multi-site/external_plugin_deps.bzl .
 ```
 
 Example of building the multi-site plugin:
diff --git a/e2e-tests/test.sh b/e2e-tests/test.sh
index c3893a8..3ecb9fd 100755
--- a/e2e-tests/test.sh
+++ b/e2e-tests/test.sh
@@ -196,7 +196,6 @@
 done
 
 # Defaults
-GLOBAL_REFDB_VER=`grep 'com.gerritforge:global-refdb' ${LOCATION}/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 DEPLOYMENT_LOCATION=$(mktemp -d || $(echo >&2 "Could not create temp dir" && exit 1))
 MULTISITE_LIB_LOCATION=${MULTISITE_LIB_LOCATION:-${DEF_MULTISITE_LOCATION}}
 BROKER_TYPE=${BROKER_TYPE:-"kafka"}
@@ -260,9 +259,8 @@
 docker cp ${CONTAINER_NAME}:/var/gerrit/plugins/replication.jar $COMMON_LIBS/
 docker rm -fv ${CONTAINER_NAME}
 
-echo "Downloading global-refdb library $GERRIT_BRANCH"
-wget https://repo1.maven.org/maven2/com/gerritforge/global-refdb/$GLOBAL_REFDB_VER/global-refdb-$GLOBAL_REFDB_VER.jar \
-  -O $COMMON_LIBS/global-refdb.jar || { echo >&2 "Cannot download global-refdb library: Check internet connection. Aborting"; exit 1; }
+echo "Copying global-refdb library $GERRIT_BRANCH"
+cp bazel-bin/plugins/global-refdb/global-refdb.jar $COMMON_LIBS/global-refdb.jar
 
 echo "Downloading events-broker library $GERRIT_BRANCH"
 cp bazel-bin/plugins/events-broker/events-broker.jar $COMMON_LIBS/events-broker.jar
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
deleted file mode 100644
index 476b65c..0000000
--- a/external_plugin_deps.bzl
+++ /dev/null
@@ -1,8 +0,0 @@
-load("//tools/bzl:maven_jar.bzl", "maven_jar")
-
-def external_plugin_deps():
-    maven_jar(
-        name = "global-refdb",
-        artifact = "com.gerritforge:global-refdb:3.4.8.6",
-        sha1 = "5b8e943f94c64e3164e0d78f1c27795db7f72a4f",
-    )
diff --git a/setup_local_env/setup.sh b/setup_local_env/setup.sh
index 38c4d04..247a721 100755
--- a/setup_local_env/setup.sh
+++ b/setup_local_env/setup.sh
@@ -19,7 +19,6 @@
 GERRIT_BRANCH=stable-3.4
 GERRIT_CI=https://archive-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
 LAST_BUILD=lastSuccessfulBuild/artifact/bazel-bin/plugins
-GLOBAL_REFDB_VER=`grep 'com.gerritforge:global-refdb' $(dirname $0)/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 
 function check_application_requirements {
   type haproxy >/dev/null 2>&1 || { echo >&2 "Require haproxy but it's not installed. Aborting."; exit 1; }
@@ -234,6 +233,7 @@
     echo "[--release-war-file]            Location to release.war file"
     echo "[--multisite-lib-file]          Location to lib multi-site.jar file"
     echo "[--eventsbroker-lib-file]       Location to lib events-broker.jar file"
+    echo "[--globalrefdb-lib-file]        Location to lib global-refdb.jar file"
     echo
     echo "[--new-deployment]              Cleans up previous gerrit deployment and re-installs it. default true"
     echo "[--get-websession-plugin]       Download websession-broker plugin from CI lastSuccessfulBuild; default true"
@@ -292,6 +292,11 @@
     shift
     shift
   ;;
+  "--globalrefdb-lib-file" )
+    GLOBAL_REFDB_LIB_LOCATION=$2
+    shift
+    shift
+  ;;
   "--gerrit-canonical-host" )
     export GERRIT_CANONICAL_HOSTNAME=$2
     shift
@@ -394,7 +399,7 @@
 RELEASE_WAR_FILE_LOCATION=${RELEASE_WAR_FILE_LOCATION:-bazel-bin/release.war}
 MULTISITE_LIB_LOCATION=${MULTISITE_LIB_LOCATION:-bazel-bin/plugins/multi-site/multi-site.jar}
 EVENTS_BROKER_LIB_LOCATION=${EVENTS_BROKER_LIB_LOCATION:-bazel-bin/plugins/events-broker/events-broker.jar}
-
+GLOBAL_REFDB_LIB_LOCATION=${GLOBAL_REFDB_LIB_LOCATION:-bazel-bin/plugins/global-refdb/global-refdb.jar}
 
 export FAKE_NFS=$COMMON_LOCATION/fake_nfs
 
@@ -419,6 +424,9 @@
 echo "Copying events-broker library"
   cp -f $EVENTS_BROKER_LIB_LOCATION $DEPLOYMENT_LOCATION/events-broker.jar  >/dev/null 2>&1 || { echo >&2 "$EVENTS_BROKER_LIB_LOCATION: Not able to copy the file. Aborting"; exit 1; }
 
+echo "Copying global-refdb library"
+  cp -f $GLOBAL_REFDB_LIB_LOCATION $DEPLOYMENT_LOCATION/global-refdb.jar  >/dev/null 2>&1 || { echo >&2 "$GLOBAL_REFDB_LIB_LOCATION: Not able to copy the file. Aborting"; exit 1; }
+
 if [ $DOWNLOAD_WEBSESSION_PLUGIN = "true" ];then
   echo "Downloading websession-broker plugin $GERRIT_BRANCH"
   wget $GERRIT_CI/plugin-websession-broker-bazel-$GERRIT_BRANCH/$LAST_BUILD/websession-broker/websession-broker.jar \
@@ -442,11 +450,6 @@
   { echo >&2 "Cannot download zookeeper plugin: Check internet connection. Abort\
 ing"; exit 1; }
 
-echo "Downloading global-refdb library $GERRIT_BRANCH"
-  wget https://repo1.maven.org/maven2/com/gerritforge/global-refdb/$GLOBAL_REFDB_VER/global-refdb-$GLOBAL_REFDB_VER.jar \
-  -O $DEPLOYMENT_LOCATION/global-refdb.jar || { echo >&2 "Cannot download global-refdb library: Check internet connection. Abort\
-ing"; exit 1; }
-
 if [ "$BROKER_TYPE" = "kafka" ]; then
 echo "Downloading events-kafka plugin $GERRIT_BRANCH"
   wget $GERRIT_CI/plugin-events-kafka-bazel-$GERRIT_BRANCH/$LAST_BUILD/events-kafka/events-kafka.jar \
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index ec44918..146341e 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -3,14 +3,7 @@
 This plugin can be built with Bazel in the 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 .
-```
+source tree.
 
 Clone the [pull-replication](https://gerrit.googlesource.com/plugins/pull-replication) on
 the same branch of the @PLUGIN@ plugin and link it to the `gerrit/plugins` directory.
@@ -23,6 +16,15 @@
   ln -s @PLUGIN@/external_plugin_deps.bzl .
 ```
 
+Clone the [global-refdb](git clone "https://gerrit.googlesource.com/modules/global-refdb") on
+the same branch of the @PLUGIN@ plugin and link it to the `gerrit/plugins` directory.
+
+```
+  export BRANCH=$(git --git-dir=@PLUGIN@ branch)
+  git clone "https://gerrit.googlesource.com/modules/global-refdb"
+  cd gerrit/plugins
+  ln -s ../../global-refdb .
+```
 
 From the Gerrit source tree issue the command: