Fork me on GitHub

About

The LessCSS Compiler is a Java library which compiles Less source files to CSS code.

From Less website:
Less is a CSS pre-processor, meaning that it extends the CSS language, adding features that allow variables, mixins, functions and many other techniques that allow you to make CSS that is more maintainable, themable and extendable.

Compatibility

The compiler is compatible with version 1.7.5. The library is based on the official Less JavaScript compiler adapted to the Rhino engine.

It supports sources located at:

  • local drives
  • protocols:
  • custom - defined by programmers (see FileSystem)

Requirements

The compiler to run requires:

  • Java 8 or higher
  • Third-Party Dependencies (see list)

Download

You can download the library from this page or using various dependency management tools.

Concept

The library contains two compilers:

The idea for the NativeLessCompiler class was based on the lesscss-java library by Marcel Overdijk.

Usage

The LessCompiler contains 32 methods. Below is an example of how to use some of them:

String cssCode = null;
LessOptions options = null;

// create compiler
LessCompiler compiler = new LessCompiler();

// compile source code
cssCode = compiler.compileCode(".basic { display: block; }");

// compile source code with custom options
options = new LessOptionsBuilder().ieCompatibilityOff().build();
cssCode = compiler.compileCode(".basic { display: block; }", options);

// compile source file specified by path
cssCode = compiler.compile("http://www.example.org/style.less");

// compile source file
cssCode = compiler.compile(new File("source.less"));

// compile source file specified by path and save CSS code in an output file
compiler.compile("http://www.example.org/style.less", new File("output.css"));

// compile source file and save CSS code in an output file
compiler.compile(new File("source.less"), new File("output.css"));

// compile source file and compress CSS code
cssCode = compiler.compileAndCompress(new File("source.less"));

// compile source file specified by path and compress CSS code using custom encoding
cssCode = compiler.compileAndCompress("http://www.example.org/style.less", Charset.forName("UTF-8"));

// compile source code and generate inline source map
cssCode = compiler.compileCodeWithInlineSourceMap(".basic { display: block; }", new LessOptions());

// compile source file and generate source map (save it in output.map file)
options = new LessOptionsBuilder().sourceMapBasePath("basePath").build();
compiler.compileWithSourceMap(new File("source.less"), new File("output.css"), new File("output.map"), options);

// compile source file specified by path and generate source map (save it in output.css.map file)
compiler.compileWithSourceMap("http://www.example.org/style.less", new File("output.css"), options);