0

I am facing a strange issue while trying to run a simple AngularJS Spring Boot project (after sorting out the issues mentioned in this thread). Following is my Maven POM. The issue I am facing is that while the Wro4j plugin is able to generate the JS/CSS files under generated-resources, and despite that under build/resources I have provided the paths to be packaged, the files under generated-resources are not getting packaged within the JAR under "static" folder (only my custom JS file and the index.html file appear there). How could this be corrected?

    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-angularjs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>  

    <parent>
        <groupId>org.test</groupId>
        <artifactId>my-microservice-parent</artifactId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <wro4j.version>1.8.0</wro4j.version>
        <java.version>1.8</java.version>
        <bootstrap.version>3.3.7-1</bootstrap.version>
        <angularjs.version>1.5.9</angularjs.version>
        <jquery.version>3.1.1-1</jquery.version>
    </properties>

    <dependencies>  
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>${angularjs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jasmine</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies> 

    <build>
        <resources>         
            <resource>
                <directory>${project.build.directory}/generated-resources</directory>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>           
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- Serves *only* to filter the wro.xml so it can get an absolute 
                            path for the project -->
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/wro</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/wro</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>ro.isdc.wro4j</groupId>
                <artifactId>wro4j-maven-plugin</artifactId>
                <version>${wro4j.version}</version> 
                <executions>
                  <execution>
                    <phase>compile</phase>
                    <goals>
                      <goal>run</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                    <wroManagerFactory>ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory</wroManagerFactory>
                    <cssDestinationFolder>${project.build.directory}/generated-resources/static/css</cssDestinationFolder>
                    <jsDestinationFolder>${project.build.directory}/generated-resources/static/js</jsDestinationFolder>
                    <wroFile>${project.build.directory}/wro/wro.xml</wroFile>
                    <!--
                    <extraConfigFile>${basedir}/src/main/wro/wro.properties</extraConfigFile>
                    <contextFolder>${basedir}/src/main/wro</contextFolder>
                    -->
                    <extraConfigFile>${project.build.directory}/wro/wro.properties</extraConfigFile>
                    <contextFolder>${project.build.directory}/wro</contextFolder>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>jquery</artifactId>
                        <version>${jquery.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>angularjs</artifactId>
                        <version>${angularjs.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>bootstrap</artifactId>
                        <version>${bootstrap.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>com.github.searls</groupId>
                <artifactId>jasmine-maven-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>       
                <dependencies>
                    <dependency>
                        <groupId>org.webjars</groupId>
                        <artifactId>angularjs</artifactId>
                        <version>${angularjs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

Following is the Maven build log (for mvn clean package):

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-angularjs 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ spring-angularjs ---
[INFO] Deleting C:\projects\spring-angularjs\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:copy-resources (copy-resources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\projects\spring-angularjs\target\generated-resources
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-angularjs ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\projects\spring-angularjs\target\classes
[INFO] 
[INFO] --- wro4j-maven-plugin:1.8.0:run (default) @ spring-angularjs ---
[INFO] C:\projects\spring-angularjs\target/wro
[INFO] Executing the mojo: 
[INFO] Wro4j Model path: C:\projects\spring-angularjs\target\wro\wro.xml
[INFO] targetGroups: null
[INFO] minimize: true
[INFO] ignoreMissingResources: null
[INFO] parallelProcessing: false
[INFO] buildDirectory: C:\projects\spring-angularjs\target
[INFO] destinationFolder: C:\projects\spring-angularjs\target
[INFO] jsDestinationFolder: C:\projects\spring-angularjs\target\generated-resources\static\js
[INFO] cssDestinationFolder: C:\projects\spring-angularjs\target\generated-resources\static\css
[INFO] The following groups will be processed: [angular-bootstrap]
[INFO] folder: C:\projects\spring-angularjs\target\generated-resources\static\css
[INFO] processing group: angular-bootstrap.css
[WARNING] Less warnings are:
[WARNING] 10:1 Cannot link source map. Css result location is not know and could not be deduced from input less source..
[INFO] file size: angular-bootstrap.css -> 145074 bytes
[INFO] C:\projects\spring-angularjs\target\generated-resources\static\css\angular-bootstrap.css (145074 bytes)
[INFO] folder: C:\projects\spring-angularjs\target\generated-resources\static\js
[INFO] processing group: angular-bootstrap.js
[INFO] file size: angular-bootstrap.js -> 254638 bytes
[INFO] C:\projects\spring-angularjs\target\generated-resources\static\js\angular-bootstrap.js (254638 bytes)
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-angularjs ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\projects\spring-angularjs\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-angularjs ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-angularjs ---
[INFO] No tests to run.
[INFO] 
[INFO] --- jasmine-maven-plugin:2.0:test (default) @ spring-angularjs ---
[INFO] jetty-8.1.14.v20131031
[INFO] Started SelectChannelConnector@0.0.0.0:60402
[INFO] Executing Jasmine Specs
[INFO] Resolved artifact C:\Users\XXXXX\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip from remote-repos-vr (https://vrartifactory.eficode.com/remote-repos-vr, default, releases+snapshots)
[INFO] Extracting C:\Users\XXXXX\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip\phantomjs-2.0.0-windows\bin\phantomjs.exe to C:\projects\spring-angularjs\target\phantomjs\phantomjs-2.0.0-windows\bin\phantomjs.exe
[INFO] 
-------------------------------------------------------
 J A S M I N E   S P E C S
-------------------------------------------------------
[INFO] 

Results: 0 specs, 0 failures, 0 pending

[INFO] stopped o.e.j.s.h.ContextHandler{/webjars,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/classpath,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/spec,file:/C:/projects/spring-angularjs/}
[INFO] stopped o.e.j.s.h.ContextHandler{/src,file:/C:/projects/spring-angularjs/}
[INFO] 
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ spring-angularjs ---
[INFO] Building jar: C:\projects\spring-angularjs\target\spring-angularjs-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.3.8.RELEASE:repackage (default) @ spring-angularjs ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.848 s
[INFO] Finished at: 2017-11-09T16:04:39+05:30
[INFO] Final Memory: 98M/503M
[INFO] ------------------------------------------------------------------------
Dchucks
  • 1,189
  • 5
  • 22
  • 48
  • Your project seems properly configured. Can you paste your maven build log to see which goals are run and how? That may help diagnose your issue. – Pierre B. Nov 09 '17 at 09:44
  • Thanks Pierre for reading through. I edited my question to add the Build log. Thanks for your help. – Dchucks Nov 09 '17 at 10:40
  • Seems your files are generated. Can you provide the debug output? Add the -X flag: `mvn clean package -X` – Pierre B. Nov 09 '17 at 11:02
  • Ya I already mentioned in my question that "Wro4j plugin is able to generate the JS/CSS files under generated-resources". But they are not being packaged in the JAR. – Dchucks Nov 09 '17 at 11:35
  • Yes, debug output will show more info regarding included and excluded folders. – Pierre B. Nov 09 '17 at 12:34
  • I finally was able to get the Wro4j generated js/css files getting packaged by adding another execution cycle in the Maven resources plugin configuration. Sadly I had to invoke it during the "test" phase as I could not think of any other way to invoke the plugin after compilation but before packaging. First time maven-resource plugin gets called is during the "validate" phase, but the wro4j plugin is yet to create its magic, so the generated files are obviously not yet there. Funny thing is: the build.resources.resource doesn't have any impact during packaging, even tried adding *targetPath*. – Dchucks Nov 09 '17 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158604/discussion-between-pierre-b-and-dchucks). – Pierre B. Nov 09 '17 at 12:56
  • Following is what I added to POM for the *maven-resources-plugin* plugin: ` copy-resources-post-compile test copy-resources ${basedir}/target/classes ${project.build.directory}/generated-res‌​ources false ` – Dchucks Nov 09 '17 at 12:56
  • Your files are generated at compile time *directly* into `generated-res‌​ources`, a resource folder as per `resources` config. During package, every resource folders are normally included in your jar, _unless a filter or exclusion is defined somewhere else_. Files are not included from `generated-resources` but from `target/classes`. I suspect another configuration causes your issue, probably from parent pom. Though your workaround is working, the real cause of your issue is somewhere else. If you provide debug output and the parent pom config we'll probably be able to identify it ;) – Pierre B. Nov 09 '17 at 13:06
  • Sure, sent you the details over Chat Pierre. Thanks again for the help. – Dchucks Nov 09 '17 at 13:32
  • There's also a dedicated starter for Spring Boot: https://github.com/michael-simons/wro4j-spring-boot-starter – Michael Simons Feb 15 '19 at 19:50

1 Answers1

1

wro4j-maven-plugin:run is running at compile phase, after the process-resources phases where all resources are copied to ${project.build.outputDirectory} (by default target/classes). Only files from this folder are included into your JAR, but your CSS/JS files are generated after Maven tried to copy them to the outputDirectory, thus never being added.

You simply have to configure Wro4j plugin to run during generate-resources phase:

       <plugin>
            <groupId>ro.isdc.wro4j</groupId>
            <artifactId>wro4j-maven-plugin</artifactId>
            <version>${wro4j.version}</version> 
            <executions>
              <execution>
                <phase>generate-resources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
       ...
       </plugin>

If for some reasons your files cannot be generated before compile (maybe it requires your code to be compiled), you'll have instead to manually add your resources into your project build output directory with the Maven Resources Plugin after they are generated as you did in your comment - though I would recommend doing it during the prepare-package phase using ${project.build.outputDirectory} such as:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <execution>
        <id>copy-resources-post-compile</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.outputDirectory}</outputDir‌​ector‌​y>
            <resources>
                <resource>
                    <directory>${project.build.directory}/generated-res‌​ources</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
</plugin>

EDIT: To learn more about the Maven lifecycle, phases and goals, see Intro to Build Lifecycle and this tutorial explains things well. Also take a look at the default bindings to see which plugins are run by default depending on your packaging.

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
  • Thanks a ton Pierre for taking time to go through all the debug log over chat. Appreciate immensely! Sadly running Wro4j during *generate-resources* phase didn't work either (I will investigate why). I then made the change in the maven-resource plugin execution to change the phase to *prepare-package* and it worked beautifully. Thanks so much again. If possible, can you point me to some good tutorial where I get t know about all the Maven phases in detail? – Dchucks Nov 09 '17 at 16:38
  • Happy to help, love to solve technical issues ;) I edited my answer with docs and tutorials. I recommend pushing your research with Google or so for more. – Pierre B. Nov 10 '17 at 08:24