Move Maven support to //tools/maven

Rewrite the Maven tool to accept a spec of things to process on the
command line and use $(location) in the invoking genrule() to locate
the necessary files from Buck.  This gets rid of special cases in the
mvn wrapper tool and allows the definition to be given completely from
Buck as part of the build description.

Maven needs to be single threaded to perform repository updates safely
so only one genrule() target is declared to buck for the deploy or
install action. The rule is given all artifact information in a single
pass, allowing the mvn.py wrapper to execute them.

Change-Id: Idbcf645b69280420987a0e8f52947ba93ac9e6f0
diff --git a/.buckconfig b/.buckconfig
index bcefa2a..9af90e7 100644
--- a/.buckconfig
+++ b/.buckconfig
@@ -1,7 +1,7 @@
 [alias]
   api = //:api
-  api_deploy = //:api_deploy
-  api_install = //:api_install
+  api_deploy = //tools/maven:deploy
+  api_install = //tools/maven:install
   download = //:download
   download_sources = //:download_sources
   gerrit = //:gerrit
diff --git a/BUCK b/BUCK
index c878aba..ef1d50c 100644
--- a/BUCK
+++ b/BUCK
@@ -20,10 +20,12 @@
   out = '__fake.api__',
 )
 
-maven_install(name = 'api_install', deps = API_DEPS)
-maven_deploy(name = 'api_deploy', deps = API_DEPS)
+java_binary(
+  name = 'extension-api',
+  deps = [':extension-lib'],
+  visibility = ['//tools/maven:'],
+)
 
