Make Maven Serve Files Like CSS.Gz and Js.Gz

Javascript and CSS files combining in Maven build WITHOUT compression, minification etc

Make the following changes to your project:

  1. In your pom.xml, <dependencies> tag, place:

    <!-- JAVASCRIPT COMBINATION -->
    <dependency>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-core</artifactId>
    </dependency>
  2. In your pom.xml, <plugins> tag, place:

    <plugin>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-maven-plugin</artifactId>
    <version>1.4.3</version>
    <executions>
    <execution>
    <phase>process-resources</phase>
    <goals>
    <goal>run</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <targetGroups>allDev</targetGroups>
    <destinationFolder>${basedir}/src/main/webapp/</destinationFolder>
    <contextFolder>${basedir}/src/main/webapp/</contextFolder>
    </configuration>
    </plugin>
  3. In your pom.xml (or the parent pom.xml) <dependencyManagement> tag, place:

    <!-- JAVASCRIPT COMBINATION -->
    <dependency>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-core</artifactId>
    <version>1.8.0</version>
    </dependency>
  4. Create a wro.xml under /project/src/main/webapp/WEB-INF and place something like the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <groups xmlns="http://www.isdc.ro/wro">
    <group name="allDev">
    <js minimize="false">/my1stJsFolder/*.js</js>
    <js minimize="false">/my2ndJsFolder/*.js</js>
    <js minimize="false">/someFileDirectlyUnderWEBINF.js</js>
    </group>
    </groups>
  5. In web.xml insert:

    <filter>
    <filter-name>WebResourceOptimizer</filter-name>
    <filter-class>ro.isdc.wro.http.WroFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>WebResourceOptimizer</filter-name>
    <url-pattern>/wro/*</url-pattern>
    </filter-mapping>

allDev.js will be generated under /project/src/main/webapp.

I am not sure yet how I could tell wro4j to only generate one in target (now you have two - one in the project source files and one in target).

When compressing with yui plugin I only get one in target. But this should not be that big of a problem for you.

For more:

  • https://code.google.com/p/wro4j/wiki/GettingStarted
  • https://code.google.com/p/wro4j/wiki/MavenPlugin
  • http://www.jarvana.com/jarvana/search?search_type=project&project=wro4j-maven-plugin

ADDITIONALLY:

If you have any problems with the resource processing try adding to <build> tag:

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

And also to <plugins> add

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
<nonFilteredFileExtension>jpg</nonFilteredFileExtension>
<nonFilteredFileExtension>jpeg</nonFilteredFileExtension>
<nonFilteredFileExtension>class</nonFilteredFileExtension>
<nonFilteredFileExtension>jks</nonFilteredFileExtension>
<nonFilteredFileExtension>exe</nonFilteredFileExtension>
<nonFilteredFileExtension>wmv</nonFilteredFileExtension>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
<nonFilteredFileExtension>zip</nonFilteredFileExtension>
<nonFilteredFileExtension>gz</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

I m trying to gzip js & css files at build time using wro4j Maven

I've written a simple Maven plugin to just do this: gzip JS & CSS files.

https://github.com/manikantag/em-maven-resource-optimizer

Sending pre-minified/pre-gzipped CSS and JS to the browser

I figure it, here is the solution:

AddEncoding gzip .jsgz .cssgz
AddType application/x-javascript .jsgz
AddType text/css .cssgz

RewriteEngine on
# If client accepts compressed files
RewriteCond %{HTTP:Accept-Encoding} gzip
# and if compressed file exists
RewriteCond %{REQUEST_FILENAME}gz -f
# send .min.cssgz and .min.cssgz instead of .css and .js
RewriteRule ^(.+)\.(min.css|min.js)$ $1.$2gz [L]

For some reason file_name.min.css.gz is not working, so solution with file_name.min.cssgz is working great. I hope that this will help someone.

gzip compression for jsf stuff

Your server.xml is all fine. Your concrete problem is most likely that you've edited the wrong server.xml file. In an IDE like Eclipse, the original Tomcat server installation is by default kept untouched. Instead, Eclipse creates a copy of all its configuration files in the Servers project and uses the workspace metadata to deploy the webapps and ultimately only the Tomcat server engine is been used from the Tomcat installation.

Make sure that you're editing the right server.xml file, the one in Servers project:

Sample Image

Tomcat's own /conf/server.xml file is only used when you start it outside Eclipse, or when you tell Eclipse to take full control over Tomcat installation instead. To do that,
doubleclick the Tomcat server entry in Servers view and alter the Server Locations accordingly.

Sample Image

What is the simplest way to aggregate/assemble multiple (js) files into one (js) file with a maven plugin WITHOUT compression?

The answer would lie in wro4j library. For a more precise setup see:

Javascript and CSS files combining in Maven build WITHOUT compression, minification etc

Maven for static web projects

If your assembly descriptor does all the data processing (creating folders, copying files, etc.), then you can also declare the project to be a pom project. This will result in maven skipping the default life-cycle phases like compile, test, etc.

Maven Webjars are not found

The bootstrap-<ver>.jar has a META-INF/resources/ subdirectory.

$ jar -tvf bootstrap-4.0.0.jar | grep META-INF/resources
0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/
0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/
0 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/bootstrap/
0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/
0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/
0 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/
43852 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid-jsf.css
4076 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid-jsf.css.gz
43852 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css
4076 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css.gz
95910 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.css.map
34243 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min-jsf.css
3483 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min-jsf.css.gz
34243 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css
3483 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css.gz
76209 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-grid.min.css.map
178152 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-jsf.css
22410 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-jsf.css.gz
4798 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot-jsf.css
1683 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot-jsf.css.gz
4798 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css
1683 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css.gz
57721 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.css.map
3936 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min-jsf.css
1584 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min-jsf.css.gz
3936 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css
1584 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css.gz
25881 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap-reboot.min.css.map
178152 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css
22410 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css.gz
411645 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css.map
144877 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min-jsf.css
20563 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min-jsf.css.gz
144877 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css
20563 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css.gz
551641 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.min.css.map
195855 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js
41578 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js.gz
326634 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.js.map
67742 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js
19244 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js.gz
273872 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.bundle.min.js.map
115048 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js
20137 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js.gz
195373 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.js.map
48944 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js
13105 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js.gz
161998 Thu Jan 18 11:29:48 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/js/bootstrap.min.js.map
284 Thu Jan 18 21:20:32 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/webjars-requirejs.js
182 Thu Jan 18 21:20:38 GMT 2018 META-INF/resources/webjars/bootstrap/4.0.0/webjars-requirejs.js.gz

This kind of JAR is a web resource jar, and for Jetty it's only available when using a full blown WebAppContext.

Note: When using a Jetty WebAppContext all of the WEB-INF/lib/*.jar!/META-INF/resources/ contents will be unpacked into a temporary directory so that Jetty can serve the contents of those special jar files.

You have a few options from here (ordered from best/easiest choice to most complex).

  1. Have maven unpack your META-INF/resources jars into your resources directory, that you then reference by classpath resource URL as the ServletContextHandler resource base location.
  2. Have your own code unpack the META-INF/resources directories into a temporary directory that you then use as a the ServletContextHandler resource base directory.
  3. Change over to using a full blown WebAppContext with a war file and all of the extra configuration necessary to enable the various features you want to use.

Note, if your eventual end goal is to build a jetty uber jar, then option 1 will be the best choice overall.

The choice you make will depend on how you eventually want to package your project up.

  • Will it be 1 self executing jar? Then option 1.
  • Will it be a collection of jars? Then options 1 and 2 are good choices.
  • Will it be a collection of jars and a war file? Then option 3 is a good choice.

The biggest issue with META-INF/resources based content is name collision resolution. Because of this, I encourage you to go with option 1 as this will resolve (at build time) any conflict resolution issues.

Eg: If you have 2 JAR files, both with META-INF/resources/foo.css file (but with different contents) and a request arrives for http://<host>/foo.css, which one do you serve?

For an example of Option 1 see - https://github.com/jetty-project/embedded-jetty-with-web-resources

Some embedded-jetty resources maintained by the Eclipse Jetty project:

  • https://www.eclipse.org/jetty/documentation/9.4.x/embedding-jetty.html
  • https://github.com/jetty-project/embedded-jetty-cookbook
  • https://github.com/eclipse/jetty.project/tree/jetty-9.4.9.v20180320/examples/embedded/


Related Topics



Leave a reply



Submit