This repository contains a gradle plugin for the Java analysis and transformation framework Spoon.
Note that there is also an official gradle plugin provided by SpoonLabs. The difference between the two plugins is that SpoonLabs plugin focuses on analysis and transformation of Java production files, while this plugin focuses on analysis and transformation of Java production and test files. Another difference is that SpoonLabs plugin supports Android development, which is not supported by this plugin.
The spoon-gradle-plugin requires the Gradle Java Plugin and is able to process all Java source sets:
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
test {
java {
srcDir 'src/test/java '
}
}
beans {
java {
srcDir 'src/main/java'
}
//dependencies
}
beansTest {
java {
srcDir 'src/test/java '
}
//dependencies
}
}
Add a buildscript section to your build.gradle file and define the required classpath dependencies to the spoon-gradle-plugin and to org.eclipse.jdt.core.
Note that the spoon-gradle-plugin currently supports Spoon version 10.1.0, which requires org.eclipse.jdt.core version 3.34.0.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'com.github.mictaege', name: 'spoon-gradle-plugin', version:'x.x'
classpath group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.core', version: '3.33.0'
}
}
Then apply the spoon plugin:
apply plugin: 'spoon'
In order to add your Spoon processors you have to add an additional dependency in the buildscript section to the library that contains your processors
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'com.github.mictaege', name: 'spoon-gradle-plugin', version:'x.x'
classpath group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.core', version: '3.33.0'
classpath group: 'mygroup', name: 'my-processors', version: '1.0'
}
}
Then you have to configure the processors with the fully qualified name
spoon{
processors = ['com.github.mictaege.spoon_processors.CheckNotNullProcessor']
}
Now the configured processors are being executed every time bevore compileJava and compileTestJava are executed.
The sources generated by Spoon will end up in ./build/generated-sources/spoon and the classes compiled from that generated sources will end up in ./build/classes/
build
├───classes
│ ├───main
│ │ └───...
│ └───test
│ └───...
├───generated-sources
│ └───spoon
│ ├───main
│ │ └───...
│ └───test
│ └───...
Within the spoon section you can also set the following flags
- compliance: Java source code compliance level (1,2,3,4,5, 6, 7 or 8). (default: 8)
- exclude: Exclude Java source sets from spoon processing. (default: none)
- fileFilter: A filter closure to specify which source files should be spooned. (default: all source files)
spoon{
processors = ['com.github.mictaege.spoon_processors.CheckNotNullProcessor']
compliance 7
exclude = ['main', 'test']
fileFilter = { File src -> src.text.contains("@NotNull") }
}
apply plugin: 'java'
apply plugin: 'spoon'
group 'xxx.xxxx.xxxx'
archivesBaseName = project.name
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'com.github.mictaege', name: 'spoon-gradle-plugin', version:'x.x'
classpath group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.core', version: '3.34.0'
classpath group: 'xxx.xxxx.xxxx', name: 'xxx-processors', version: 'x.x'
}
}
repositories {
mavenCentral()
}
spoon{
processors = ['xxx.xxxx.xxxx.XXXXProcessor']
}