Skip to content

Commit a89e509

Browse files
committed
Reuse joobyRun options between plugins
1 parent b3ac7c6 commit a89e509

File tree

9 files changed

+137
-78
lines changed

9 files changed

+137
-78
lines changed

modules/jooby-gradle-plugin/src/main/java/io/jooby/gradle/JoobyPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.jooby.gradle;
1717

18+
import io.jooby.run.JoobyRunConf;
1819
import org.gradle.api.Plugin;
1920
import org.gradle.api.Project;
2021
import org.gradle.api.Task;
@@ -24,7 +25,7 @@
2425

2526
public class JoobyPlugin implements Plugin<Project> {
2627
@Override public void apply(Project project) {
27-
project.getExtensions().create("joobyRun", RunTaskConfig.class);
28+
project.getExtensions().create("joobyRun", JoobyRunConf.class);
2829

2930
Map<String, Object> options = new HashMap<>();
3031
options.put(Task.TASK_TYPE, RunTask.class);

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package io.jooby.gradle;
1717

18-
import io.jooby.run.HotSwap;
18+
import io.jooby.run.JoobyRun;
19+
import io.jooby.run.JoobyRunConf;
1920
import org.gradle.api.DefaultTask;
2021
import org.gradle.api.Project;
2122
import org.gradle.api.artifacts.Configuration;
@@ -54,18 +55,20 @@ public class RunTask extends DefaultTask {
5455
public void run() throws Throwable {
5556
try {
5657
Project current = getProject();
57-
RunTaskConfig config = current.getExtensions().getByType(RunTaskConfig.class);
58+
JoobyRunConf config = current.getExtensions().getByType(JoobyRunConf.class);
5859
List<Project> projects = Arrays.asList(current);
5960

60-
String mainClass = projects.stream()
61-
.filter(it -> it.getProperties().containsKey("mainClassName"))
62-
.map(it -> it.getProperties().get("mainClassName").toString())
63-
.findFirst()
64-
.orElseThrow(() -> new IllegalArgumentException(
65-
"Application class not found. Did you forget to set `mainClassName`?"));
66-
67-
HotSwap hotSwap = new HotSwap(current.getName(), mainClass);
68-
hotSwap.setPort(config.getPort());
61+
if (config.getMainClass() == null) {
62+
String mainClass = projects.stream()
63+
.filter(it -> it.getProperties().containsKey("mainClassName"))
64+
.map(it -> it.getProperties().get("mainClassName").toString())
65+
.findFirst()
66+
.orElseThrow(() -> new IllegalArgumentException(
67+
"Application class not found. Did you forget to set `mainClassName`?"));
68+
config.setMainClass(mainClass);
69+
}
70+
config.setProjectName(current.getName());
71+
JoobyRun joobyRun = new JoobyRun(config);
6972

7073
connection = GradleConnector.newConnector()
7174
.useInstallation(current.getGradle().getGradleHomeDir())
@@ -78,7 +81,7 @@ public void run() throws Throwable {
7881
.forTasks("classes");
7982

8083
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
81-
hotSwap.shutdown();
84+
joobyRun.shutdown();
8285
connection.close();
8386
}));
8487

@@ -87,7 +90,7 @@ public void run() throws Throwable {
8790
compiler.run(new ResultHandler<Void>() {
8891
@Override public void onComplete(Void result) {
8992
getLogger().debug("Restarting application on file change: " + path);
90-
hotSwap.restart();
93+
joobyRun.restart();
9194
}
9295

9396
@Override public void onFailure(GradleConnectionException failure) {
@@ -96,7 +99,7 @@ public void run() throws Throwable {
9699
});
97100
} else if (config.isRestartExtension(path)) {
98101
getLogger().debug("Restarting application on file change: " + path);
99-
hotSwap.restart();
102+
joobyRun.restart();
100103
} else {
101104
getLogger().debug("Ignoring file change: " + path);
102105
}
@@ -109,28 +112,28 @@ public void run() throws Throwable {
109112
// main/resources
110113
sourceSet.getResources().getSrcDirs().stream()
111114
.map(File::toPath)
112-
.forEach(file -> hotSwap.addResource(file, onFileChanged));
115+
.forEach(file -> joobyRun.addResource(file, onFileChanged));
113116
// conf directory
114117
Path conf = project.getProjectDir().toPath().resolve("conf");
115-
hotSwap.addResource(conf, onFileChanged);
118+
joobyRun.addResource(conf, onFileChanged);
116119

117120
// build classes
118-
binDirectories(project, sourceSet).forEach(hotSwap::addResource);
121+
binDirectories(project, sourceSet).forEach(joobyRun::addResource);
119122

120123
Set<Path> src = sourceDirectories(project, sourceSet);
121124
if (src.isEmpty()) {
122125
getLogger().debug("Compiler is off in favor of Eclipse compiler.");
123126
binDirectories(project, sourceSet)
124-
.forEach(path -> hotSwap.addResource(path, onFileChanged));
127+
.forEach(path -> joobyRun.addResource(path, onFileChanged));
125128
} else {
126-
src.forEach(path -> hotSwap.addResource(path, onFileChanged));
129+
src.forEach(path -> joobyRun.addResource(path, onFileChanged));
127130
}
128131

129-
dependencies(project, sourceSet).forEach(hotSwap::addResource);
132+
dependencies(project, sourceSet).forEach(joobyRun::addResource);
130133
}
131134

132135
// Block current thread.
133-
hotSwap.start();
136+
joobyRun.start();
134137
} catch (InvocationTargetException x) {
135138
throw x.getCause();
136139
}

modules/jooby-maven-plugin/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@
5151
<groupId>org.codehaus.plexus</groupId>
5252
<artifactId>plexus-utils</artifactId>
5353
</dependency>
54+
55+
<!-- Test dependencies -->
56+
<dependency>
57+
<groupId>org.junit.jupiter</groupId>
58+
<artifactId>junit-jupiter-engine</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.jacoco</groupId>
64+
<artifactId>org.jacoco.agent</artifactId>
65+
<classifier>runtime</classifier>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.mockito</groupId>
71+
<artifactId>mockito-core</artifactId>
72+
<scope>test</scope>
73+
</dependency>
5474
</dependencies>
5575

5676
<build>

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package io.jooby.maven;
1717

1818
import edu.emory.mathcs.backport.java.util.Collections;
19-
import io.jooby.run.HotSwap;
19+
import io.jooby.run.JoobyRun;
20+
import io.jooby.run.JoobyRunConf;
2021
import org.apache.maven.Maven;
2122
import org.apache.maven.execution.DefaultMavenExecutionRequest;
2223
import org.apache.maven.execution.MavenExecutionRequest;
@@ -42,7 +43,6 @@
4243
import org.eclipse.aether.graph.Dependency;
4344

4445
import java.io.File;
45-
import java.lang.reflect.InvocationTargetException;
4646
import java.nio.file.Files;
4747
import java.nio.file.Path;
4848
import java.nio.file.Paths;
@@ -97,10 +97,9 @@ public class RunMojo extends AbstractMojo {
9797
}
9898
getLog().debug("Found `" + APP_CLASS + "`: " + mainClass);
9999

100-
HotSwap hotSwap = new HotSwap(session.getCurrentProject().getArtifactId(), mainClass);
101-
hotSwap.setPort(port);
100+
JoobyRun joobyRun = new JoobyRun(createOptions());
102101

103-
Runtime.getRuntime().addShutdownHook(new Thread(hotSwap::shutdown));
102+
Runtime.getRuntime().addShutdownHook(new Thread(joobyRun::shutdown));
104103

105104
BiConsumer<String, Path> onFileChanged = (event, path) -> {
106105
if (isCompileExtension(path)) {
@@ -110,11 +109,11 @@ public class RunMojo extends AbstractMojo {
110109
getLog().debug("Compilation error found: " + path);
111110
} else {
112111
getLog().debug("Restarting application on file change: " + path);
113-
hotSwap.restart();
112+
joobyRun.restart();
114113
}
115114
} else if (isRestartExtension(path)) {
116115
getLog().debug("Restarting application on file change: " + path);
117-
hotSwap.restart();
116+
joobyRun.restart();
118117
} else {
119118
getLog().debug("Ignoring file change: " + path);
120119
}
@@ -128,34 +127,44 @@ public class RunMojo extends AbstractMojo {
128127
resourceList.stream()
129128
.map(Resource::getDirectory)
130129
.map(Paths::get)
131-
.forEach(file -> hotSwap.addResource(file, onFileChanged));
130+
.forEach(file -> joobyRun.addResource(file, onFileChanged));
132131
// conf directory
133132
Path conf = project.getBasedir().toPath().resolve("conf");
134-
hotSwap.addResource(conf, onFileChanged);
133+
joobyRun.addResource(conf, onFileChanged);
135134

136135
// target/classes
137-
hotSwap.addResource(Paths.get(project.getBuild().getOutputDirectory()));
136+
joobyRun.addResource(Paths.get(project.getBuild().getOutputDirectory()));
138137

139138
Set<Path> src = sourceDirectories(project);
140139
if (src.isEmpty()) {
141140
getLog().debug("Compiler is off in favor of Eclipse compiler.");
142-
binDirectories(project).forEach(path -> hotSwap.addResource(path, onFileChanged));
141+
binDirectories(project).forEach(path -> joobyRun.addResource(path, onFileChanged));
143142
} else {
144-
src.forEach(path -> hotSwap.addResource(path, onFileChanged));
143+
src.forEach(path -> joobyRun.addResource(path, onFileChanged));
145144
}
146145

147-
artifacts(project).forEach(hotSwap::addResource);
146+
artifacts(project).forEach(joobyRun::addResource);
148147
}
149148

150149
// Block current thread.
151-
hotSwap.start();
150+
joobyRun.start();
152151
} catch (MojoExecutionException | MojoFailureException x) {
153152
throw x;
154153
} catch (Exception x) {
155154
throw new MojoFailureException("jooby-run resulted in exception", x);
156155
}
157156
}
158157

158+
private JoobyRunConf createOptions() {
159+
JoobyRunConf options = new JoobyRunConf();
160+
options.setMainClass(mainClass);
161+
options.setCompileExtensions(compileExtensions);
162+
options.setPort(port);
163+
options.setProjectName(session.getCurrentProject().getArtifactId());
164+
options.setRestartExtensions(restartExtensions);
165+
return options;
166+
}
167+
159168
public List<String> getCompileExtensions() {
160169
return compileExtensions;
161170
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.jooby.maven;
2+
3+
import io.jooby.run.JoobyRunConf;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.lang.reflect.Field;
7+
import java.util.stream.Stream;
8+
9+
import static junit.framework.Assert.assertEquals;
10+
11+
public class RunMojoTest {
12+
13+
@Test
14+
public void ensureConfigurationOptions() {
15+
Stream.of(JoobyRunConf.class.getDeclaredFields())
16+
.filter(field -> !field.getName().equals("projectName"))
17+
.forEach(field -> {
18+
try {
19+
Field target = RunMojo.class.getDeclaredField(field.getName());
20+
assertEquals(field.getGenericType(), target.getGenericType());
21+
} catch (Exception x) {
22+
throw new IllegalStateException(x);
23+
}
24+
});
25+
}
26+
}

modules/jooby-run/src/main/java/io/jooby/run/HotSwap.java renamed to modules/jooby-run/src/main/java/io/jooby/run/JoobyRun.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.Set;
3737
import java.util.function.BiConsumer;
3838

39-
public class HotSwap {
39+
public class JoobyRun {
4040

4141
private static class ExtModuleLoader extends ModuleLoader {
4242

@@ -52,34 +52,32 @@ public void unload(String name, final Module module) {
5252
private static class AppModule {
5353
private final Logger logger;
5454
private final ExtModuleLoader loader;
55-
private final String projectName;
56-
private String mainClass;
55+
private final JoobyRunConf conf;
5756
private Module module;
5857
private ClassLoader contextClassLoader;
59-
private int port;
6058

61-
public AppModule(Logger logger, ExtModuleLoader loader, String projectName, String mainClass,
62-
int port, ClassLoader contextClassLoader) {
59+
public AppModule(Logger logger, ExtModuleLoader loader, ClassLoader contextClassLoader,
60+
JoobyRunConf conf) {
6361
this.logger = logger;
6462
this.loader = loader;
65-
this.projectName = projectName;
66-
this.mainClass = mainClass;
67-
this.port = port;
63+
this.conf = conf;
6864
this.contextClassLoader = contextClassLoader;
6965
}
7066

7167
public void start() {
7268
try {
7369
System.setProperty("___jooby_run_hook__", ServerRef.class.getName());
7470

75-
module = loader.loadModule(projectName);
71+
module = loader.loadModule(conf.getProjectName());
7672
ModuleClassLoader classLoader = module.getClassLoader();
7773
Thread.currentThread().setContextClassLoader(classLoader);
7874

79-
module.run(mainClass, new String[]{"server.port=" + port, "server.join=false"});
75+
module.run(conf.getMainClass(),
76+
new String[]{"server.port=" + conf.getPort(), "server.join=false"});
8077

8178
} catch (Exception x) {
82-
logger.error("execution of {} resulted in exception", mainClass, withoutReflection(x));
79+
logger.error("execution of {} resulted in exception", conf.getMainClass(),
80+
withoutReflection(x));
8381
} finally {
8482
Thread.currentThread().setContextClassLoader(contextClassLoader);
8583
}
@@ -105,7 +103,7 @@ private Throwable withoutReflection(Throwable cause) {
105103
private void unloadModule() {
106104
try {
107105
if (module != null) {
108-
loader.unload(projectName, module);
106+
loader.unload(conf.getProjectName(), module);
109107
}
110108
} catch (Exception x) {
111109
logger.debug("unload module resulted in exception", x);
@@ -125,9 +123,7 @@ private void closeServer() {
125123

126124
private final Logger logger = LoggerFactory.getLogger(getClass());
127125

128-
private final String projectName;
129-
130-
private final String mainClass;
126+
private final JoobyRunConf conf;
131127

132128
private final Set<Path> resources = new LinkedHashSet<>();
133129

@@ -139,11 +135,8 @@ private void closeServer() {
139135

140136
private AppModule module;
141137

142-
private int port = 8080;
143-
144-
public HotSwap(String projectName, String mainClass) {
145-
this.projectName = projectName;
146-
this.mainClass = mainClass;
138+
public JoobyRun(JoobyRunConf conf) {
139+
this.conf = conf;
147140
}
148141

149142
public boolean addResource(Path path, BiConsumer<String, Path> callback) {
@@ -173,26 +166,18 @@ public void start() throws Exception {
173166
try {
174167
logger.debug("project: {}", toString());
175168

176-
ModuleFinder[] finders = {new FlattenClasspath(projectName, resources, dependencies)};
169+
ModuleFinder[] finders = {
170+
new FlattenClasspath(conf.getProjectName(), resources, dependencies)};
177171

178172
ExtModuleLoader loader = new ExtModuleLoader(finders);
179-
module = new AppModule(logger, loader, projectName, mainClass, port,
180-
Thread.currentThread().getContextClassLoader());
173+
module = new AppModule(logger, loader, Thread.currentThread().getContextClassLoader(), conf);
181174
module.start();
182175
watcher.watch();
183176
} catch (ClosedWatchServiceException expected) {
184177
logger.trace("Watcher.close resulted in exception", expected);
185178
}
186179
}
187180

188-
public int getPort() {
189-
return port;
190-
}
191-
192-
public void setPort(int port) {
193-
this.port = port;
194-
}
195-
196181
public void restart() {
197182
module.restart();
198183
}
@@ -225,7 +210,7 @@ private DirectoryWatcher newWatcher() throws IOException {
225210

226211
@Override public String toString() {
227212
StringBuilder buff = new StringBuilder();
228-
buff.append(projectName).append("\n");
213+
buff.append(conf.getProjectName()).append("\n");
229214
buff.append(" watch-dirs: ").append("\n");
230215
watchDirs.forEach(
231216
(path, callback) -> buff.append(" ").append(path.toAbsolutePath()).append("\n"));

0 commit comments

Comments
 (0)