Skip to content

Commit 02b573c

Browse files
committed
rename maven and gradle plugin + support server.port
1 parent 36f7ef8 commit 02b573c

File tree

5 files changed

+111
-67
lines changed

5 files changed

+111
-67
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
public class JoobyPlugin implements Plugin<Project> {
2626
@Override public void apply(Project project) {
27+
project.getExtensions().create("joobyRun", RunTaskConfig.class);
28+
2729
Map<String, Object> options = new HashMap<>();
28-
options.put(Task.TASK_TYPE, JoobyRun.class);
30+
options.put(Task.TASK_TYPE, RunTask.class);
2931
options.put(Task.TASK_DEPENDS_ON, "classes");
3032
options.put(Task.TASK_NAME, "joobyRun");
3133
options.put(Task.TASK_DESCRIPTION, "Run, debug and hot reload applications");

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

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package io.jooby.gradle;
1717

1818
import io.jooby.run.HotSwap;
19-
import org.gradle.StartParameter;
2019
import org.gradle.api.DefaultTask;
2120
import org.gradle.api.Project;
22-
import org.gradle.api.Task;
2321
import org.gradle.api.artifacts.Configuration;
2422
import org.gradle.api.plugins.JavaPluginConvention;
2523
import org.gradle.api.tasks.SourceSet;
@@ -44,24 +42,19 @@
4442
import java.util.function.Predicate;
4543
import java.util.stream.Collectors;
4644

47-
public class JoobyRun extends DefaultTask {
45+
public class RunTask extends DefaultTask {
4846

4947
static {
5048
System.setProperty("jooby.useShutdownHook", "false");
5149
}
5250

53-
private String executionMode = "DEFAULT";
54-
55-
private List<String> restartExtensions = Arrays.asList("conf", "properties", "class");
56-
57-
private List<String> compileExtensions = Arrays.asList("java", "kt");
58-
5951
private ProjectConnection connection;
6052

6153
@TaskAction
6254
public void run() throws Throwable {
6355
try {
6456
Project current = getProject();
57+
RunTaskConfig config = current.getExtensions().getByType(RunTaskConfig.class);
6558
List<Project> projects = Arrays.asList(current);
6659

6760
String mainClass = projects.stream()
@@ -71,7 +64,8 @@ public void run() throws Throwable {
7164
.orElseThrow(() -> new IllegalArgumentException(
7265
"Application class not found. Did you forget to set `mainClassName`?"));
7366

74-
HotSwap hotSwap = new HotSwap(current.getName(), mainClass, executionMode);
67+
HotSwap hotSwap = new HotSwap(current.getName(), mainClass, config.getExecutionMode());
68+
hotSwap.setPort(config.getPort());
7569

7670
connection = GradleConnector.newConnector()
7771
.useInstallation(current.getGradle().getGradleHomeDir())
@@ -89,7 +83,7 @@ public void run() throws Throwable {
8983
}));
9084

9185
BiConsumer<String, Path> onFileChanged = (event, path) -> {
92-
if (isCompileExtension(path)) {
86+
if (config.isCompileExtension(path)) {
9387
compiler.run(new ResultHandler<Void>() {
9488
@Override public void onComplete(Void result) {
9589
getLogger().debug("Restarting application on file change: " + path);
@@ -100,7 +94,7 @@ public void run() throws Throwable {
10094
getLogger().debug("Compilation error found: " + path);
10195
}
10296
});
103-
} else if (isRestartExtension(path)) {
97+
} else if (config.isRestartExtension(path)) {
10498
getLogger().debug("Restarting application on file change: " + path);
10599
hotSwap.restart();
106100
} else {
@@ -187,41 +181,4 @@ private SourceSet sourceSet(final Project project) {
187181
public JavaPluginConvention getJavaConvention(final Project project) {
188182
return project.getConvention().getPlugin(JavaPluginConvention.class);
189183
}
190-
191-
private boolean isCompileExtension(Path path) {
192-
return containsExtension(compileExtensions, path);
193-
}
194-
195-
private boolean isRestartExtension(Path path) {
196-
return containsExtension(restartExtensions, path);
197-
}
198-
199-
private boolean containsExtension(List<String> extensions, Path path) {
200-
String filename = path.getFileName().toString();
201-
return extensions.stream().anyMatch(ext -> filename.endsWith("." + ext));
202-
}
203-
204-
public String getExecutionMode() {
205-
return executionMode;
206-
}
207-
208-
public void setExecutionMode(String executionMode) {
209-
this.executionMode = executionMode;
210-
}
211-
212-
public List<String> getRestartExtensions() {
213-
return restartExtensions;
214-
}
215-
216-
public void setRestartExtensions(List<String> restartExtensions) {
217-
this.restartExtensions = restartExtensions;
218-
}
219-
220-
public List<String> getCompileExtensions() {
221-
return compileExtensions;
222-
}
223-
224-
public void setCompileExtensions(List<String> compileExtensions) {
225-
this.compileExtensions = compileExtensions;
226-
}
227184
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
16+
package io.jooby.gradle;
17+
18+
import java.nio.file.Path;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
public class RunTaskConfig {
23+
24+
private String executionMode = "DEFAULT";
25+
26+
private List<String> restartExtensions = Arrays.asList("conf", "properties", "class");
27+
28+
private List<String> compileExtensions = Arrays.asList("java", "kt");
29+
30+
private int port = 8080;
31+
32+
public int getPort() {
33+
return port;
34+
}
35+
36+
public void setPort(int port) {
37+
this.port = port;
38+
}
39+
40+
public String getExecutionMode() {
41+
return executionMode;
42+
}
43+
44+
public void setExecutionMode(String executionMode) {
45+
this.executionMode = executionMode;
46+
}
47+
48+
public List<String> getRestartExtensions() {
49+
return restartExtensions;
50+
}
51+
52+
public void setRestartExtensions(List<String> restartExtensions) {
53+
this.restartExtensions = restartExtensions;
54+
}
55+
56+
public List<String> getCompileExtensions() {
57+
return compileExtensions;
58+
}
59+
60+
public void setCompileExtensions(List<String> compileExtensions) {
61+
this.compileExtensions = compileExtensions;
62+
}
63+
64+
public boolean isCompileExtension(Path path) {
65+
return containsExtension(compileExtensions, path);
66+
}
67+
68+
public boolean isRestartExtension(Path path) {
69+
return containsExtension(restartExtensions, path);
70+
}
71+
72+
private boolean containsExtension(List<String> extensions, Path path) {
73+
String filename = path.getFileName().toString();
74+
return extensions.stream().anyMatch(ext -> filename.endsWith("." + ext));
75+
}
76+
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
@Mojo(name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
5656
@Execute(phase = LifecyclePhase.TEST)
57-
public class JoobyRun extends AbstractMojo {
57+
public class RunMojo extends AbstractMojo {
5858

5959
static {
6060
System.setProperty("jooby.useShutdownHook", "false");
@@ -66,14 +66,17 @@ public class JoobyRun extends AbstractMojo {
6666
private String mainClass;
6767

6868
@Parameter(defaultValue = "${application.mode}")
69-
private String executionMode;
69+
private String executionMode = "DEFAULT";
7070

7171
@Parameter(defaultValue = "conf,properties,class")
7272
private List<String> restartExtensions;
7373

7474
@Parameter(defaultValue = "java,kt")
7575
private List<String> compileExtensions;
7676

77+
@Parameter(defaultValue = "${server.port}")
78+
private int port = 8080;
79+
7780
@Parameter(defaultValue = "${session}", required = true, readonly = true)
7881
private MavenSession session;
7982

@@ -95,13 +98,11 @@ public class JoobyRun extends AbstractMojo {
9598
.orElseThrow(() -> new MojoExecutionException(
9699
"Application class not found. Did you forget to set `application.class`?"));
97100
}
98-
if (executionMode == null) {
99-
executionMode = "DEFAULT";
100-
}
101101
getLog().debug("Found `" + APP_CLASS + "`: " + mainClass);
102102

103103
HotSwap hotSwap = new HotSwap(session.getCurrentProject().getArtifactId(), mainClass,
104104
executionMode);
105+
hotSwap.setPort(port);
105106

106107
Runtime.getRuntime().addShutdownHook(new Thread(hotSwap::shutdown));
107108

@@ -155,13 +156,7 @@ public class JoobyRun extends AbstractMojo {
155156
} catch (MojoExecutionException | MojoFailureException x) {
156157
throw x;
157158
} catch (Exception x) {
158-
Throwable cause;
159-
if (x instanceof InvocationTargetException) {
160-
cause = x.getCause();
161-
} else {
162-
cause = x;
163-
}
164-
throw new MojoFailureException("jooby-run resulted in exception", cause);
159+
throw new MojoFailureException("jooby-run resulted in exception", x);
165160
}
166161
}
167162

modules/jooby-run/src/main/java/io/jooby/run/HotSwap.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ private static class AppModule {
8888
private Module module;
8989
private Server server;
9090
private ClassLoader contextClassLoader;
91+
private int port;
9192

9293
public AppModule(Logger logger, ExtModuleLoader loader, String projectName, String mainClass,
93-
String executionMode, ClassLoader contextClassLoader) {
94+
int port, String executionMode, ClassLoader contextClassLoader) {
9495
this.logger = logger;
9596
this.loader = loader;
9697
this.projectName = projectName;
9798
this.mainClass = mainClass;
99+
this.port = port;
98100
this.executionMode = executionMode;
99101
this.contextClassLoader = contextClassLoader;
100102
}
@@ -113,10 +115,11 @@ public void start() {
113115

114116
Class appClass = classLoader.loadClass(mainClass);
115117
Enum executionModeValue = Enum.valueOf(executionModeClass, executionMode.toUpperCase());
116-
App app = new App(createApp.invoke(null, new String[0], executionModeValue, appClass));
118+
App app = new App(createApp
119+
.invoke(null, new String[]{"server.port=" + port}, executionModeValue, appClass));
117120
server = app.start();
118121
} catch (Exception x) {
119-
logger.error("Unable to start: {}", mainClass, withoutReflection(x));
122+
logger.error("execution of {} resulted in exception", mainClass, withoutReflection(x));
120123
} finally {
121124
Thread.currentThread().setContextClassLoader(contextClassLoader);
122125
}
@@ -133,11 +136,12 @@ public void close() {
133136
}
134137

135138
private Throwable withoutReflection(Throwable cause) {
136-
while(cause instanceof ReflectiveOperationException) {
139+
while (cause instanceof ReflectiveOperationException) {
137140
cause = cause.getCause();
138141
}
139142
return cause;
140143
}
144+
141145
private void unloadModule() {
142146
try {
143147
if (module != null) {
@@ -181,6 +185,8 @@ private void closeServer() {
181185

182186
private AppModule module;
183187

188+
private int port = 8080;
189+
184190
public HotSwap(String projectName, String mainClass, String executionMode) {
185191
this.projectName = projectName;
186192
if (mainClass.endsWith("Kt")) {
@@ -222,7 +228,7 @@ public void start() throws Exception {
222228
ModuleFinder[] finders = {new FlattenClasspath(projectName, resources, dependencies)};
223229

224230
ExtModuleLoader loader = new ExtModuleLoader(finders);
225-
module = new AppModule(logger, loader, projectName, mainClass, executionMode,
231+
module = new AppModule(logger, loader, projectName, mainClass, port, executionMode,
226232
Thread.currentThread().getContextClassLoader());
227233
module.start();
228234
watcher.watch();
@@ -231,6 +237,14 @@ public void start() throws Exception {
231237
}
232238
}
233239

240+
public int getPort() {
241+
return port;
242+
}
243+
244+
public void setPort(int port) {
245+
this.port = port;
246+
}
247+
234248
public void restart() {
235249
module.restart();
236250
}

0 commit comments

Comments
 (0)