title: simple protobuf compilation with gradle
body: <p>If you're looking for sample gradle protobuf project look <a href="https://github.com/google/protobuf-gradle-plugin/tree/master/examples/exampleProject" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I'm having hard time with gradle and protobuf, 
  i want to create a simple gradle project that will take any proto files from default <code>src/main/proto</code>, <code>src/test/proto</code> and compile them to <code>src/main/java</code>, <code>src/test/java</code> accordingly, then pack that into a jar and publish to local repo.</p>
  
  <p>Unfortunately i'm new to gradle and cant figure out how the original project is composed.</p>
  
  <p>Here is my unfinished build.gradle file </p>
  
  <pre><code>apply plugin: 'java'
  apply plugin: "com.google.protobuf"
  
  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
      }
  }
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      compile 'com.google.protobuf:protobuf-java:3.0.0-beta-1'
  }
  
  sourceSets {
      main {
          proto {
              srcDir 'src/main/proto'
          }
          java {
              srcDir 'src/main/java'
          }
      }
      test {
          proto {
              srcDir 'src/test/proto'
          }
          proto {
              srcDir 'src/test/java'
          }
      }
  }
  
  protobuf {
      // Configure the protoc executable
      protoc {
          // Download from repositories
          artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3'
      }
      generateProtoTasks {
          // all() returns the collection of all protoc tasks
          all().each { task -&gt;
              // Here you can configure the task
          }
  
          // In addition to all(), you may get the task collection by various
          // criteria:
  
          // (Java only) returns tasks for a sourceSet
          ofSourceSet('main')
  
      }
  }
  </code></pre>
  
  <p>After runing jar task we have this : </p>
  
  <p><a href="https://i.stack.imgur.com/jVRho.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jVRho.png" alt="enter image description here"></a></p>
  
  <p>as you can see gradle builds both test and main protos to the same classes directory (red arrows), in the jar i can see both generated classes included (while tests should be skipped).</p>
  
  <p>but the main problem is that I want to make <strong>compile proto files directly to appropriate source directories</strong> (blue arrows), after that ordinary build will do the correct thing... After all we need those classes in src to use them in business logic...</p>
  
  <p>So we only need one task that compiles proto to appropriate src directory... nothing more.</p>
  
  <pre><code>src/main/proto to src/main/java
  src/test/proto to src/test/java
  </code></pre>
  
  <p>The current project as it is is located <a href="https://github.com/vach/sample-gradle-protobuf" rel="nofollow noreferrer">here</a>. Please help to configure this, i'm pretty sure lot of people will need it later...</p>