1111import java .nio .file .Files ;
1212import java .nio .file .Path ;
1313import java .nio .file .Paths ;
14+ import java .util .ArrayList ;
1415import java .util .Collections ;
1516import java .util .List ;
1617import java .util .Set ;
2425import org .apache .maven .plugins .annotations .Mojo ;
2526import org .apache .maven .plugins .annotations .Parameter ;
2627import org .apache .maven .project .MavenProject ;
27- import org .slf4j .Logger ;
28- import org .slf4j .LoggerFactory ;
2928
3029import io .jooby .run .JoobyRun ;
3130import io .jooby .run .JoobyRunOptions ;
4443@ Execute (phase = PROCESS_CLASSES )
4544public 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" ;
0 commit comments