A Gradle plugin that utilizes google-java-format to format the Java source files in your Gradle project.
-
Apply the plugin in your build script (follow these instructions for Gradle versions below
2.1)plugins { id 'com.github.sherter.google-java-format' version '0.9' } -
Make sure you have defined a repository that contains version
1.8ofgoogle-java-formatrepositories { mavenCentral() } -
Execute the task
googleJavaFormatto format all*.javafiles in the project$ ./gradlew goJF
-
Execute the task
verifyGoogleJavaFormatto verify that all*.javafiles are formatted properly$ ./gradlew verGJF
-
The plugin adds the extension
googleJavaFormatto your project. Adjust the variabletoolVersionto use a specific version ofgoogle-java-format. You can even defineSNAPSHOTversions, but make sure that you have added a repository to the project that contains this version (e.g.mavenLocal()). For plugin version0.9this value defaults to1.8. On every new release the default value will be updated to the latest version ofgoogle-java-format.googleJavaFormat { toolVersion = '1.1-SNAPSHOT' } -
Choose betweeen
'GOOGLE'(default) and'AOSP'style by setting the style option:googleJavaFormat { options style: 'AOSP' } -
The extension object allows you to configure the default inputs for all tasks related to this plugin. It provides the same methods as a
SourceTaskto set these inputs. Initially, aFileTreethat contains all*.javafiles in the project directory (and recursivly all subdirectories, excluding files in a (sub)project’sbuildDir) is added with onesourcemethod call. However, this initial value for the default inputs can be overwritten (usingsetSource), extended (using additionalsourcecalls) and/or further filtered (usingincludeand/orexcludepatterns, see Ant-style exclude patterns). The chapter aboutWorking With Filesin the Gradle user guide might be worth reading.googleJavaFormat { source = sourceSets*.allJava source 'src/special_dir' include '**/*.java' exclude '**/*Template.java' exclude 'src/test/template_*' } -
All tasks are of type
SourceTaskand can be configured accordingly. In addition to the default tasksgoogleJavaFormatandverifyGoogleJavaFormatyou can define custom tasks if you like. The task typeVerifyGoogleJavaFormatalso implements the interfaceVerificationTask.import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormat import com.github.sherter.googlejavaformatgradleplugin.VerifyGoogleJavaFormat task format(type: GoogleJavaFormat) { source 'src/main' source 'src/test' include '**/*.java' exclude '**/*Template.java' } task verifyFormatting(type: VerifyGoogleJavaFormat) { source 'src/main' include '**/*.java' ignoreFailures true }
If you set sources in a task (by calling
setSourceand/orsource), the default inputs from the extension object are not added to the final set of sources to process. However, if you don’t modify the sources in the task and only addincludeand/orexcludepatterns, the default inputs are first added and then further filtered according the the patterns. -
An additional include filter can be applied on the command line at execution time by setting the
<taskName>.includesystem property. All input files that remain after applying the filters inbuild.gradlealso have to match at least one of the patterns in the comma separated list in order to be eventually processed by the task. Note that this only allows you to further reduce the number of processed files. You can not use this to add anotherincludemethod call to a task. (See contrib/pre-commit for a useful application of this feature).$ ./gradlew verGJF -DverifyGoogleJavaFormat.include="*Foo.java,bar/Baz.java"
On every push to the master branch Travis runs
the tests and, if all tests pass, publishes the built artifact to
Sonatype’s
snapshots repository. Use the following build script snippet for
the current snapshot version:
buildscript {
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.github.sherter.googlejavaformatgradleplugin:google-java-format-gradle-plugin:0.9-SNAPSHOT'
}
}
apply plugin: 'com.github.sherter.google-java-format'