Skip to content

Commit 91c376a

Browse files
committed
maven assets compiler isn't executed fix jooby-project#528
1 parent cca14b0 commit 91c376a

5 files changed

Lines changed: 84 additions & 86 deletions

File tree

jooby-maven-plugin/src/main/java/org/jooby/AssetMojo.java

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.lang.reflect.InvocationTargetException;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.function.Consumer;
2726
import java.util.stream.Collectors;
2827

2928
import org.apache.maven.plugin.AbstractMojo;
@@ -48,10 +47,6 @@
4847
@Execute(phase = LifecyclePhase.PREPARE_PACKAGE)
4948
public class AssetMojo extends AbstractMojo {
5049

51-
@SuppressWarnings("serial")
52-
private static class CompilationDone extends RuntimeException {
53-
}
54-
5550
@Component
5651
private MavenProject mavenProject;
5752

@@ -78,72 +73,67 @@ public void execute() throws MojoExecutionException, MojoFailureException {
7873
System.setProperty("application.env", env);
7974

8075
new JoobyRunner(mavenProject)
81-
.run(mainClass, app -> {
82-
app.on("*", compile(app.getClass().getClassLoader()));
76+
.run(mainClass, (app, conf) -> {
77+
compile(app.getClass().getClassLoader(), conf);
8378
});
84-
} catch (CompilationDone ex) {
8579
long end = System.currentTimeMillis();
8680
getLog().info("compilation took " + (end - start) + "ms");
8781
} catch (Throwable ex) {
8882
throw new MojoFailureException("Can't compile assets for " + mainClass, ex);
8983
}
9084
}
9185

92-
private Consumer<Config> compile(final ClassLoader loader) {
93-
return conf -> {
94-
try {
95-
output.mkdirs();
96-
97-
getLog().debug("claspath: " + loader);
98-
99-
Config assetConf = ConfigFactory.parseResources(loader, "assets.conf")
100-
.withFallback(conf);
101-
102-
getLog().debug("assets.conf: " + assetConf.getConfig("assets"));
103-
104-
AssetCompiler compiler = new AssetCompiler(loader, assetConf);
105-
106-
Map<String, List<File>> fileset = compiler.build(env, output);
107-
108-
StringBuilder dist = new StringBuilder();
109-
dist.append("assets.fileset {\n").append(fileset.entrySet().stream().map(e -> {
110-
String files = e.getValue().stream()
111-
.map(file -> output.toPath().relativize(file.toPath()))
112-
.map(path -> "/" + path.toString().replace("\\", "/"))
113-
.collect(Collectors.joining("\", \"", "[\"", "\"]"));
114-
return " " + e.getKey() + ": " + files;
115-
}).collect(Collectors.joining("\n")))
116-
.append("\n}\n");
117-
dist.append("assets.cache.maxAge = ").append(maxAge).append("\n");
118-
dist.append("assets.pipeline.dev = {}\n");
119-
dist.append("assets.pipeline.").append(env).append(" = {}\n");
120-
dist.append("assets.watch = false\n");
121-
File distFile = new File(output, "assets." + env + ".conf");
122-
try (FileWriter writer = new FileWriter(distFile)) {
123-
writer.write(dist.toString());
124-
}
125-
getLog().info("done: " + distFile.getPath());
126-
127-
// move output to fixed location required by zip/war dist
128-
List<File> files = fileset.values().stream()
129-
.flatMap(it -> it.stream())
130-
.collect(Collectors.toList());
131-
132-
for (File from : files) {
133-
File to = assemblyOutput.toPath().resolve(output.toPath().relativize(from.toPath()))
134-
.toFile();
135-
to.getParentFile().mkdirs();
136-
getLog().debug("copying file to: " + to);
137-
Files.copy(from, to);
138-
}
139-
} catch (InvocationTargetException ex) {
140-
throw Throwables.propagate(ex.getCause());
141-
} catch (Exception ex) {
142-
throw Throwables.propagate(ex);
86+
private void compile(final ClassLoader loader, final Config conf) {
87+
try {
88+
output.mkdirs();
89+
90+
getLog().debug("claspath: " + loader);
91+
92+
Config assetConf = ConfigFactory.parseResources(loader, "assets.conf")
93+
.withFallback(conf);
94+
95+
getLog().debug("assets.conf: " + assetConf.getConfig("assets"));
96+
97+
AssetCompiler compiler = new AssetCompiler(loader, assetConf);
98+
99+
Map<String, List<File>> fileset = compiler.build(env, output);
100+
101+
StringBuilder dist = new StringBuilder();
102+
dist.append("assets.fileset {\n").append(fileset.entrySet().stream().map(e -> {
103+
String files = e.getValue().stream()
104+
.map(file -> output.toPath().relativize(file.toPath()))
105+
.map(path -> "/" + path.toString().replace("\\", "/"))
106+
.collect(Collectors.joining("\", \"", "[\"", "\"]"));
107+
return " " + e.getKey() + ": " + files;
108+
}).collect(Collectors.joining("\n")))
109+
.append("\n}\n");
110+
dist.append("assets.cache.maxAge = ").append(maxAge).append("\n");
111+
dist.append("assets.pipeline.dev = {}\n");
112+
dist.append("assets.pipeline.").append(env).append(" = {}\n");
113+
dist.append("assets.watch = false\n");
114+
File distFile = new File(output, "assets." + env + ".conf");
115+
try (FileWriter writer = new FileWriter(distFile)) {
116+
writer.write(dist.toString());
143117
}
144-
// signal we are done
145-
throw new CompilationDone();
146-
};
118+
getLog().info("done: " + distFile.getPath());
119+
120+
// move output to fixed location required by zip/war dist
121+
List<File> files = fileset.values().stream()
122+
.flatMap(it -> it.stream())
123+
.collect(Collectors.toList());
124+
125+
for (File from : files) {
126+
File to = assemblyOutput.toPath().resolve(output.toPath().relativize(from.toPath()))
127+
.toFile();
128+
to.getParentFile().mkdirs();
129+
getLog().debug("copying file to: " + to);
130+
Files.copy(from, to);
131+
}
132+
} catch (InvocationTargetException ex) {
133+
throw Throwables.propagate(ex.getCause());
134+
} catch (Exception ex) {
135+
throw Throwables.propagate(ex);
136+
}
147137
}
148138

149139
}

jooby-maven-plugin/src/main/java/org/jooby/JoobyRunner.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,28 @@
3333
package org.jooby;
3434

3535
import java.net.URLClassLoader;
36-
import java.util.List;
37-
import java.util.function.Consumer;
36+
import java.util.function.BiConsumer;
3837

3938
import org.apache.maven.project.MavenProject;
4039

40+
import com.typesafe.config.Config;
41+
4142
public class JoobyRunner {
4243

4344
private Classpath cp;
4445

45-
private Consumer<List<Route.Definition>> routes;
46-
4746
public JoobyRunner(final MavenProject project) {
4847
this.cp = new Classpath(project);
4948
}
5049

51-
public JoobyRunner with(final Consumer<List<Route.Definition>> callback) {
52-
this.routes = callback;
53-
return this;
54-
}
55-
56-
public void run(final String mainClass, final Consumer<Jooby> callback)
50+
public void run(final String mainClass, final BiConsumer<Jooby, Config> callback)
5751
throws Throwable {
5852
ClassLoader global = Thread.currentThread().getContextClassLoader();
5953
try (URLClassLoader local = cp.toClassLoader()) {
6054
Thread.currentThread().setContextClassLoader(local);
6155
Jooby app = (Jooby) local.loadClass(mainClass).newInstance();
62-
callback.accept(app);
63-
if (routes != null) {
64-
routes.accept(Jooby.exportRoutes(app));
65-
} else {
66-
app.start();
67-
}
56+
Config conf = Jooby.exportConf(app);
57+
callback.accept(app, conf);
6858
} finally {
6959
Thread.currentThread().setContextClassLoader(global);
7060
}

jooby-maven-plugin/src/main/java/org/jooby/RouteProcessorMojo.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@
4040
@Execute(phase = LifecyclePhase.PROCESS_CLASSES)
4141
public class RouteProcessorMojo extends AbstractMojo {
4242

43-
@SuppressWarnings("serial")
44-
private static class ProcessDone extends RuntimeException {
45-
}
46-
4743
@Component
4844
private MavenProject mavenProject;
4945

@@ -56,10 +52,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
5652
Path srcdir = new File(mavenProject.getBuild().getSourceDirectory()).toPath();
5753
Path bindir = new File(mavenProject.getBuild().getOutputDirectory()).toPath();
5854
new JoobyRunner(mavenProject)
59-
.run(mainClass, app -> {
55+
.run(mainClass, (app, conf) -> {
6056
process(app, srcdir, bindir);
6157
});
62-
} catch (ProcessDone ex) {
6358
} catch (Throwable ex) {
6459
throw new MojoFailureException("Can't build route spec for: " + mainClass, ex);
6560
}
@@ -76,7 +71,6 @@ private void process(final Jooby app, final Path srcdir, final Path bindir) {
7671
.map(RouteSpec::toString)
7772
.collect(Collectors.joining("\n"));
7873
getLog().debug(app.getClass().getSimpleName() + ".spec :\n" + routes);
79-
throw new ProcessDone();
8074
}
8175

8276
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import java.util.concurrent.ExecutorService;
8282
import java.util.concurrent.TimeUnit;
8383
import java.util.concurrent.atomic.AtomicBoolean;
84+
import java.util.concurrent.atomic.AtomicReference;
8485
import java.util.function.Consumer;
8586
import java.util.function.Function;
8687
import java.util.function.Predicate;
@@ -1857,6 +1858,21 @@ public static void run(final Class<? extends Jooby> app, final String... args) {
18571858
run(() -> Try.of(() -> app.newInstance()).get(), args);
18581859
}
18591860

1861+
/**
1862+
* Export configuration from an application. Useful for tooling, testing, debugging, etc...
1863+
*
1864+
* @param app Application to extract/collect configuration.
1865+
* @return Application conf or <code>empty</code> conf on error.
1866+
*/
1867+
public static Config exportConf(final Jooby app) {
1868+
AtomicReference<Config> conf = new AtomicReference<>(ConfigFactory.empty());
1869+
app.on("*", c -> {
1870+
conf.set(c);
1871+
});
1872+
exportRoutes(app);
1873+
return conf.get();
1874+
}
1875+
18601876
/**
18611877
* Export routes from an application. Useful for route analysis, testing, debugging, etc...
18621878
*
@@ -1880,7 +1896,7 @@ class Success extends RuntimeException {
18801896
} catch (Success success) {
18811897
routes = success.routes;
18821898
} catch (Throwable x) {
1883-
logger(app).error("Unable to get routes from {}", app, x);
1899+
logger(app).debug("Failed bootstrap: {}", app, x);
18841900
}
18851901
return routes;
18861902
}

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,14 @@ public void exportRoutes() {
904904
assertEquals("GET", routes.get(0).method());
905905
}
906906

907+
@Test
908+
public void exportConf() {
909+
Jooby app = new Jooby();
910+
app.use(ConfigFactory.empty().withValue("JoobyTest", ConfigValueFactory.fromAnyRef("foo")));
911+
Config conf = Jooby.exportConf(app);
912+
assertEquals("foo", conf.getString("JoobyTest"));
913+
}
914+
907915
@Test
908916
public void exportRoutesFailure() {
909917
Jooby app = new Jooby();

0 commit comments

Comments
 (0)