2323import java .lang .reflect .InvocationTargetException ;
2424import java .util .List ;
2525import java .util .Map ;
26- import java .util .function .Consumer ;
2726import java .util .stream .Collectors ;
2827
2928import org .apache .maven .plugin .AbstractMojo ;
4847@ Execute (phase = LifecyclePhase .PREPARE_PACKAGE )
4948public 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}
0 commit comments