|
31 | 31 | import org.gradle.tooling.ResultHandler; |
32 | 32 |
|
33 | 33 | import java.io.File; |
| 34 | +import java.lang.reflect.InvocationTargetException; |
34 | 35 | import java.nio.file.Files; |
35 | 36 | import java.nio.file.Path; |
36 | 37 | import java.util.Arrays; |
@@ -58,82 +59,87 @@ public class JoobyRun extends DefaultTask { |
58 | 59 | private ProjectConnection connection; |
59 | 60 |
|
60 | 61 | @TaskAction |
61 | | - public void run() throws Exception { |
62 | | - Project current = getProject(); |
63 | | - List<Project> projects = Arrays.asList(current); |
64 | | - |
65 | | - String mainClass = projects.stream() |
66 | | - .filter(it -> it.getProperties().containsKey("mainClassName")) |
67 | | - .map(it -> it.getProperties().get("mainClassName").toString()) |
68 | | - .findFirst() |
69 | | - .orElseThrow(() -> new IllegalArgumentException( |
70 | | - "Application class not found. Did you forget to set `mainClassName`?")); |
71 | | - |
72 | | - HotSwap hotSwap = new HotSwap(current.getName(), mainClass, executionMode); |
73 | | - |
74 | | - connection = GradleConnector.newConnector() |
75 | | - .useInstallation(current.getGradle().getGradleHomeDir()) |
76 | | - .forProjectDirectory(current.getRootDir()) |
77 | | - .connect(); |
78 | | - |
79 | | - BuildLauncher compiler = connection.newBuild() |
80 | | - .setStandardError(System.err) |
81 | | - .setStandardOutput(System.out) |
82 | | - .forTasks("classes"); |
83 | | - |
84 | | - Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
85 | | - hotSwap.shutdown(); |
86 | | - connection.close(); |
87 | | - })); |
88 | | - |
89 | | - BiConsumer<String, Path> onFileChanged = (event, path) -> { |
90 | | - if (isCompileExtension(path)) { |
91 | | - compiler.run(new ResultHandler<Void>() { |
92 | | - @Override public void onComplete(Void result) { |
93 | | - getLogger().debug("Restarting application on file change: " + path); |
94 | | - hotSwap.restart(); |
95 | | - } |
96 | | - |
97 | | - @Override public void onFailure(GradleConnectionException failure) { |
98 | | - getLogger().debug("Compilation error found: " + path); |
99 | | - } |
100 | | - }); |
101 | | - } else if (isRestartExtension(path)) { |
102 | | - getLogger().debug("Restarting application on file change: " + path); |
103 | | - hotSwap.restart(); |
104 | | - } else { |
105 | | - getLogger().debug("Ignoring file change: " + path); |
106 | | - } |
107 | | - }; |
108 | | - |
109 | | - for (Project project : projects) { |
110 | | - getLogger().debug("Adding project: " + project.getName()); |
111 | | - |
112 | | - SourceSet sourceSet = sourceSet(project); |
113 | | - // main/resources |
114 | | - sourceSet.getResources().getSrcDirs().stream() |
115 | | - .map(File::toPath) |
116 | | - .forEach(file -> hotSwap.addResource(file, onFileChanged)); |
117 | | - // conf directory |
118 | | - Path conf = project.getProjectDir().toPath().resolve("conf"); |
119 | | - hotSwap.addResource(conf, onFileChanged); |
120 | | - |
121 | | - // build classes |
122 | | - binDirectories(project, sourceSet).forEach(hotSwap::addResource); |
123 | | - |
124 | | - Set<Path> src = sourceDirectories(project, sourceSet); |
125 | | - if (src.isEmpty()) { |
126 | | - getLogger().debug("Compiler is off in favor of Eclipse compiler."); |
127 | | - binDirectories(project, sourceSet).forEach(path -> hotSwap.addResource(path, onFileChanged)); |
128 | | - } else { |
129 | | - src.forEach(path -> hotSwap.addResource(path, onFileChanged)); |
| 62 | + public void run() throws Throwable { |
| 63 | + try { |
| 64 | + Project current = getProject(); |
| 65 | + List<Project> projects = Arrays.asList(current); |
| 66 | + |
| 67 | + String mainClass = projects.stream() |
| 68 | + .filter(it -> it.getProperties().containsKey("mainClassName")) |
| 69 | + .map(it -> it.getProperties().get("mainClassName").toString()) |
| 70 | + .findFirst() |
| 71 | + .orElseThrow(() -> new IllegalArgumentException( |
| 72 | + "Application class not found. Did you forget to set `mainClassName`?")); |
| 73 | + |
| 74 | + HotSwap hotSwap = new HotSwap(current.getName(), mainClass, executionMode); |
| 75 | + |
| 76 | + connection = GradleConnector.newConnector() |
| 77 | + .useInstallation(current.getGradle().getGradleHomeDir()) |
| 78 | + .forProjectDirectory(current.getRootDir()) |
| 79 | + .connect(); |
| 80 | + |
| 81 | + BuildLauncher compiler = connection.newBuild() |
| 82 | + .setStandardError(System.err) |
| 83 | + .setStandardOutput(System.out) |
| 84 | + .forTasks("classes"); |
| 85 | + |
| 86 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 87 | + hotSwap.shutdown(); |
| 88 | + connection.close(); |
| 89 | + })); |
| 90 | + |
| 91 | + BiConsumer<String, Path> onFileChanged = (event, path) -> { |
| 92 | + if (isCompileExtension(path)) { |
| 93 | + compiler.run(new ResultHandler<Void>() { |
| 94 | + @Override public void onComplete(Void result) { |
| 95 | + getLogger().debug("Restarting application on file change: " + path); |
| 96 | + hotSwap.restart(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override public void onFailure(GradleConnectionException failure) { |
| 100 | + getLogger().debug("Compilation error found: " + path); |
| 101 | + } |
| 102 | + }); |
| 103 | + } else if (isRestartExtension(path)) { |
| 104 | + getLogger().debug("Restarting application on file change: " + path); |
| 105 | + hotSwap.restart(); |
| 106 | + } else { |
| 107 | + getLogger().debug("Ignoring file change: " + path); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + for (Project project : projects) { |
| 112 | + getLogger().debug("Adding project: " + project.getName()); |
| 113 | + |
| 114 | + SourceSet sourceSet = sourceSet(project); |
| 115 | + // main/resources |
| 116 | + sourceSet.getResources().getSrcDirs().stream() |
| 117 | + .map(File::toPath) |
| 118 | + .forEach(file -> hotSwap.addResource(file, onFileChanged)); |
| 119 | + // conf directory |
| 120 | + Path conf = project.getProjectDir().toPath().resolve("conf"); |
| 121 | + hotSwap.addResource(conf, onFileChanged); |
| 122 | + |
| 123 | + // build classes |
| 124 | + binDirectories(project, sourceSet).forEach(hotSwap::addResource); |
| 125 | + |
| 126 | + Set<Path> src = sourceDirectories(project, sourceSet); |
| 127 | + if (src.isEmpty()) { |
| 128 | + getLogger().debug("Compiler is off in favor of Eclipse compiler."); |
| 129 | + binDirectories(project, sourceSet) |
| 130 | + .forEach(path -> hotSwap.addResource(path, onFileChanged)); |
| 131 | + } else { |
| 132 | + src.forEach(path -> hotSwap.addResource(path, onFileChanged)); |
| 133 | + } |
| 134 | + |
| 135 | + dependencies(project, sourceSet).forEach(hotSwap::addResource); |
130 | 136 | } |
131 | 137 |
|
132 | | - dependencies(project, sourceSet).forEach(hotSwap::addResource); |
| 138 | + // Block current thread. |
| 139 | + hotSwap.start(); |
| 140 | + } catch (InvocationTargetException x) { |
| 141 | + throw x.getCause(); |
133 | 142 | } |
134 | | - |
135 | | - // Block current thread. |
136 | | - hotSwap.start(); |
137 | 143 | } |
138 | 144 |
|
139 | 145 | private Set<Path> binDirectories(Project project, SourceSet sourceSet) { |
|
0 commit comments