88
99package org .seedstack .maven .watcher ;
1010
11+ import static org .twdata .maven .mojoexecutor .MojoExecutor .artifactId ;
12+ import static org .twdata .maven .mojoexecutor .MojoExecutor .configuration ;
13+ import static org .twdata .maven .mojoexecutor .MojoExecutor .executeMojo ;
14+ import static org .twdata .maven .mojoexecutor .MojoExecutor .executionEnvironment ;
15+ import static org .twdata .maven .mojoexecutor .MojoExecutor .goal ;
16+ import static org .twdata .maven .mojoexecutor .MojoExecutor .groupId ;
17+ import static org .twdata .maven .mojoexecutor .MojoExecutor .plugin ;
18+ import static org .twdata .maven .mojoexecutor .MojoExecutor .version ;
19+
1120import java .io .File ;
1221import java .util .Set ;
22+ import org .apache .maven .plugin .MojoExecutionException ;
23+ import org .seedstack .maven .Context ;
1324import org .seedstack .maven .WatchMojo ;
1425
1526public class ResourceChangeListener implements FileChangeListener {
16- private WatchMojo watchMojo ;
27+ private final WatchMojo watchMojo ;
28+ private final Context context ;
1729
18- public ResourceChangeListener (WatchMojo watchMojo ) {
30+ public ResourceChangeListener (WatchMojo watchMojo , Context context ) {
1931 this .watchMojo = watchMojo ;
32+ this .context = context ;
2033 }
2134
2235 @ Override
2336 public void onChange (Set <FileEvent > fileEvents ) {
2437 boolean configChanged = false ;
38+ boolean staticResourceChanged = false ;
2539 watchMojo .getLog ().info ("Resource change(s) detected" );
2640
2741 for (FileEvent fileEvent : fileEvents ) {
@@ -38,18 +52,24 @@ public void onChange(Set<FileEvent> fileEvents) {
3852 }
3953 }
4054
41- if (configChanged ) {
42- // Wait for the application to notice the change
43- watchMojo .getLog ()
44- .info ("A configuration file has changed, waiting for the application to notice the change" );
45- try {
46- Thread .sleep (2000 );
47- } catch (InterruptedException e ) {
48- // ignore
55+ try {
56+ copyResources ();
57+
58+ if (configChanged ) {
59+ // Wait for the application to notice the change
60+ watchMojo .getLog ()
61+ .info ("A configuration file has changed, waiting for the application to notice it" );
62+ try {
63+ Thread .sleep (2000 );
64+ } catch (InterruptedException e ) {
65+ // ignore
66+ }
4967 }
50- }
5168
52- watchMojo .liveReload ();
69+ watchMojo .liveReload ();
70+ } catch (MojoExecutionException e ) {
71+ watchMojo .getLog ().warn ("An error occurred during resource copy, ignoring resource changes" , e );
72+ }
5373 }
5474
5575 private boolean isConfigFile (File file ) {
@@ -62,4 +82,15 @@ private boolean isConfigFile(File file) {
6282 || fileName .equals ("application.override.properties" )
6383 || file .getParentFile ().getPath ().endsWith ("META-INF" + File .separator + "configuration" );
6484 }
85+
86+ private void copyResources () throws MojoExecutionException {
87+ executeMojo (
88+ plugin (groupId ("org.apache.maven.plugins" ), artifactId ("maven-resources-plugin" ), version ("3.1.0" )),
89+ goal ("resources" ),
90+ configuration (),
91+ executionEnvironment (context .getMavenProject (),
92+ context .getMavenSession (),
93+ context .getBuildPluginManager ())
94+ );
95+ }
6596}
0 commit comments