Convert project to bucklet build

Change-Id: Ic8e6ebe1c1df974d65d1360c9467bb384117f78e
diff --git a/.buckconfig b/.buckconfig
index 9f228b8..1e0af75 100644
--- a/.buckconfig
+++ b/.buckconfig
@@ -1,9 +1,6 @@
 [alias]
   plugin = //:server-config
 
-[buildfile]
-  includes = //tools/buck.defs
-
 [java]
   src_roots = java, resources
 
diff --git a/.gitignore b/.gitignore
index a45e726..4fd69d7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,6 @@
 /.project
 /.settings/org.maven.ide.eclipse.prefs
 /.settings/org.eclipse.m2e.core.prefs
+bucklets
+.buckversion
+.watchmanconfig
diff --git a/BUCK b/BUCK
index f6de1af..27d3a0c 100644
--- a/BUCK
+++ b/BUCK
@@ -1,7 +1,4 @@
-include_defs('//lib/maven.defs')
-
-API_VERSION = '2.9-SNAPSHOT'
-REPO = MAVEN_LOCAL
+include_defs('//bucklets/gerrit_plugin.bucklet')
 
 gerrit_plugin(
   name = 'server-config',
@@ -12,3 +9,10 @@
     'Gerrit-HttpModule: com.googlesource.gerrit.plugins.serverconfig.HttpModule',
   ]
 )
+
+# this is required for bucklets/tools/eclipse/project.py to work
+# not sure, if this does something useful in standalone context
+java_library(
+  name = 'classpath',
+  deps = [':server-config__plugin'],
+)
diff --git a/VERSION b/VERSION
index 44ed74b..3a7a6e8 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1,4 @@
-2.9-SNAPSHOT
+# Used by BUCK to include "Implementation-Version" in plugin Manifest.
+# If this file doesn't exist the output of 'git describe' is used
+# instead.
+PLUGIN_VERSION = '1.0-SNAPSHOT'
diff --git a/lib/build.defs b/lib/build.defs
deleted file mode 100644
index 08418d0..0000000
--- a/lib/build.defs
+++ /dev/null
@@ -1,76 +0,0 @@
-include_defs('//lib/maven.defs')
-
-def java_library2(
-    name,
-    srcs = [],
-    resources = [],
-    deps = [],
-    compile_deps = [],
-    visibility = []):
-  c = name + '__compile'
-  t = name + '__link'
-  j = 'lib__%s__output/%s.jar' % (c, c)
-  o = 'lib__%s__output/%s.jar' % (name, name)
-  java_library(
-    name = c,
-    srcs = srcs,
-    resources = resources,
-    deps = deps + compile_deps,
-    visibility = visibility,
-  )
-  genrule(
-    name = t,
-    cmd = 'mkdir -p $(dirname $OUT);ln -s $SRCS $OUT',
-    srcs = [genfile(j)],
-    deps = [':' + c],
-    out = o,
-  )
-  prebuilt_jar(
-    name = name,
-    binary_jar = genfile(o),
-    deps = deps + [':' + t],
-    visibility = visibility,
-  )
-
-def gerrit_plugin(
-    name,
-    deps = [],
-    srcs = [],
-    resources = [],
-    manifest_file = None,
-    manifest_entries = [],
-    type = 'plugin',
-    visibility = ['PUBLIC']):
-  mf_cmd = 'v=$(cat VERSION);'
-  if manifest_file:
-    mf_src = [manifest_file]
-    mf_cmd += 'sed "s:@VERSION@:$v:g" $SRCS >$OUT'
-  else:
-    mf_src = []
-    mf_cmd += 'echo "Manifest-Version: 1.0" >$OUT;'
-    mf_cmd += 'echo "Gerrit-ApiType: %s" >>$OUT;' % type
-    mf_cmd += 'echo "Implementation-Version: $v" >>$OUT'
-    for line in manifest_entries:
-      mf_cmd += ';echo "%s" >> $OUT' % line
-  genrule(
-    name = name + '__manifest',
-    cmd = mf_cmd,
-    srcs = mf_src,
-    out = 'MANIFEST.MF',
-  )
-  java_library2(
-    name = name + '__plugin',
-    srcs = srcs,
-    resources = resources,
-    deps = deps,
-    compile_deps = ['//:%s-lib' % type],
-  )
-  java_binary(
-    name = name,
-    manifest_file = genfile('MANIFEST.MF'),
-    deps = [
-      ':%s__plugin' % name,
-      ':%s__manifest' % name,
-    ],
-    visibility = visibility,
-  )
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
new file mode 100644
index 0000000..a197eb1
--- /dev/null
+++ b/lib/gerrit/BUCK
@@ -0,0 +1,12 @@
+include_defs('//bucklets/maven_jar.bucklet')
+
+VER = '2.11-SNAPSHOT'
+REPO = MAVEN_LOCAL
+
+maven_jar(
+  name = 'plugin-api',
+  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
+  attach_source = False,
+  repository = REPO,
+  license = 'Apache2.0',
+)
diff --git a/lib/maven.defs b/lib/maven.defs
deleted file mode 100644
index 689040a..0000000
--- a/lib/maven.defs
+++ /dev/null
@@ -1,63 +0,0 @@
-GERRIT = 'GERRIT:'
-GERRIT_API = 'GERRIT_API:'
-MAVEN_LOCAL = 'MAVEN_LOCAL:'
-
-def maven_jar(
-    name,
-    id,
-    license,
-    deps = [],
-    sha1 = '',
-    bin_sha1 = '',
-    src_sha1 = '',
-    repository = GERRIT,
-    attach_source = True,
-    visibility = ['PUBLIC']):
-  from os import path
-
-  parts = id.split(':')
-  if len(parts) != 3:
-    raise NameError('expected id="groupId:artifactId:version"')
-  group, artifact, version = parts
-
-  file_version = version
-
-  jar = path.join(name, artifact.lower() + '-' + file_version)
-  url = '/'.join([
-    repository,
-    group.replace('.', '/'), artifact, version,
-    artifact + '-' + file_version])
-
-  binjar = jar + '.jar'
-  binurl = url + '.jar'
-
-  srcjar = jar + '-src.jar'
-  srcurl = url + '-sources.jar'
-
-  cmd = ['$(exe //tools:download_file)', '-o', '$OUT', '-u', binurl]
-  if sha1:
-    cmd.extend(['-v', sha1])
-  elif bin_sha1:
-    cmd.extend(['-v', bin_sha1])
-
-  genrule(
-    name = name + '__download_bin',
-    cmd = ' '.join(cmd),
-    deps = ['//tools:download_file'],
-    out = binjar,
-  )
-
-  srcjar = None
-  genrule(
-    name = name + '__download_src',
-    cmd = ':>$OUT',
-    out = '__' + name + '__no_src',
-  )
-
-  prebuilt_jar(
-    name = name,
-    deps = deps + [':' + name + '__download_bin'],
-    binary_jar = genfile(binjar),
-    source_jar = genfile(srcjar) if srcjar else None,
-    visibility = visibility,
-  )
diff --git a/pom.xml b/pom.xml
index b504c52..6251705 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,7 @@
   <groupId>com.googlesource.gerrit.plugins.server-config</groupId>
   <artifactId>server-config</artifactId>
   <packaging>jar</packaging>
