Using SASS in Java web applications
Modern frontend technologies like SASS, Less or CoffeeScript are not as easy to setup in classic Java web application as it is in modern frameworks like Ruby On Rails or Play Framework but it is possible. After spending some time on a project using SASS I can’t really imagine going back to plain old CSS so I decided to have a look what is offered currently for Java.
There are very powerful (and complex) solutions for handling SASS compilation like Wro4j but I wanted something simple that meets requirements:
- compiles SASS to CSS during build
- not requiring me to add any additional libs to application
- developer mode friendly – I want to be able to change SASS files on the fly during development and avoid server restarts
Sass Maven Plugin
Sass Maven Plugin comes with help. Just add plugin configuration to your pom.xml and you are ready to start.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
This configuration assumes that you will store your SASS files in src/main/stylesheets directory and they will be compiled to CSS into src/main/webapp/resources/stylesheets. Plugin will be invoked during build process before project is packaged to WAR.
So called development mode let’s you change assets like view templates and static resources on the fly without need for server restart. It works very well with embedded Tomcat. In order to enable SASS compilation whenever SASS files change just execute