Improve Gradle build of protoc grpc plugin

A Gradle protoc plugin is used for generating and compiling the grpc
codegen. The code organization was changed to match what Gradle expects.

Proto 3 is now required.
diff --git a/README.md b/README.md
index 97f4f02..f4b5e80 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,48 @@
 grpc-java
 =========
 
+How to Build
+------------
+
+grpc-java requires Netty 5, which is still in flux. The version we need can be
+found in the lib/netty submodule:
+```
+$ git submodule update --init
+$ cd lib/netty
+$ mvn install -pl codec-http2 -am -DskipTests=true
+```
+
+The codegen plugin requires a recent protobuf build from master (what will
+become proto3):
+```
+$ git clone https://github.com/google/protobuf.git
+$ cd protobuf
+$ ./autogen.sh
+$ ./configure
+$ make
+$ make check
+$ sudo make install
+$ cd java
+$ mvn install
+```
+
+If you are comfortable with C++ compilation and autotools, you can specify a
+--prefix for protobuf and use -I in CXXFLAGS, -L in LDFLAGS, LD\_LIBRARY\_PATH,
+and PATH to reference it. The environment variables will be used when building
+grpc-java.
+
+Now to build grpc-java itself:
+```
+$ ./gradlew install
+```
+
+Navigating Around the Source
+----------------------------
+
 Heres a quick readers guide to the code to help folks get started. At a high level there are three distinct layers
 to the library: stub, channel & transport. 
 
-## Stub
+### Stub
 
 The 'stub'  layer is what is exposed to most developers and provides type-safe bindings to whatever 
 datamodel/IDL/interface you are adapting. An example is provided of a binding to code generated by the protocol-buffers compiler but others should be trivial to add and are welcome.