-  <version>2.9-SNAPSHOT</version>
+  <version>2.11-SNAPSHOT</version>
   <name>server-config</name>
 
   <properties>
@@ -94,11 +94,4 @@
       <version>7.0.32</version>
     </dependency>
   </dependencies>
-
-  <repositories>
-    <repository>
-      <id>gerrit-api-repository</id>
-      <url>https://gerrit-api.commondatastorage.googleapis.com/snapshot/</url>
-    </repository>
-  </repositories>
 </project>
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..6d1e8da
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,86 @@
+Build
+=====
+
+This plugin can be built with Buck or Maven.
+
+Buck
+----
+
+Two build modes are supported: Standalone and in Gerrit tree.
+The in-tree build mode is recommended if this plugin shall be
+build together with the Gerrit tree.
+The standalone build mode can be use to build this plugin
+independently of the Gerrit tree.
+
+### Build standalone
+
+Clone bucklets library:
+
+```
+  git clone https://gerrit.googlesource.com/bucklets
+
+```
+and link it to server-config plugin directory:
+
+```
+  cd server-config && ln -s ../bucklets .
+```
+
+Add link to the .buckversion file:
+
+```
+  cd server-config && ln -s bucklets/buckversion .buckversion
+```
+
+Add link to the .watchmanconfig file:
+```
+  cd server-config && ln -s bucklets/watchmanconfig .watchmanconfig
+```
+
+To build the plugin, issue the following command:
+
+
+```
+  buck build plugin
+```
+
+The output is created in
+
+```
+  buck-out/gen/server-config.jar
+```
+
+
+### Build in Gerrit tree
+
+Clone or link this plugin to the plugins directory of Gerrit's source
+tree, and issue the command:
+
+```
+  buck build plugins/server-config
+```
+
+The output is created in
+
+```
+  buck-out/gen/plugins/server-config/server-config.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./tools/eclipse/project.py
+```
+
+Maven
+-----
+
+Note that the Maven build is provided for compatibility reasons, but
+it is considered to be deprecated and will be removed in a future
+version of this plugin.
+
+To build with Maven, run
+
+```
+mvn clean package
+```
diff --git a/tools/BUCK b/tools/BUCK
deleted file mode 100644
index 1b1175c..0000000
--- a/tools/BUCK
+++ /dev/null
@@ -1,5 +0,0 @@
-python_binary(
-  name = 'download_file',
-  main = 'download_file.py',
-  visibility = ['PUBLIC'],
-)
diff --git a/tools/buck.defs b/tools/buck.defs
deleted file mode 100644
index 41eeeca..0000000
--- a/tools/buck.defs
+++ /dev/null
@@ -1,67 +0,0 @@
-# Copyright (C) 2014 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.
-
-include_defs('//lib/build.defs')
-
-original_java_library = java_library
-def java_library(
-    name,
-    srcs=[],
-    resources=[],
-    source='7',
-    target='7',
-    proguard_config=None,
-    deps=[],
-    exported_deps=[],
-    visibility=[],
-    ):
-  original_java_library(
-    name=name,
-    srcs=srcs,
-    resources=resources,
-    source=source,
-    target=target,
-    proguard_config=proguard_config,
-    deps=deps,
-    exported_deps=exported_deps,
-    visibility=visibility,
-  )
-
-original_java_test = java_test
-def java_test(
-    name,
-    srcs=[],
-    labels=[],
-    resources=[],
-    source='7',
-    target='7',
-    vm_args=[],
-    source_under_test=[],
-    contacts=[],
-    deps=[],
-    visibility=[],
-    ):
-  original_java_test(
-    name=name,
-    srcs=srcs,
-    labels=labels,
-    resources=resources,
-    source=source,
-    target=target,
-    vm_args=vm_args,
-    source_under_test=source_under_test,
-    contacts=contacts,
-    deps=deps,
-    visibility=visibility,
-  )
diff --git a/tools/download_file.py b/tools/download_file.py
deleted file mode 100755
index d71c3ac..0000000
--- a/tools/download_file.py
+++ /dev/null
@@ -1,209 +0,0 @@
-#!/usr/bin/python
-# Copyright (C) 2013 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.
-
-from __future__ import print_function
-
-from hashlib import sha1
-from optparse import OptionParser
-from os import link, makedirs, path, remove
-import shutil
-from subprocess import check_call, CalledProcessError
-from sys import stderr
-from zipfile import ZipFile, BadZipfile, LargeZipFile
-
-REPO_ROOTS = {
-  'GERRIT': 'http://gerrit-maven.storage.googleapis.com',
-  'GERRIT_API': 'https://gerrit-api.commondatastorage.googleapis.com/release',
-  'ECLIPSE': 'https://repo.eclipse.org/content/groups/releases',
-  'MAVEN_CENTRAL': 'http://repo1.maven.org/maven2',
-  'MAVEN_LOCAL': 'file://' + path.expanduser('~/.m2/repository'),
-}
-
-GERRIT_HOME = path.expanduser('~/.gerritcodereview')
-CACHE_DIR = path.join(GERRIT_HOME, 'buck-cache')
-LOCAL_PROPERTIES = 'local.properties'
-
-
-def hashfile(p):
-  d = sha1()
-  with open(p, 'rb') as f:
-    while True:
-      b = f.read(8192)
-      if not b:
-        break
-      d.update(b)
-  return d.hexdigest()
-
-def safe_mkdirs(d):
-  if path.isdir(d):
-    return
-  try:
-    makedirs(d)
-  except OSError as err:
-    if not path.isdir(d):
-      raise err
-
-def download_properties(root_dir):
-  """ Get the download properties.
-
-  First tries to find the properties file in the given root directory,
-  and if not found there, tries in the Gerrit settings folder in the
-  user's home directory.
-
-  Returns a set of download properties, which may be empty.
-
-  """
-  p = {}
-  local_prop = path.join(root_dir, LOCAL_PROPERTIES)
-  if not path.isfile(local_prop):
-    local_prop = path.join(GERRIT_HOME, LOCAL_PROPERTIES)
-  if path.isfile(local_prop):
-    try:
-      with open(local_prop) as fd:
-        for line in fd:
-          if line.startswith('download.'):
-            d = [e.strip() for e in line.split('=', 1)]
-            name, url = d[0], d[1]
-            p[name[len('download.'):]] = url
-    except OSError:
-      pass
-  return p
-
-def cache_entry(args):
-  if args.v:
-    h = args.v
-  else:
-    h = sha1(args.u).hexdigest()
-  name = '%s-%s' % (path.basename(args.o), h)
-  return path.join(CACHE_DIR, name)
-
-def resolve_url(url, redirects):
-  s = url.find(':')
-  if s < 0:
-    return url
-  scheme, rest = url[:s], url[s+1:]
-  if scheme not in REPO_ROOTS:
-    return url
-  if scheme in redirects:
-    root = redirects[scheme]
-  else:
-    root = REPO_ROOTS[scheme]
-  root = root.rstrip('/')
-  rest = rest.lstrip('/')
-  return '/'.join([root, rest])
-
-opts = OptionParser()
-opts.add_option('-o', help='local output file')
-opts.add_option('-u', help='URL to download')
-opts.add_option('-v', help='expected content SHA-1')
-opts.add_option('-x', action='append', help='file to delete from ZIP')
-opts.add_option('--exclude_java_sources', action='store_true')
-opts.add_option('--unsign', action='store_true')
-args, _ = opts.parse_args()
-
-root_dir = args.o
-while root_dir:
-  root_dir, n = path.split(root_dir)
-  if n == 'buck-out':
-    break
-
-redirects = download_properties(root_dir)
-cache_ent = cache_entry(args)
-src_url = resolve_url(args.u, redirects)
-
-if not path.exists(cache_ent):
-  try:
-    safe_mkdirs(path.dirname(cache_ent))
-  except OSError as err:
-    print('error creating directory %s: %s' %
-          (path.dirname(cache_ent), err), file=stderr)
-    exit(1)
-
-  print('Download %s' % src_url, file=stderr)
-  try:
-    check_call(['curl', '--proxy-anyauth', '-sfo', cache_ent, src_url])
-  except OSError as err:
-    print('could not invoke curl: %s\nis curl installed?' % err, file=stderr)
-    exit(1)
-  except CalledProcessError as err:
-    print('error using curl: %s' % err, file=stderr)
-    exit(1)
-
-if args.v:
-  have = hashfile(cache_ent)
-  if args.v != have:
-    print((
-      '%s:\n' +
-      'expected %s\n' +
-      'received %s\n') % (src_url, args.v, have), file=stderr)
-    try:
-      remove(cache_ent)
-    except OSError as err:
-      if path.exists(cache_ent):
-        print('error removing %s: %s' % (cache_ent, err), file=stderr)
-    exit(1)
-
-exclude = []
-if args.x:
-  exclude += args.x
-if args.exclude_java_sources:
-  try:
-    zf = ZipFile(cache_ent, 'r')
-    try:
-      for n in zf.namelist():
-        if n.endswith('.java'):
-          exclude.append(n)
-    finally:
-      zf.close()
-  except (BadZipfile, LargeZipFile) as err:
-    print('error opening %s: %s'  % (cache_ent, err), file=stderr)
-    exit(1)
-
-if args.unsign:
-  try:
-    zf = ZipFile(cache_ent, 'r')
-    try:
-      for n in zf.namelist():
-        if (n.endswith('.RSA')
-            or n.endswith('.SF')
-            or n.endswith('.LIST')):
-          exclude.append(n)
-    finally:
-      zf.close()
-  except (BadZipfile, LargeZipFile) as err:
-    print('error opening %s: %s'  % (cache_ent, err), file=stderr)
-    exit(1)
-
-safe_mkdirs(path.dirname(args.o))
-if exclude:
-  try:
-    shutil.copyfile(cache_ent, args.o)
-  except (shutil.Error, IOError) as err:
-    print('error copying to %s: %s' % (args.o, err), file=stderr)
-    exit(1)
-  try:
-    check_call(['zip', '-d', args.o] + exclude)
-  except CalledProcessError as err:
-    print('error removing files from zip: %s' % err, file=stderr)
-    exit(1)
-else:
-  try:
-    link(cache_ent, args.o)
-  except OSError as err:
-    try:
-      shutil.copyfile(cache_ent, args.o)
-    except (shutil.Error, IOError) as err:
-      print('error copying to %s: %s' % (args.o, err), file=stderr)
-      exit(1)