Skip to content

Commit 45e4a25

Browse files
committed
joobyRun: new property watchDirs
fix #4060
1 parent 79aa596 commit 45e4a25

4 files changed

Lines changed: 161 additions & 14 deletions

File tree

‎modules/jooby-gradle-plugin/README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,37 @@ pluginManagement {
2828
rootProject.name = 'your project name'
2929
```
3030

31+
# Build-time templates
32+
33+
For templates compiled by an annotation processor, configure the application's `build.gradle`:
34+
35+
```groovy
36+
tasks.named('joobyRun') {
37+
watchDirs = ['web']
38+
compileExtensions = ['java', 'kt', 'hbs']
39+
}
40+
41+
tasks.named('compileJava', JavaCompile) {
42+
inputs.files(fileTree('web') {
43+
include '**/*.hbs'
44+
})
45+
.withPropertyName('templates')
46+
.withPathSensitivity(PathSensitivity.RELATIVE)
47+
}
48+
```
49+
50+
`watchDirs` adds directories to Jooby's file watcher. Paths are absolute or relative to the project
51+
directory. Source directories, resource directories and `conf` are already watched, so additional
52+
watch directories are only necessary for custom locations such as `web`.
53+
54+
`compileExtensions` requests a build when a matching file changes. The `compileJava` inputs ensure
55+
Gradle reruns Java compilation and annotation processing when template contents change. The
56+
application restarts only after a successful build, without touching Java source files.
57+
58+
Both settings are needed: watching a directory does not declare compiler inputs, and declaring
59+
compiler inputs does not add that directory to Jooby's watcher. The same configuration applies to
60+
`joobyTestRun`; declare inputs on the compile task that runs the template processor.
61+
62+
Gradle fingerprints file contents, so a timestamp-only touch does not necessarily trigger
63+
compilation. Template input changes cause full Java recompilation; this setup does not provide
64+
per-template incremental compilation.

‎modules/jooby-gradle-plugin/src/main/java/io/jooby/gradle/RunTask.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class RunTask extends BaseTask {
5959

6060
private List<String> compileExtensions;
6161

62+
private List<String> watchDirs;
63+
6264
private Integer port;
6365

6466
/**
@@ -174,6 +176,15 @@ public void run() throws Throwable {
174176
joobyRun.addResource(conf);
175177
joobyRun.addWatchDir(conf, onFileChanged);
176178

179+
if (watchDirs != null) {
180+
for (var watchDir : watchDirs) {
181+
Path path = project.getProjectDir().toPath().resolve(watchDir).normalize();
182+
if (!joobyRun.addWatchDir(path, onFileChanged)) {
183+
getLogger().warn("Watch directory does not exist: " + path);
184+
}
185+
}
186+
}
187+
177188
// build classes
178189
binDirectories(project, sourceSet).forEach(joobyRun::addClasses);
179190

@@ -342,6 +353,28 @@ public void setWaitTimeBeforeRestart(Long waitTimeBeforeRestart) {
342353
this.waitTimeBeforeRestart = waitTimeBeforeRestart;
343354
}
344355

356+
/**
357+
* Additional directories to watch, resolved relative to each project directory. Changes are
358+
* handled according to compileExtensions and restartExtensions.
359+
*
360+
* @return Additional directories to watch.
361+
*/
362+
@Input
363+
@org.gradle.api.tasks.Optional
364+
public List<String> getWatchDirs() {
365+
return watchDirs;
366+
}
367+
368+
/**
369+
* Set additional directories to watch. Extensions in compileExtensions trigger a build before
370+
* restarting; extensions in restartExtensions only trigger a restart.
371+
*
372+
* @param watchDirs Directories, absolute or relative to each project directory.
373+
*/
374+
public void setWatchDirs(List<String> watchDirs) {
375+
this.watchDirs = watchDirs;
376+
}
377+
345378
/**
346379
*
347380
* Shutdown without killing gradle daemon on ENTER KEY.

‎modules/jooby-maven-plugin/src/main/java/io/jooby/maven/RunMojo.java‎

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
14+
import java.util.ArrayList;
1415
import java.util.Collections;
1516
import java.util.List;
1617
import java.util.Set;
@@ -24,8 +25,6 @@
2425
import org.apache.maven.plugins.annotations.Mojo;
2526
import org.apache.maven.plugins.annotations.Parameter;
2627
import org.apache.maven.project.MavenProject;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
2928

3029
import io.jooby.run.JoobyRun;
3130
import io.jooby.run.JoobyRunOptions;
@@ -44,8 +43,6 @@
4443
@Execute(phase = PROCESS_CLASSES)
4544
public class RunMojo extends BaseMojo {
4645

47-
private static final Logger log = LoggerFactory.getLogger(RunMojo.class);
48-
4946
static {
5047
/* Turn off shutdown hook on Server. */
5148
System.setProperty("jooby.useShutdownHook", "false");
@@ -65,6 +62,13 @@ public class RunMojo extends BaseMojo {
6562
@Parameter(property = "jooby.compileExtensions")
6663
private List<String> compileExtensions;
6764

65+
/**
66+
* File extensions that force recompilation by touching the main Java source before building.
67+
* Extensions are specified without a leading dot.
68+
*/
69+
@Parameter(property = "jooby.recompileExtensions")
70+
private List<String> recompileExtensions;
71+
6872
/** Application port. */
6973
@Parameter(property = "jooby.port")
7074
private Integer port;
@@ -80,6 +84,13 @@ public class RunMojo extends BaseMojo {
8084
@Parameter(property = "jooby.useSingleClassLoader")
8185
private boolean useSingleClassLoader;
8286

87+
/**
88+
* Additional directories to watch for file changes. By default, only source directories and
89+
* resources are watched.
90+
*/
91+
@Parameter(property = "jooby.watchDirs")
92+
private List<String> watchDirs;
93+
8394
@Override
8495
protected void doExecute(List<MavenProject> projects, String mainClass) throws Throwable {
8596
Maven maven = getMaven();
@@ -90,9 +101,22 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws T
90101

91102
Runtime.getRuntime().addShutdownHook(new Thread(joobyRun::shutdown));
92103

104+
var mainJava = mainJava(projects);
93105
BiConsumer<String, Path> onFileChanged =
94106
(event, path) -> {
95-
if (options.isCompileExtension(path)) {
107+
if (options.isCompileExtension(path) || isRecompileExtension(path)) {
108+
if (isRecompileExtension(path)) {
109+
if (mainJava != null) {
110+
getLog().debug("Touching main class: " + mainJava);
111+
try {
112+
Files.setLastModifiedTime(
113+
mainJava,
114+
java.nio.file.attribute.FileTime.fromMillis(System.currentTimeMillis()));
115+
} catch (Exception x) {
116+
getLog().error("Failed to touch main class: " + mainJava, x);
117+
}
118+
}
119+
}
96120
getLog().debug("Restarting application on file change: " + path);
97121
joobyRun.restart(
98122
path,
@@ -120,20 +144,28 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws T
120144
}
121145
};
122146

123-
for (MavenProject project : projects) {
147+
for (var project : projects) {
124148
getLog().debug("Adding project: " + project.getArtifactId());
125149

126150
// main resources + conf, etc..
127151
resources(project, useTestScope)
128-
.forEach(
129-
file -> {
130-
joobyRun.addResource(file);
131-
joobyRun.addWatchDir(file, onFileChanged);
132-
});
152+
.forEach(file -> {
153+
joobyRun.addResource(file);
154+
joobyRun.addWatchDir(file, onFileChanged);
155+
});
133156

134157
// target/classes
135158
bin(project, useTestScope).forEach(joobyRun::addClasses);
136159

160+
if (watchDirs != null) {
161+
for (var watchDir : watchDirs) {
162+
Path path = project.getBasedir().toPath().resolve(watchDir);
163+
if (!joobyRun.addWatchDir(path, onFileChanged)) {
164+
getLog().warn("Watch directory does not exist: " + path);
165+
}
166+
}
167+
}
168+
137169
Set<Path> src = sourceDirectories(project, useTestScope);
138170
if (src.isEmpty()) {
139171
getLog().debug("Compiler is off in favor of Eclipse compiler.");
@@ -153,9 +185,13 @@ private JoobyRunOptions createOptions(String mainClass) {
153185
JoobyRunOptions options = new JoobyRunOptions();
154186
options.setBasedir(project.getBasedir().toPath());
155187
options.setMainClass(mainClass);
156-
if (compileExtensions != null) {
157-
options.setCompileExtensions(compileExtensions);
188+
var compileExt =
189+
new ArrayList<>(
190+
compileExtensions == null ? options.getCompileExtensions() : compileExtensions);
191+
if (recompileExtensions != null) {
192+
compileExt.addAll(recompileExtensions);
158193
}
194+
options.setCompileExtensions(compileExt);
159195
options.setPort(port);
160196
options.setWaitTimeBeforeRestart(waitTimeBeforeRestart);
161197
options.setProjectName(session.getCurrentProject().getArtifactId());
@@ -212,6 +248,30 @@ public void setUseSingleClassLoader(boolean useSingleClassLoader) {
212248
this.useSingleClassLoader = useSingleClassLoader;
213249
}
214250

251+
public List<String> getWatchDirs() {
252+
return watchDirs;
253+
}
254+
255+
public void setWatchDirs(List<String> watchDirs) {
256+
this.watchDirs = watchDirs;
257+
}
258+
259+
/**
260+
* @return File extensions that force recompilation by touching the main Java source before
261+
* building. Extensions are specified without a leading dot.
262+
*/
263+
public List<String> getRecompileExtensions() {
264+
return recompileExtensions;
265+
}
266+
267+
/**
268+
* @param recompileExtensions File extensions that force recompilation by touching the main Java
269+
* source before building. Extensions are specified without a leading dot.
270+
*/
271+
public void setRecompileExtensions(List<String> recompileExtensions) {
272+
this.recompileExtensions = recompileExtensions;
273+
}
274+
215275
protected void setUseTestScope(boolean useTestScope) {
216276
this.useTestScope = useTestScope;
217277
}
@@ -226,6 +286,18 @@ private MavenExecutionRequest mavenRequest(String goal) {
226286
return DefaultMavenExecutionRequest.copy(session.getRequest()).setGoals(List.of(goal));
227287
}
228288

289+
private Path mainJava(List<MavenProject> projects) {
290+
for (MavenProject mavenProject : projects) {
291+
var mainJava =
292+
Paths.get(mavenProject.getBuild().getSourceDirectory())
293+
.resolve(getMainClass().replace('.', '/') + ".java");
294+
if (Files.exists(mainJava)) {
295+
return mainJava;
296+
}
297+
}
298+
return null;
299+
}
300+
229301
private Set<Path> sourceDirectories(MavenProject project, boolean useTestScope) {
230302
Path eclipse = project.getBasedir().toPath().resolve(".classpath");
231303
if (Files.exists(eclipse)) {
@@ -239,6 +311,14 @@ private Set<Path> sourceDirectories(MavenProject project, boolean useTestScope)
239311
return Collections.singleton(sourceDir);
240312
}
241313

314+
private boolean isRecompileExtension(Path path) {
315+
if (recompileExtensions != null) {
316+
String filename = path.getFileName().toString();
317+
return recompileExtensions.stream().anyMatch(ext -> filename.endsWith("." + ext));
318+
}
319+
return false;
320+
}
321+
242322
@Override
243323
protected String mojoName() {
244324
return "run";

‎modules/jooby-run/src/main/java/io/jooby/run/JoobyRun.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public boolean addWatchDir(Path path, BiConsumer<String, Path> callback) {
309309
if (Files.exists(path)) {
310310
if (Files.isDirectory(path)) {
311311
watchDirs.put(path, callback);
312+
return true;
312313
}
313-
return true;
314314
}
315315
return false;
316316
}

0 commit comments

Comments
 (0)