@@ -14,7 +52,7 @@
 [Stream Observer](https://github.com/google/grpc-java/blob/master/stub/src/main/java/io/grpc/stub/StreamObserver.java)
 
 
-## Channel
+### Channel
 
 The 'channel' layer is an abstraction over transport handling that is suitable for interception/decoration and exposes more behavior to the application than the stub layer. It is intended to be easy for application frameworks to use this layer to address cross-cutting concerns such as logging, monitoring, auth etc. Flow-control is also exposed at this layer to allow more sophisticated applications to interact with it directly.
 
@@ -33,7 +71,7 @@
 * [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
 
 
-## Transport
+### Transport
 
 The 'transport' layer does the heavy lifting of putting & taking bytes off the wire. The interfaces to it are abstract just enough to allow plugging in of different implementations. Transports are modeled as 'Stream' factories. The variation in interface between a server stream and a client stream exists to codify their differing semantics for cancellation and error reporting.
 
@@ -53,6 +91,6 @@
 * [Server Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStreamListener.java)
 
 
-# Examples
+### Examples
 
 Tests showing how these layers are composed to execute calls using protobuf messages can be found here https://github.com/google/grpc-java/tree/master/integration-testing/src/main/java/io/grpc/testing/integration
diff --git a/build.gradle b/build.gradle
index 02b667b..1bcdf1b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -20,7 +20,7 @@
 
     // External dependency management
     ext.libraries = [
-        protobuf: 'com.google.protobuf:protobuf-java:2.6.1',
+        protobuf: 'com.google.protobuf:protobuf-java:3.0.0-pre',
         guava: 'com.google.guava:guava:18.0',
         jsr305: 'com.google.code.findbugs:jsr305:3.0.0',
         oauth_client: 'com.google.oauth-client:google-oauth-client:1.18.0-rc',
diff --git a/compiler/build.gradle b/compiler/build.gradle
index acfd0f5..3f12ad6 100644
--- a/compiler/build.gradle
+++ b/compiler/build.gradle
@@ -1,11 +1,26 @@
 apply plugin: "cpp"
+apply plugin: "protobuf"
 
 description = 'The protoc plugin for gRPC Java'
 
+buildscript {
+    repositories {
+        mavenCentral()
+    }
+    dependencies {
+        classpath libraries.protobuf_plugin
+    }
+}
+
 executables {
   java_plugin {}
 }
 
+dependencies {
+  compile project(':grpc-stub'),
+          libraries.protobuf
+}
+
 binaries.all {
   if (toolChain in Gcc) {
     cppCompiler.args "-std=c++11"
@@ -22,19 +37,13 @@
   }
 }
 
-sources {
-  java_plugin {
-    // Configure an existing CppSourceSet
-    cpp {
-      source {
-        srcDirs "src/"
-        include "**/*.cc"
-      }
-    }
-  }
-}
+protobufCodeGenPlugins = ["java_plugin:$buildDir/binaries/java_pluginExecutable/java_plugin"]
 
-task test(type: Exec, dependsOn: 'java_pluginExecutable') {
-  environment 'TEST_TMP_DIR', temporaryDir
-  commandLine './run_test.sh'
+generateTestProto.dependsOn 'java_pluginExecutable'
+test.dependsOn 'testGolden'
+
+task testGolden(type: Exec, dependsOn: 'generateTestProto') {
+  executable "diff"
+  args "$buildDir/generated-sources/test/io/grpc/testing/integration/TestServiceGrpc.java",
+       "$projectDir/src/test/golden/TestService.java.txt"
 }
diff --git a/compiler/run_test.sh b/compiler/run_test.sh
deleted file mode 100755
index 5f311ba..0000000
--- a/compiler/run_test.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-
-if [ -z "$TEST_TMP_DIR" ]; then
-  echo '$TEST_TMP_DIR not set'
-  exit 1;
-fi
-
-cd $(dirname $0)
-
-TEST_SRC_DIR='test'
-INPUT_FILE="$TEST_SRC_DIR/test.proto"
-OUTPUT_FILE="$TEST_TMP_DIR/TestServiceGrpc.src.jar"
-GOLDEN_FILE="$TEST_SRC_DIR/TestService.java.txt"
-
-protoc --plugin=protoc-gen-java_rpc=build/binaries/java_pluginExecutable/java_plugin \
-  --java_rpc_out="$OUTPUT_FILE" --proto_path="$TEST_SRC_DIR" "$INPUT_FILE" && \
-  unzip -o -d "$TEST_TMP_DIR" "$OUTPUT_FILE" && \
-  diff "$TEST_TMP_DIR/io/grpc/testing/integration/TestServiceGrpc.java" \
-    "$GOLDEN_FILE" && \
-  echo "PASS"
diff --git a/compiler/src/java_generator.cc b/compiler/src/java_plugin/cpp/java_generator.cpp
similarity index 100%
rename from compiler/src/java_generator.cc
rename to compiler/src/java_plugin/cpp/java_generator.cpp
diff --git a/compiler/src/java_generator.h b/compiler/src/java_plugin/cpp/java_generator.h
similarity index 100%
rename from compiler/src/java_generator.h
rename to compiler/src/java_plugin/cpp/java_generator.h
diff --git a/compiler/src/java_plugin.cc b/compiler/src/java_plugin/cpp/java_plugin.cpp
similarity index 100%
rename from compiler/src/java_plugin.cc
rename to compiler/src/java_plugin/cpp/java_plugin.cpp
diff --git a/compiler/test/TestService.java.txt b/compiler/src/test/golden/TestService.java.txt
similarity index 100%
rename from compiler/test/TestService.java.txt
rename to compiler/src/test/golden/TestService.java.txt
diff --git a/compiler/test/test.proto b/compiler/src/test/proto/test.proto
similarity index 100%
rename from compiler/test/test.proto
rename to compiler/src/test/proto/test.proto
diff --git a/settings.gradle b/settings.gradle
index ccf44c5..41d99c0 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -5,6 +5,7 @@
 include ":grpc-okhttp"
 include ":grpc-netty"
 include ":grpc-testing"
+include ":grpc-compiler"
 include ":grpc-integration-testing"
 include ":grpc-all"
 
@@ -14,5 +15,6 @@
 project(':grpc-okhttp').projectDir = "$rootDir/okhttp" as File
 project(':grpc-netty').projectDir = "$rootDir/netty" as File
 project(':grpc-testing').projectDir = "$rootDir/testing" as File
+project(':grpc-compiler').projectDir = "$rootDir/compiler" as File
 project(':grpc-integration-testing').projectDir = "$rootDir/integration-testing" as File
 project(':grpc-all').projectDir = "$rootDir/all" as File