Fixes to buck build.

I have restructured the build files to ensure that both clean builds and incremental builds work.

Looks like buck has a couple of issues:
- it's "illegal" to have multiple gerrit_plugin targets in a single BUCK file,
  i.e. their MANIFESTs will silently collide.
- it's illegal to create gerrit_plugin targets with no source files.
diff --git a/BUCK b/BUCK
index df25896..da2e37a 100644
--- a/BUCK
+++ b/BUCK
@@ -1,101 +1,4 @@
-include_defs('//lib/maven.defs')
-include_defs('//lib/prolog/prolog.defs')
-
-JACKSON_REV = '2.1.1'
-maven_jar(
-  name = 'jackson-core',
-  id = 'com.fasterxml.jackson.core:jackson-core:%s' % JACKSON_REV,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-  name = 'jackson-databind',
-  id = 'com.fasterxml.jackson.core:jackson-databind:%s' % JACKSON_REV,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-  name = 'jackson-annotations',
-  id = 'com.fasterxml.jackson.core:jackson-annotations:%s' % JACKSON_REV,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-  name = 'jackson-dataformat-yaml',
-  id = 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:%s' % JACKSON_REV,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-  name = 'gitective-core',
-  id = 'org.gitective:gitective-core:0.9.9',
-  license = 'Apache2.0',
-)
-
-EXTERNAL_DEPS = [
-  ':gitective-core',
-  ':jackson-core',
-  ':jackson-databind',
-  ':jackson-annotations',
-  ':jackson-dataformat-yaml',
-]
-
-java_library(
-  name = 'gerrit-owners-common-lib',
-  srcs = glob([
-    'gerrit-owners-common/src/main/java/**/*.java',
-  ]),
-  deps = [
-    '//:plugin-lib',
-  ] + EXTERNAL_DEPS,
-)
-
-java_library(
-  name = 'gerrit-owners-lib',
-  srcs = glob([
-    'gerrit-owners/src/main/java/**/*.java',
-  ]),
-  deps = [
-    ':gerrit-owners-common-lib',
-    '//:plugin-lib',
-    '//lib/prolog:prolog-cafe',
-    '//gerrit-server/src/main/prolog:common',
-  ] + EXTERNAL_DEPS,
-)
-
-prolog_cafe_library(
-  name = 'gerrit-owners-prolog-rules',
-  srcs = glob(['gerrit-owners/src/main/prolog/*.pl']),
-  deps = [
-    '//gerrit-server/src/main/prolog:common',
-    ':gerrit-owners-lib',
-  ],
-)
-
-gerrit_plugin(
-  name = 'owners',
-  srcs = [],
-  manifest_entries = [
-    'Implementation-Title: Gerrit OWNERS plugin',
-    'Implementation-URL: https://github.com/vadims/gerrit-owners',
-    'Gerrit-PluginName: owners',
-  ],
-  deps = [
-    ':gerrit-owners-prolog-rules',
-  ] + EXTERNAL_DEPS,
-)
-
-gerrit_plugin(
-  name = 'owners-autoassign',
-  srcs = glob([
-    'gerrit-owners-autoassign/src/main/java/**/*.java',
-  ]),
-  manifest_entries = [
-    'Implementation-Title: Gerrit OWNERS autoassign plugin',
-    'Implementation-URL: https://github.com/vadims/gerrit-owners',
-    'Gerrit-PluginName: owners-autoassign',
-  ],
-  deps = [
-    ':gerrit-owners-common-lib',
-  ] + EXTERNAL_DEPS,
-)
+# Look in the subdirectories' BUCK files for gerrit_plugin targets.
+#
+# I would like to have all the gerrit_plugin targets defined here, but there can only
+# be a single gerrit_plugin target in each BUCK file.
diff --git a/common.defs b/common.defs
new file mode 100644
index 0000000..17f4c86
--- /dev/null
+++ b/common.defs
@@ -0,0 +1,51 @@
+include_defs('//lib/maven.defs')
+include_defs('//lib/prolog/prolog.defs')
+
+JACKSON_REV = '2.1.1'
+maven_jar(
+  name = 'jackson-core',
+  id = 'com.fasterxml.jackson.core:jackson-core:%s' % JACKSON_REV,
+  license = 'Apache2.0',
+)
+
+maven_jar(
+  name = 'jackson-databind',
+  id = 'com.fasterxml.jackson.core:jackson-databind:%s' % JACKSON_REV,
+  license = 'Apache2.0',
+)
+
+maven_jar(
+  name = 'jackson-annotations',
+  id = 'com.fasterxml.jackson.core:jackson-annotations:%s' % JACKSON_REV,
+  license = 'Apache2.0',
+)
+
+maven_jar(
+  name = 'jackson-dataformat-yaml',
+  id = 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:%s' % JACKSON_REV,
+  license = 'Apache2.0',
+)
+
+maven_jar(
+  name = 'gitective-core',
+  id = 'org.gitective:gitective-core:0.9.9',
+  license = 'Apache2.0',
+)
+
+EXTERNAL_DEPS = [
+  ':gitective-core',
+  ':jackson-core',
+  ':jackson-databind',
+  ':jackson-annotations',
+  ':jackson-dataformat-yaml',
+]
+
+# These are dependencies that must be made available to the plugins' libraries at compilation
+# time, but should not be included in the plugins' jar files since they will be provided
+# by the gerrit server jar.
+# For this reason all the intermediate java libraries that we build are java_library2 targets
+# rather than java_library.
+COMPILE_DEPS = [
+  '//:plugin-lib',
+  '//lib/prolog:prolog-cafe',
+]
diff --git a/gerrit-owners-autoassign/BUCK b/gerrit-owners-autoassign/BUCK
new file mode 100644
index 0000000..18d0393
--- /dev/null
+++ b/gerrit-owners-autoassign/BUCK
@@ -0,0 +1,16 @@
+include_defs('//plugins/gerrit-owners/common.defs')
+
+gerrit_plugin(
+  name = 'owners-autoassign',
+  srcs = glob([
+    'src/main/java/**/*.java',
+  ]),
+  manifest_entries = [
+    'Implementation-Title: Gerrit OWNERS autoassign plugin',
+    'Implementation-URL: https://github.com/vadims/gerrit-owners',
+    'Gerrit-PluginName: owners-autoassign',
+  ],
+  deps = [
+    '//plugins/gerrit-owners/gerrit-owners-common:common',
+  ] + EXTERNAL_DEPS,
+)
diff --git a/gerrit-owners-common/BUCK b/gerrit-owners-common/BUCK
new file mode 100644
index 0000000..ea5fb6a
--- /dev/null
+++ b/gerrit-owners-common/BUCK
@@ -0,0 +1,14 @@
+include_defs('//plugins/gerrit-owners/common.defs')
+
+# see common.defs on why this is a java_library2 rather than java_library
+java_library2(
+  name = 'common',
+  srcs = glob([
+    'src/main/java/**/*.java',
+  ]),
+  deps = [],
+  # Notice that we put EXTERNAL_DEPS as compile_deps to prevent double inclusion in the owners
+  # plugin.
+  compile_deps = COMPILE_DEPS + EXTERNAL_DEPS,
+  visibility = ['PUBLIC'],
+)
diff --git a/gerrit-owners/BUCK b/gerrit-owners/BUCK
new file mode 100644
index 0000000..0ef0040
--- /dev/null
+++ b/gerrit-owners/BUCK
@@ -0,0 +1,42 @@
+include_defs('//plugins/gerrit-owners/common.defs')
+
+# buck is unhappy to build a gerrit_plugin with no source files.
+# On the other side, gerrit-owners-prolog-rules needs to be built after all the java files have been
+# compiled. For this reason we have a stub class that has no content and just makes buck happy.
+COMPILE_STUB = ['src/main/java/com/vmware/gerrit/owners/CompileStub.java']
+
+java_library2(
+  name = 'gerrit-owners-lib',
+  srcs = glob([
+    'src/main/java/**/*.java',
+  ], excludes=COMPILE_STUB),
+  deps = [
+    '//plugins/gerrit-owners/gerrit-owners-common:common',
+  ],
+  # Notice that we put EXTERNAL_DEPS as compile_deps to prevent double inclusion in the owners
+  # plugin.
+  compile_deps = COMPILE_DEPS + EXTERNAL_DEPS,
+)
+
+prolog_cafe_library(
+  name = 'gerrit-owners-prolog-rules',
+  srcs = glob(['src/main/prolog/*.pl']),
+  deps = [
+    ':gerrit-owners-lib',
+    '//gerrit-server/src/main/prolog:common',
+  ],
+)
+
+gerrit_plugin(
+  name = 'owners',
+  srcs = COMPILE_STUB,
+  manifest_entries = [
+    'Implementation-Title: Gerrit OWNERS plugin',
+    'Implementation-URL: https://github.com/vadims/gerrit-owners',
+    'Gerrit-PluginName: owners',
+  ],
+  deps = [
+    ':gerrit-owners-lib',
+    ':gerrit-owners-prolog-rules',
+  ] + EXTERNAL_DEPS,
+)
diff --git a/gerrit-owners/src/main/java/com/vmware/gerrit/owners/CompileStub.java b/gerrit-owners/src/main/java/com/vmware/gerrit/owners/CompileStub.java
new file mode 100644
index 0000000..c378ff0
--- /dev/null
+++ b/gerrit-owners/src/main/java/com/vmware/gerrit/owners/CompileStub.java
@@ -0,0 +1,7 @@
+package com.vmware.gerrit.owners;
+
+/**
+ * A stub class only used for pleasing BUCK.
+ */
+public class CompileStub {
+}
diff --git a/gerrit-owners/src/main/java/com/vmware/gerrit/owners/OwnersStoredValues.java b/gerrit-owners/src/main/java/com/vmware/gerrit/owners/OwnersStoredValues.java
index bf5fd52..66c3154 100644
--- a/gerrit-owners/src/main/java/com/vmware/gerrit/owners/OwnersStoredValues.java
+++ b/gerrit-owners/src/main/java/com/vmware/gerrit/owners/OwnersStoredValues.java
@@ -14,11 +14,15 @@
 import com.googlecode.prolog_cafe.lang.Prolog;
 import com.googlecode.prolog_cafe.lang.SystemException;
 import org.eclipse.jgit.lib.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 
 /**
  * StoredValues for the Gerrit OWNERS plugin.
  */
 public class OwnersStoredValues {
+  private static final Logger log = LoggerFactory.getLogger(OwnersStoredValues.class);
 
   public static StoredValue<PathOwners> PATH_OWNERS;
 
@@ -27,6 +31,7 @@
     if (PATH_OWNERS != null) {
       return;
     }
+    log.error("Initializing OwnerStoredValues");
     PATH_OWNERS = new StoredValue<PathOwners>() {
       @Override
       protected PathOwners createValue(Prolog engine) {