Skip to content

Commit b88a206

Browse files
sphoganjknack
authored andcommitted
Making the src extensions that trigger rebuild configurable (jooby-project#859)
* making the src extensions that trigger rebuild configurable * add documentation and fix XML Parsing on Kotlin recompilation
1 parent 188181a commit b88a206

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

doc/doc/maven-plugin/maven.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ It's worth to mention that dynamic reload of classes is done via {{jboss-modules
5656
</includes>
5757
<excludes>
5858
</excludes>
59+
<srcExtensions>.java,.kt,.conf,.properties</srcExtensions>
5960
</configuration>
6061
</plugin>
6162
```
@@ -150,4 +151,8 @@ Make sure to enable the ```fork``` option too, otherwise ```vmArgs``` are ignore
150151

151152
### includes / excludes
152153

153-
List of file patterns to listen for file changes.
154+
List of file patterns to listen for file changes and trigger restarting the server.
155+
156+
### srcExtensions
157+
158+
List of file extensions to listen for file changes and trigger re-compilation.

doc/gradle-jooby-run.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ joobyRun {
6262
includes = ['**/*.class', '**/*.conf', '**/*.properties']
6363
excludes = []
6464
logLevel = 'info'
65+
srcExtensions = [".java", ".kt", ".conf", ".properties"]
6566
}
6667

6768
```
@@ -86,4 +87,8 @@ Compilation success or error messages are displayed in the console (not in the b
8687

8788
### includes / excludes
8889

89-
List of file patterns to watch for file changes.
90+
List of file patterns to watch for file changes and trigger restarting the server.
91+
92+
### srcExtensions
93+
94+
List of file extensions watch for changes and trigger re-compilation.

modules/jooby-gradle-plugin/src/main/java/org/jooby/run/JoobyPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ private void configureJoobyRun(final Project project) throws IOException {
250250

251251
mapping.map("mainClassName", () -> project.getProperties().get("mainClassName"));
252252

253+
mapping.map("srcExtensions", () -> new String[] {".java", ".conf", ".properties"});
254+
253255
mapping.map("compiler", () -> {
254256
File eclipseClasspath = new File(project.getProjectDir(), ".classpath");
255257
return eclipseClasspath.exists() ? "off" : "on";

modules/jooby-gradle-plugin/src/main/java/org/jooby/run/JoobyTask.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public class JoobyTask extends ConventionTask {
248248

249249
private List<String> watchDirs;
250250

251+
private List<String> srcExtensions;
252+
251253
private String mainClassName;
252254

253255
private String compiler;
@@ -284,10 +286,8 @@ public void run() throws Exception {
284286
.toArray(new Path[0]);
285287
// don't start watcher if continuous is ON
286288
new Watcher((k, path) -> {
287-
if (path.toString().endsWith(".java")) {
288-
runTask(project, path, "classes");
289-
} else if (path.toString().endsWith(".conf")
290-
|| path.toString().endsWith(".properties")) {
289+
if (getSrcExtensions().stream()
290+
.anyMatch(ext -> path.toString().endsWith(ext))) {
291291
runTask(project, path, "classes");
292292
}
293293
}, watchDirs).start();
@@ -409,11 +409,20 @@ public void setWatchDirs(final List<String> watchDirs) {
409409
this.watchDirs = watchDirs;
410410
}
411411

412+
public List<String> getSrcExtensions() {
413+
return srcExtensions;
414+
}
415+
416+
public void setSrcExtensions(final List<String> srcExtensions) {
417+
this.srcExtensions = srcExtensions;
418+
}
419+
412420
public String getCompiler() {
413421
return compiler;
414422
}
415423

416424
public void setCompiler(final String compiler) {
417425
this.compiler = compiler;
418426
}
427+
419428
}

modules/jooby-maven-plugin/src/main/java/org/jooby/JoobyMojo.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,25 @@
224224

225225
import java.io.File;
226226
import java.nio.file.Path;
227-
import java.util.ArrayList;
228-
import java.util.Arrays;
229-
import java.util.Collections;
230-
import java.util.LinkedHashSet;
231-
import java.util.List;
232-
import java.util.Optional;
233-
import java.util.Set;
227+
import java.util.*;
234228
import java.util.function.Consumer;
235229
import java.util.stream.Collectors;
236230

237231
@Mojo(name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
238232
@Execute(phase = LifecyclePhase.TEST_COMPILE)
239233
public class JoobyMojo extends AbstractMojo {
234+
private static final Object LOCK = new Object();
235+
236+
private static final List<String> XML_PROPS = Arrays.asList(
237+
"javax.xml.parsers.DocumentBuilderFactory",
238+
"javax.xml.parsers.SAXParserFactory",
239+
"javax.xml.stream.XMLInputFactory",
240+
"javax.xml.stream.XMLEventFactory",
241+
"javax.xml.transform.TransformerFactory",
242+
"javax.xml.stream.XMLOutputFactory",
243+
"javax.xml.datatype.DatatypeFactory",
244+
"org.xml.sax.driver");
245+
240246

241247
private static class ShutdownHook extends Thread {
242248
private Log log;
@@ -291,6 +297,9 @@ public void run() {
291297
@Parameter(property = "jooby.excludes")
292298
private List<String> excludes;
293299

300+
@Parameter(property = "jooby.srcExtensions", defaultValue = ".java,.kt,.conf,.properties")
301+
private List<String> srcExtensions;
302+
294303
@Parameter(property = "application.debug", defaultValue = "true")
295304
private String debug;
296305

@@ -388,7 +397,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
388397
getLog().debug("cmd: " + cmd.debug());
389398
}
390399

391-
Watcher watcher = setupCompiler(mavenProject, compiler, goal -> {
400+
Watcher watcher = setupCompiler(mavenProject, compiler, srcExtensions, goal -> {
392401
maven.execute(DefaultMavenExecutionRequest.copy(session.getRequest())
393402
.setGoals(Arrays.asList(goal)));
394403
});
@@ -455,7 +464,7 @@ private Set<File> refcp(final Set<File> files) {
455464

456465
@SuppressWarnings("unchecked")
457466
private static Watcher setupCompiler(final MavenProject project, final String compiler,
458-
final Consumer<String> task) throws MojoFailureException {
467+
final List<String> srcExtensions, final Consumer<String> task) throws MojoFailureException {
459468
File eclipseClasspath = new File(project.getBasedir(), ".classpath");
460469
if ("off".equalsIgnoreCase(compiler) || eclipseClasspath.exists()) {
461470
return null;
@@ -472,11 +481,9 @@ private static Watcher setupCompiler(final MavenProject project, final String co
472481
ClassLoader currentloader = Thread.currentThread().getContextClassLoader();
473482
try {
474483
Thread.currentThread().setContextClassLoader(backloader);
475-
if (path.toString().endsWith(".java")) {
476-
task.accept("compile");
477-
} else if (path.toString().endsWith(".conf")
478-
|| path.toString().endsWith(".properties")) {
479-
task.accept("compile");
484+
if (srcExtensions.stream()
485+
.anyMatch(ext -> path.toString().endsWith(ext))) {
486+
runCompile(task);
480487
}
481488
} finally {
482489
Thread.currentThread().setContextClassLoader(currentloader);
@@ -487,6 +494,19 @@ private static Watcher setupCompiler(final MavenProject project, final String co
487494
}
488495
}
489496

497+
private static void runCompile(Consumer<String> task) {
498+
synchronized (LOCK) {
499+
Map<String, String> xml = new HashMap<>();
500+
XML_PROPS.forEach(p -> xml.put(p, (String) System.getProperties().remove(p)));
501+
task.accept("compile");
502+
xml.forEach((k, v) -> {
503+
if (v != null) {
504+
System.setProperty(k, v);
505+
}
506+
});
507+
}
508+
}
509+
490510
private void setLogback() {
491511
// logback
492512
File[] logbackFiles = {localFile("conf", "logback-test.xml"),

0 commit comments

Comments
 (0)