Ever searched for a simple configuration, that lets you use your jetty-maven-plugin as you are used to, while working with LESS to simplify your stylesheets?
-You cannot do both, use the Client-side mode of LESS to ease development and use the lesscss-maven-plugin to automatically compile the LESS-sources into CSS for production. That does not work, because your stylesheets must be linked in different ways if you are switching between the client-side mode – which is best for development – and the pre-compiled mode – which is best for production. For the client-side mode you need something like:
+Ever searched for a simple configuration, that lets you use your jetty-maven-plugin as you are used to, while working with LESS to simplify your stylesheets?
+You cannot do both, use the Client-side mode of LESS to ease development and use the lesscss-maven-plugin to automatically compile the LESS-sources into CSS for production. That does not work, because your stylesheets must be linked in different ways if you are switching between the client-side mode – which is best for development – and the pre-compiled mode – which is best for production. For the client-side mode you need something like:
- <link rel="stylesheet/less" type="text/css" href="styles.less" />
+ <link rel="stylesheet" type="text/css" href="styles.css" />
<script src="less.js" type="text/javascript"></script>
While, for the pre-compiled mode, you want to link to your stylesheets as usual, with:
- <link rel="stylesheet/less" type="text/css" href="styles.css" />
+ <link rel="stylesheet" type="text/css" href="styles.css" />
While looking for a solution to this dilemma, I stumbled accross wro4j. Originally intended, to speed up page-delivery by combining and minimizing multiple resources into one through the use of a servlet-filter, this tool also comes with a maven-plugin, that let you do the same offline, while compiling your webapp.
@@ -50,13 +47,13 @@
<groups xmlns="http://www.isdc.ro/wro">
<group name="base">
- <css>/less/base/*.less</css>
+ <css>/css/base/*.css</css>
</group>
- wro4j looks for /less/base/*.less
inside the root of the web-context, which is equal to src/main/webapp
in a normal maven-project. There are other ways to specifie the resources, which enable you to store them elswhere. But this approach works best for our goal, because the path is understandable for both: the wro4j servlet-filter, we are configuring now for our development-environment, and the wro4j-maven-plugin, that we will configure later for build-time compilation.
wro4j looks for /css/base/*.css
inside the root of the web-context, which is equal to src/main/webapp
in a normal maven-project. There are other ways to specifie the resources, which enable you to store them elswhere. But this approach works best for our goal, because the path is understandable for both: the wro4j servlet-filter, we are configuring now for our development-environment, and the wro4j-maven-plugin, that we will configure later for build-time compilation.
wro.properties
-wro.properties in short tells wro4j, how or if it should convert the combined sources and how it should behave. I am using the following configuration to tell wro4j, that it should convert *.less
-sources into CSS and do that on every request:
wro.properties in short tells wro4j, how or if it should convert the combined sources and how it should behave. I am using the following configuration to tell wro4j, that it should convert *.css
-sources into CSS and do that on every request:
preProcessors=less4j
@@ -205,7 +202,7 @@
The filter processes any URI’s that end with .css
. This way, the wro4j servlet-filter makes base.css
available under any path, because for exampl /base.css
, /css/base.css
and /foo/bar/base.css
all end with .css
.
This is all, that is needed to develop with dynamically reloadable compiled LESS-resources. Just fire up your browser and browse to /what/you/like/base.css
. (But do not forget to put some LESS-files in src/main/webapp/less/base/
first!)
This is all, that is needed to develop with dynamically reloadable compiled LESS-resources. Just fire up your browser and browse to /what/you/like/base.css
. (But do not forget to put some LESS-files in src/main/webapp/css/base/
first!)
Step 3: Install wro4j-maven-plugin
All that is left over to configure now, is the build-process. If you would build and deploy your webapp now, the CSS-file base.css
would not be generated and the link to your stylesheet, that already works in our jetty-maven-plugin environment would point to a 404. Hence, we need to set up the wro4j-maven-plugin. I am using this configuration:
@@ -253,7 +250,7 @@We only scrached the surface, of what can be done with wro4j. Based on this configuration, you can easily enable additional features to fine-tune your final build for maximum speed. You really should take a look at the list of available Processors!