-java_binary(name = 'extension-api', deps = [':extension-lib'])
 java_library(
   name = 'extension-lib',
   deps = [
@@ -35,11 +37,13 @@
   export_deps = True,
   visibility = ['PUBLIC'],
 )
+
 genrule(
   name = 'extension-api-src',
   cmd = 'ln -s $(location //gerrit-extension-api:api-src) $OUT',
   deps = ['//gerrit-extension-api:api-src'],
   out = 'extension-api-src.jar',
+  visibility = ['//tools/maven:'],
 )
 
 PLUGIN_API = [
@@ -48,18 +52,25 @@
   '//gerrit-httpd:httpd',
 ]
 
-java_binary(name = 'plugin-api', deps = [':plugin-lib'])
+java_binary(
+  name = 'plugin-api',
+  deps = [':plugin-lib'],
+  visibility = ['//tools/maven:'],
+)
+
 java_library(
   name = 'plugin-lib',
   deps = PLUGIN_API,
   export_deps = True,
   visibility = ['PUBLIC'],
 )
+
 java_binary(
   name = 'plugin-api-src',
   deps = [
     '//gerrit-extension-api:api-src',
   ] + [d + '-src' for d in PLUGIN_API],
+  visibility = ['//tools/maven:'],
 )
 
 genrule(
diff --git a/tools/BUCK b/tools/BUCK
index d18b653..0e96f5d 100644
--- a/tools/BUCK
+++ b/tools/BUCK
@@ -17,13 +17,6 @@
   visibility = ['PUBLIC'],
 )
 
-python_binary(
-  name = 'maven_deploy',
-  main = 'maven_deploy.py',
-  deps = [':util'],
-  visibility = ['PUBLIC'],
-)
-
 python_library(
   name = 'util',
   srcs = ['util.py'],
diff --git a/tools/build.defs b/tools/build.defs
index 58bc862..b1198e3 100644
--- a/tools/build.defs
+++ b/tools/build.defs
@@ -11,7 +11,6 @@
 # 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('//VERSION')
 
 DOCS = ['//Documentation:html.zip']
 LIBS = [
diff --git a/tools/deploy_api.sh b/tools/deploy_api.sh
deleted file mode 100755
index 26baa31..0000000
--- a/tools/deploy_api.sh
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/bin/sh
-
-set -e
-
-SRC=$(ls gerrit-plugin-api/target/gerrit-plugin-api-*-sources.jar)
-VER=${SRC#gerrit-plugin-api/target/gerrit-plugin-api-}
-VER=${VER%-sources.jar}
-
-type=release
-case $VER in
-*-SNAPSHOT)
-  echo >&2 "fatal: Cannot deploy $VER"
-  echo >&2 "       Use ./tools/version.sh --release && mvn clean package"
-  exit 1
-  ;;
-*-[0-9]*-g*) type=snapshot ;;
-esac
-URL=gs://gerrit-api/$type
-
-
-echo "Deploying $type gerrit-extension-api $VER"
-mvn deploy:deploy-file \
-  -DgroupId=com.google.gerrit \
-  -DartifactId=gerrit-extension-api \
-  -Dversion=$VER \
-  -Dpackaging=jar \
-  -Dfile=gerrit-extension-api/target/gerrit-extension-api-$VER-all.jar \
-  -DrepositoryId=gerrit-api-repository \
-  -Durl=$URL
-
-mvn deploy:deploy-file \
-  -DgroupId=com.google.gerrit \
-  -DartifactId=gerrit-extension-api \
-  -Dversion=$VER \
-  -Dpackaging=java-source \
-  -Dfile=gerrit-extension-api/target/gerrit-extension-api-$VER-all-sources.jar \
-  -Djava-source=false \
-  -DrepositoryId=gerrit-api-repository \
-  -Durl=$URL
-
-
-echo "Deploying $type gerrit-plugin-api $VER"
-mvn deploy:deploy-file \
-  -DgroupId=com.google.gerrit \
-  -DartifactId=gerrit-plugin-api \
-  -Dversion=$VER \
-  -Dpackaging=jar \
-  -Dfile=gerrit-plugin-api/target/gerrit-plugin-api-$VER.jar \
-  -DrepositoryId=gerrit-api-repository \
-  -Durl=$URL
-
-mvn deploy:deploy-file \
-  -DgroupId=com.google.gerrit \
-  -DartifactId=gerrit-plugin-api \
-  -Dversion=$VER \
-  -Dpackaging=java-source \
-  -Dfile=gerrit-plugin-api/target/gerrit-plugin-api-$VER-sources.jar \
-  -Djava-source=false \
-  -DrepositoryId=gerrit-api-repository \
-  -Durl=$URL
diff --git a/tools/maven/BUCK b/tools/maven/BUCK
new file mode 100644
index 0000000..0a470a4
--- /dev/null
+++ b/tools/maven/BUCK
@@ -0,0 +1,24 @@
+include_defs('//VERSION')
+include_defs('//tools/maven/package.defs')
+
+TYPE = 'snapshot' if GERRIT_VERSION.endswith('-SNAPSHOT') else 'release'
+
+maven_package(
+  repository = 'gerrit-api-repository',
+  url = 's3://gerrit-api@commondatastorage.googleapis.com/%s' % TYPE,
+  version = GERRIT_VERSION,
+  jar = {
+    'gerrit-extension-api': '//:extension-api',
+    'gerrit-plugin-api': '//:plugin-api',
+  },
+  src = {
+    'gerrit-extension-api': '//:extension-api-src',
+    'gerrit-plugin-api': '//:plugin-api-src',
+  },
+)
+
+python_binary(
+  name = 'mvn',
+  main = 'mvn.py',
+  deps = ['//tools:util'],
+)
diff --git a/tools/maven/mvn.py b/tools/maven/mvn.py
new file mode 100644
index 0000000..cea610c
--- /dev/null
+++ b/tools/maven/mvn.py
@@ -0,0 +1,61 @@
+#!/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 optparse import OptionParser
+from sys import stderr
+from util import check_output
+
+opts = OptionParser()
+opts.add_option('--repository', help='maven repository id')
+opts.add_option('--url', help='maven repository url')
+opts.add_option('-a', help='action (valid actions are: install,deploy)')
+opts.add_option('-v', help='gerrit version')
+opts.add_option('-s', action='append', help='triplet of artifactId:type:path')
+
+args, ctx = opts.parse_args()
+if not args.v:
+  print('version is empty', file=stderr)
+  exit(1)
+
+common = [
+  '-DgroupId=com.google.gerrit',
+  '-Dversion=%s' % args.v,
+]
+
+if 'install' == args.a:
+  cmd = ['mvn', 'install:install-file'] + common
+elif 'deploy' == args.a:
+  cmd = [
+    'mvn',
+    'deploy:deploy-file',
+    '-DrepositoryId=%s' % args.repository,
+    '-Durl=%s' % args.url,
+  ] + common
+else:
+  print("unknown action -a %s" % args.a, file=stderr)
+  exit(1)
+
+for spec in args.s:
+  artifact, type, src = spec.split(':')
+  try:
+    check_output(cmd + [
+      '-DartifactId=%s' % artifact,
+      '-Dpackaging=%s' % type,
+      '-Dfile=%s' % src,
+    ])
+  except Exception as e:
+    print('%s command failed: %s' % (action, e), file=stderr)
+    exit(1)
diff --git a/tools/maven/package.defs b/tools/maven/package.defs
new file mode 100644
index 0000000..b19701f
--- /dev/null
+++ b/tools/maven/package.defs
@@ -0,0 +1,45 @@
+# 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.
+
+def maven_package(
+    version,
+    repository = None,
+    url = None,
+    jar = {},
+    src = {}):
+  cmd = ['$(exe //tools/maven:mvn)', '-v', version]
+  dep = []
+
+  for type,d in [('jar', jar), ('java-source', src)]:
+    for a,t in d.iteritems():
+      cmd.append('-s %s:%s:$(location %s)' % (a,type,t))
+      dep.append(t)
+
+  genrule(
+    name = 'install',
+    cmd = ' '.join(cmd + ['-a', 'install']),
+    deps = dep + ['//tools/maven:mvn'],
+    out = '__fake.install__',
+  )
+
+  if repository and url:
+    genrule(
+      name = 'deploy',
+      cmd = ' '.join(cmd + [
+        '-a', 'deploy',
+        '--repository', repository,
+        '--url', url]),
+      deps = dep + ['//tools/maven:mvn'],
+      out = '__fake.deploy__',
+    )
diff --git a/tools/maven_deploy.py b/tools/maven_deploy.py
deleted file mode 100644
index 4779abe..0000000
--- a/tools/maven_deploy.py
+++ /dev/null
@@ -1,92 +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 optparse import OptionParser
-from os.path import exists
-from sys import stderr
-from util import check_output
-
-opts = OptionParser()
-opts.add_option('-a', help='action (valid actions are: install,deploy)')
-opts.add_option('-v', help='gerrit version')
-opts.add_option('-d', help='dependencies (jars artifacts)')
-
-args, ctx = opts.parse_args()
-action = args.a
-if action not in ['deploy', 'install']:
-  print("unknown action : %s" % action, file=stderr)
-  exit(1)
-
-deps = args.d.split()
-if not deps:
-  print('dependencies are empty')
-  exit(1)
-
-extension_jar = [x for x in deps if "extension-api.jar" in x][0]
-extension_src = [x for x in deps if "extension-api-src.jar" in x][0]
-plugin_jar = [x for x in deps if "plugin-api.jar" in x][0]
-plugin_src = [x for x in deps if "plugin-api-src.jar" in x][0]
-
-version = args.v
-if not version:
-  print('version is empty')
-  exit(1)
-
-REPO_TYPE = 'snapshot' if version.endswith("SNAPSHOT") else 'release'
-URL = 's3://gerrit-api@commondatastorage.googleapis.com/%s' % REPO_TYPE
-
-plugin = ['-DartifactId=gerrit-plugin-api']
-extension = ['-DartifactId=gerrit-extension-api']
-common = [
-  '-DgroupId=com.google.gerrit',
-  '-Dversion=%s' % version,
-]
-jar = ['-Dpackaging=jar']
-src = ['-Dpackaging=java-source']
-
-cmd = {
-  'deploy': ['mvn',
-             'deploy:deploy-file',
-             '-DrepositoryId=gerrit-api-repository',
-             '-Durl=%s' % URL],
-  'install': ['mvn',
-              'install:install-file'],
-  }
-
-try:
-  check_output(cmd[action] +
-               plugin +
-               common +
-               jar +
-               ['-Dfile=%s' % plugin_jar])
-  check_output(cmd[action] +
-               plugin +
-               common +
-               src +
-               ['-Dfile=%s' % plugin_src])
-  check_output(cmd[action] +
-               extension +
-               common +
-               jar +
-               ['-Dfile=%s' % extension_jar])
-  check_output(cmd[action] +
-               extension +
-               common +
-               src +
-               ['-Dfile=%s' % extension_src])
-except Exception as e:
-  print('%s command failed: %s' % (action, e), file=stderr)
-  exit(1)