Skip to content

Commit 9008c97

Browse files
committed
Fix resource change
1 parent d4d92b4 commit 9008c97

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 2.7.2 (2018-10-18)
2+
3+
* [fix] In watch goal, resources (including static resources under `META-INF/resources`) are now properly updated on change.
4+
15
# Version 2.7.1 (2018-05-18)
26

37
* [new] In watch goal, trigger a LiveReload (without app refresh) when a resource changes.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>org.seedstack</groupId>
2121
<artifactId>seedstack-maven-plugin</artifactId>
22-
<version>2.7.1-SNAPSHOT</version>
22+
<version>2.7.2-SNAPSHOT</version>
2323
<packaging>maven-plugin</packaging>
2424

2525
<properties>

src/license/THIRD-PARTY.properties

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
2-
#-------------------------------------------------------------------------------
3-
# Already used licenses in project :
4-
# - Apache 2
5-
# - BSD
6-
# - CDDL 1.0
7-
# - CPL 1.0
8-
# - EPL 1.0
9-
# - MIT
10-
# - MPL
11-
# - Public Domain
12-
#-------------------------------------------------------------------------------
13-
# Please fill the missing licenses for dependencies :
14-
#
15-
#
16-
#Fri May 18 14:52:30 CEST 2018
17-
classworlds--classworlds--1.1-alpha-2=
18-
dom4j--dom4j--1.6.1=
19-
jdom--jdom--1.0=
20-
org.codehaus.plexus--plexus-component-api--1.0-alpha-16=
21-
org.codehaus.plexus--plexus-container-default--1.0-alpha-9-stable-1=
22-
org.codehaus.plexus--plexus-interactivity-api--1.0-alpha-6=
23-
oro--oro--2.0.8=
1+
# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
2+
#-------------------------------------------------------------------------------
3+
# Already used licenses in project :
4+
# - Apache 2
5+
# - BSD
6+
# - CDDL 1.0
7+
# - CPL 1.0
8+
# - EPL 1.0
9+
# - MIT
10+
# - MPL
11+
# - Public Domain
12+
#-------------------------------------------------------------------------------
13+
# Please fill the missing licenses for dependencies :
14+
#
15+
#
16+
#Wed Oct 17 15:28:47 CEST 2018
17+
classworlds--classworlds--1.1-alpha-2=
18+
org.codehaus.plexus--plexus-container-default--1.0-alpha-9-stable-1=
19+
jdom--jdom--1.0=
20+
org.codehaus.plexus--plexus-component-api--1.0-alpha-16=
21+
org.codehaus.plexus--plexus-interactivity-api--1.0-alpha-6=
22+
oro--oro--2.0.8=
23+
dom4j--dom4j--1.6.1=

src/main/java/org/seedstack/maven/WatchMojo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void execute() throws MojoExecutionException {
5757

5858
setupResourceWatcher(mavenProject);
5959

60+
// Force config watching
6061
System.setProperty("seedstack.config.config.watch", "true");
6162

6263
this.appRunnable = new AppRunnable(getContext());
@@ -113,7 +114,7 @@ private void stopWatcher(DirectoryWatcher watcher, Thread watcherThread) {
113114

114115
private void setupResourceWatcher(MavenProject mavenProject) throws MojoExecutionException {
115116
try {
116-
this.resourceWatcher = new DirectoryWatcher(getLog(), new ResourceChangeListener(this));
117+
this.resourceWatcher = new DirectoryWatcher(getLog(), new ResourceChangeListener(this, getContext()));
117118
} catch (IOException e) {
118119
throw new MojoExecutionException("Unable to create resource watcher", e);
119120
}

src/main/java/org/seedstack/maven/watcher/ResourceChangeListener.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,34 @@
88

99
package 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+
1120
import java.io.File;
1221
import java.util.Set;
22+
import org.apache.maven.plugin.MojoExecutionException;
23+
import org.seedstack.maven.Context;
1324
import org.seedstack.maven.WatchMojo;
1425

1526
public 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
}

src/main/java/org/seedstack/maven/watcher/SourceChangeListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private void refresh(Set<FileEvent> fileEvents) {
114114
if (toLog == null || !toLog.getClass().getName().equals(COMPILATION_FAILURE_EXCEPTION)) {
115115
toLog = e;
116116
}
117-
watchMojo.getLog().warn("An error occurred during application refresh, ignoring changes", toLog);
117+
watchMojo.getLog().warn("An error occurred during application refresh, ignoring source changes", toLog);
118118
}
119119
}
120120

0 commit comments

Comments
 (0)