Skip to content

Commit e5f6a1e

Browse files
committed
Allow to toggle service provider config generation.
1 parent fac8cad commit e5f6a1e

4 files changed

Lines changed: 69 additions & 4 deletions

File tree

docs/asciidoc/mvc-api.adoc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,50 @@
33
MVC API is an alternative way to define routes in Jooby. It uses annotations and byte code generation
44
to define and execute routes.
55

6+
If you use Gradle 6.0 or a later version, you can leverage incremental annotation processing support,
7+
which means that Gradle only compiles classes that changed since the last compilation, and only runs
8+
annotation processing on those changed classes.
9+
10+
The annotation processor has two options allowing you to control incremental processing behavior:
11+
12+
.build.gradle
13+
[source, groovy, role = "primary", subs="verbatim,attributes"]
14+
----
15+
tasks.withType(JavaCompile) {
16+
options.compilerArgs += [
17+
'-parameters',
18+
'-Ajooby.incremental=true',
19+
'-Ajooby.services=true'
20+
]
21+
}
22+
----
23+
24+
.Kotlin
25+
[source, groovy, role = "secondary", subs="verbatim,attributes"]
26+
----
27+
kapt {
28+
arguments {
29+
arg('jooby.incremental', true)
30+
arg('jooby.services', true)
31+
}
32+
}
33+
----
34+
35+
By setting `jooby.incremental` to `false` you can disable incremental processing entirely, which means
36+
the regardless what's changed, the whole project is recompiled each time. Defaults to `true`.
37+
38+
The generated bytecode is responsible for registering routes, retrieving and invoking your controllers.
39+
Jooby loads these classes with Java's service-provider loading facility by default. To make this work,
40+
a so-called _provider configuration_ file needs to be created alongside with the generated classes.
41+
The content of this file is dependent on all MVC controllers, therefore the annotation processor
42+
must operate in *aggregating* mode, in which _all generated_ classes are rewritten each time.
43+
44+
You may disable the generation of the provider configuration file by setting `jooby.services` to `false`
45+
(the default is `true`). This allows the annotation processor to run in *isolating* mode: if you
46+
change e.g. `HelloController` only, then only the class responsible for registering the routes for
47+
`HelloController` will be regenerated. This however will force Jooby to load the generated classes
48+
with reflection instead of the service-provider loading facility.
49+
650
The package `io.jooby.annotations` contains all the annotations available for MVC routes.
751

852
.MVC API:

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
*/
4949
@SupportedOptions({
5050
JoobyProcessor.OPT_DEBUG,
51-
JoobyProcessor.OPT_INCREMENTAL })
51+
JoobyProcessor.OPT_INCREMENTAL,
52+
JoobyProcessor.OPT_SERVICES })
5253
public class JoobyProcessor extends AbstractProcessor {
5354

5455
protected static final String OPT_DEBUG = "jooby.debug";
5556
protected static final String OPT_INCREMENTAL = "jooby.incremental";
57+
protected static final String OPT_SERVICES = "jooby.services";
5658

5759
private ProcessingEnvironment processingEnv;
5860

@@ -66,15 +68,22 @@ public class JoobyProcessor extends AbstractProcessor {
6668

6769
private boolean debug;
6870
private boolean incremental;
71+
private boolean services;
6972

7073
private int round;
7174

7275
@Override public Set<String> getSupportedOptions() {
7376
Set<String> options = new HashSet<>(super.getSupportedOptions());
7477

7578
if (incremental) {
76-
// enables incremental annotation processing support in Gradle
77-
options.add("org.gradle.annotation.processing.aggregating");
79+
// Enables incremental annotation processing support in Gradle.
80+
// If service provider configuration is being generated,
81+
// only 'aggregating' mode is supported since it's likely that
82+
// more then one originating element is passed to the Filer
83+
// API on writing the resource file - isolating mode does not
84+
// allow this.
85+
options.add(String.format("org.gradle.annotation.processing.%s",
86+
services ? "aggregating" : "isolating"));
7887
}
7988

8089
return options;
@@ -94,8 +103,10 @@ public class JoobyProcessor extends AbstractProcessor {
94103

95104
debug = boolOpt(processingEnv, OPT_DEBUG, false);
96105
incremental = boolOpt(processingEnv, OPT_INCREMENTAL, true);
106+
services = boolOpt(processingEnv, OPT_SERVICES, true);
97107

98108
debug("Incremental annotation processing is turned %s.", incremental ? "ON" : "OFF");
109+
debug("Generation of service provider configuration is turned %s.", services ? "ON" : "OFF");
99110
}
100111

101112
@Override
@@ -207,7 +218,9 @@ private void build(Filer filer) throws Exception {
207218
modules.put(type, moduleClass);
208219
}
209220

210-
doServices(filer, modules);
221+
if (services) {
222+
doServices(filer, modules);
223+
}
211224
}
212225

213226
private String signature(ExecutableElement method) {

modules/jooby-cli/src/main/resources/cli/build.gradle.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ tasks.withType(JavaCompile) {
7070
'-parameters',
7171
{{#if apt}}
7272
'-Ajooby.incremental=true',
73+
'-Ajooby.services=true',
7374
'-Ajooby.debug=false'
7475
{{/if}}
7576
]
@@ -81,6 +82,7 @@ tasks.withType(JavaCompile) {
8182
kapt {
8283
arguments {
8384
arg('jooby.incremental', true)
85+
arg('jooby.services', true)
8486
arg('jooby.debug', false)
8587
}
8688
}

modules/jooby-cli/src/main/resources/cli/pom.xml.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
<annotationProcessorArg>
9191
jooby.debug=false
9292
</annotationProcessorArg>
93+
<annotationProcessorArg>
94+
jooby.services=true
95+
</annotationProcessorArg>
9396
</annotationProcessorArgs>
9497
</configuration>
9598
</execution>
@@ -125,6 +128,9 @@
125128
<compilerArg>
126129
-Ajooby.debug=false
127130
</compilerArg>
131+
<compilerArg>
132+
-Ajooby.services=true
133+
</compilerArg>
128134
</compilerArgs>
129135
</configuration>
130136
{{/if}}

0 commit comments

Comments
 (0)