Skip to content

Gradle Build Plugin

Ben Fagin edited this page Mar 30, 2015 · 9 revisions

Starting with version 0.8, a Gradle build plugin is available. This allows you to define your descriptor in a separate module, and have the code generation occur on the fly with each build.

There are several components:

  • The build plugin, which reads descriptors, generates sources, and compiles them. The plugin can also write out the runtime files so that consumers of your jar do not have to include the runtime jar either as a dependency or transitive dependency.
  • The runtime, either included as a dependency ('flapi-runtime') or embedded in your output sources.

An example of this setup is included in the project. See the build-test-producer and build-test-consumer modules.

Add the plugin.

Add the plugin to your build script.

buildscript {
  repositories {
    maven {
      url 'http://www.unquietcode.com/maven/releases'
    }
  }

  dependencies {
    classpath 'unquietcode.tools.flapi:flapi-gradle-plugin:0.8'
  }
}
apply plugin: 'com.unquietcode.flapi.plugin'

Declare dependencies.

If your descriptor is declared in the same project, you will probably also need to explicitly declare a dependency on Flapi. (A common pattern is to have descriptor classes built in the test classpath, so they will not be bundled with the jar.)

dependencies {
  testCompile 'unquietcode.tools.flapi:flapi:0.8'
}

Configure the task.

Modify the plugin configuration closure as needed.

flapi {
	/**
	 * The list of {@link unquietcode.tools.flapi.DescriptorMaker} classes.
	 */
	String[] descriptorClasses;

	/**
	 * The directory to which the generated classes
	 * will be written. (default is build/generated-sources)
	 */
	String classesDirectory;

	/**
	 * The directory to which the generated sources
	 * will be written. (default is src/main/java)
	 */
	String sourcesDirectory;

	/**
	 * If true, the runtime classes will be written
	 * out alongside the generated classes.
	 */
	boolean includeRuntime = true

	/**
	 * If true, the sources will be written.
	 */
	boolean writeSources = true

	/**
	 * If true, the sources will be compiled and written.
	 */
	boolean writeClasses = true
}

Run the build.

Calling gradle build on your project will cause the new flapi task to be executed after the Java compilation task completes.

Clone this wiki locally