Compile LESS"

From Documentation
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKStyleCustomizationGuidePageHeader}}
 
{{ZKStyleCustomizationGuidePageHeader}}
  
= Compile LESS to CSS =
+
= Install Node.js =
 +
Node.js is now required for our ZK-Less Engine since ZK 7.0.3.
  
If your LESS files don't use any DSP functions, you can simply compile it by following instruction on the LESS official website. Or, if you prefer to compile LESS from java, you can use [http://www.asual.com/lesscss/ lesscss-engine] developed by Asual DZZD.
+
If you don't have Node.js installed yet, please follow the steps given by the [http://nodejs.org/ official-website] to install Node.js.
 +
 
 +
Node version 0.10.30 or above is required. To check your Node.js version, simply run:
 +
 
 +
<source lang="bash">node --version</source>
  
 
= Compile LESS to DSP =
 
= Compile LESS to DSP =
  
If you prefer to use DSP page, here we introduce how to compile LESS to DSP files by Ant and Maven.
+
Here we introduce two ways to compile LESS files to DSP files. One is by Ant and the other is by Maven.
 +
 
 +
You could choose either one of them depends on your preferences. 
  
 
== Compile LESS by Ant ==
 
== Compile LESS by Ant ==
 
+
* Have Node.js installed in your environment.
 
* Follow the online [http://ant.apache.org/manual/install.html document] to install Apache Ant.
 
* Follow the online [http://ant.apache.org/manual/install.html document] to install Apache Ant.
* Download the following necessary jars in your '''''project/lib''''' folder from [http://github.com/zkoss/zk/tree/v7.0.0-Preview/dist/lib/ext zk repository] (right click -> save as).
+
* Download the following necessary jars into your '''''project/lib''''' folder from [https://github.com/zkoss/zk/tree/7.0-Stable/dist/lib/ext zk repository] (right click -> save as).
** commons-logging.jar
 
** js.jar
 
** lesscss-engine.jar
 
 
** yuicompressor.jar
 
** yuicompressor.jar
 
** zkjszips.jar
 
** zkjszips.jar
** zkless.jar (extension from [http://www.asual.com/lesscss/ lesscss-engine])
+
** commons-io.jar
 +
** CssCompressor.jar
 
** zul.jar (extract zk-bin-7.0.X.zip file from [http://sourceforge.net/projects/zk1/files/ZK/ sourceforge])
 
** zul.jar (extract zk-bin-7.0.X.zip file from [http://sourceforge.net/projects/zk1/files/ZK/ sourceforge])
 +
* Execute the following command under your project folder to install ZK-Less' node package.
 +
<source lang=bash>
 +
cd project_folder
 +
npm install zkless-engine
 +
</source>
 
* Write an ant script '''''build.xml''''' file like the following sample under project folder, and change the input folder and output folder as needed.
 
* Write an ant script '''''build.xml''''' file like the following sample under project folder, and change the input folder and output folder as needed.
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0"?>
 
<?xml version="1.0"?>
 
<project name="less.compile" default="lessc" basedir=".">
 
<project name="less.compile" default="lessc" basedir=".">
    <target name="lessc">
+
    <target name="lessc">
        <java classname="CpLess" fork="true">
+
        <exec executable="node">
            <arg value="${basedir}/src/main/webapp/less"/><!-- input folder that contains less file -->
+
            <!-- location of the engine's core file -->
            <arg value="${basedir}/src/main/webapp/less"/><!-- output folder -->
+
            <arg value="${basedir}/node_modules/zkless-engine/lib/CpLess.js"/>
            <classpath>
+
            <!-- input folder that contains less files-->
                <pathelement location="${basedir}/lib/commons-logging.jar"/>
+
            <arg value="${basedir}/src/main/webapp"/>
                <pathelement location="${basedir}/lib/js.jar"/>
+
            <!-- output folder -->
                <pathelement location="${basedir}/lib/lesscss-engine.jar"/>
+
            <arg value="${basedir}/src/main/webapp"/>
                <pathelement location="${basedir}/lib/yuicompressor.jar"/>
+
            <!-- path of zul.jar -->
                <pathelement location="${basedir}/lib/zkjszips.jar"/>
+
            <arg value="${basedir}/lib/zul.jar"/>
                <pathelement location="${basedir}/lib/zkless.jar"/>
+
        </exec>
                <pathelement location="${basedir}/lib/zul.jar"/><!-- only needed if using _zkmixins.less -->
+
        <!-- compress the result using zk's Css Compressor -->
            </classpath>
+
        <java classname="CompressCss" fork="true">
        </java>
+
            <!-- input folder (same as above) -->
    </target>
+
            <arg value="${basedir}/src/main/webapp"/>
 +
            <!-- output folder (same as above) -->
 +
            <arg value="${basedir}/src/main/webapp"/>
 +
            <classpath>
 +
                <!-- required jars -->
 +
                <pathelement location="${basedir}/lib/zkjszips.jar"/>
 +
                <pathelement location="${basedir}/lib/yuicompressor.jar"/>
 +
                <pathelement location="${basedir}/lib/commons-io.jar"/>
 +
                <pathelement location="${basedir}/lib/CssCompressor.jar"/>
 +
            </classpath>
 +
        </java>
 +
    </target>
 
</project>
 
</project>
 
</source>
 
</source>
* Execute '''<tt>ant</tt>''' command in command line tool.
+
* Execute '''<tt>ant</tt>''' command in command line tool, i.e:
 +
<source lang=bash>
 +
cd project_folder
 +
ant lessc
 +
</source>
  
 
== Compile LESS by Maven ==
 
== Compile LESS by Maven ==
 
+
* Have Node.js installed in your environment.
 +
* Execute the following command to install the native LESS engine by [http://lesscss.org/ Less.js]
 +
<source lang=bash>
 +
npm install -g less
 +
</source>
 
* Modify pom.xml in maven project
 
* Modify pom.xml in maven project
 
<source lang="xml">
 
<source lang="xml">
 
<!-- Add Plugin Repository -->
 
<!-- Add Plugin Repository -->
 
<pluginRepositories>
 
<pluginRepositories>
<pluginRepository>
+
    <pluginRepository>
<id>zkmaven</id>
+
        <id>zkmaven</id>
<name>ZK Maven Plugin Repository</name>
+
        <name>ZK Maven Plugin Repository</name>
<url>http://mavensync.zkoss.org/maven2/</url>
+
        <url>http://mavensync.zkoss.org/maven2/</url>
</pluginRepository>
+
    </pluginRepository>
 
</pluginRepositories>
 
</pluginRepositories>
 
<dependencies>
 
<dependencies>
<!-- only needed if using _zkmixins.less provided by ZK -->
+
    <!-- only needed if using _zkmixins.less provided by ZK -->
<dependency>
+
    <dependency>
<groupId>org.zkoss.zk</groupId>
+
        <groupId>org.zkoss.zk</groupId>
<artifactId>zul</artifactId>
+
        <artifactId>zul</artifactId>
<version>7.0.0</version>
+
        <version>7.0.3</version>
</dependency>
+
    </dependency>
 
</dependencies>
 
</dependencies>
 
<build>
 
<build>
<plugins>
+
    <plugins>
<!-- Add zkless-engine-maven-plugin -->
+
        <!-- Add zkless-engine-maven-plugin -->
<plugin>
+
        <plugin>
<groupId>org.zkoss.maven</groupId>
+
            <groupId>org.zkoss.maven</groupId>
<artifactId>zkless-engine-maven-plugin</artifactId>
+
            <artifactId>zkless-engine-maven-plugin</artifactId>
<version>0.8.2</version>
+
            <version>0.9.6</version>
<executions>
+
            <executions>
<execution>
+
                <execution>
<id>compile-less</id>
+
                    <id>compile-less</id>
<goals>
+
                    <goals>
<goal>lessc</goal>
+
                        <goal>lessc</goal>
</goals>
+
                    </goals>
<configuration>
+
                    <configuration>
<!-- LESS source folder -->
+
                        <!-- LESS source folder -->
<sourceDirectory>${project.basedir}/src/main/resources</sourceDirectory>
+
                        <sourceDirectory>
<!-- *.CSS.DSP output folder -->
+
                            ${project.basedir}/src/main/resources
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
+
                        </sourceDirectory>
</configuration>
+
                        <!-- *.CSS.DSP output folder -->
</execution>
+
                        <outputDirectory>
</executions>
+
                            ${project.basedir}/src/main/resources
</plugin>
+
                        </outputDirectory>
</plugins>
+
                       <!-- Compress Option, default is true -->
 +
                        <!--  <compress>false</compress> -->
 +
                    </configuration>
 +
                </execution>
 +
            </executions>
 +
        </plugin>
 +
    </plugins>
 
</build>
 
</build>
 
</source>
 
</source>
Line 93: Line 128:
 
= Compile LESS to DSP during Development phase =
 
= Compile LESS to DSP during Development phase =
  
It is not friendly to debug LESS during development by running Ant. Therefore, we provide a servlet named '''ZKLessServlet''' that can be used in web project to develop LESS by simply refreshing the browser page.
+
It is not friendly to debug LESS during development by running Ant or Maven each time you modify your LESS files.  
 +
 
 +
Therefore, we provide a servlet named '''ZKLessServlet''' that can be used in web project to develop LESS by simply refreshing the browser page.
  
 
'''Note:''' This is only recommended in development environment.
 
'''Note:''' This is only recommended in development environment.
  
 
Steps to use '''ZKLessServlet''' within a Maven Project:
 
Steps to use '''ZKLessServlet''' within a Maven Project:
 
+
* Have Node.js installed in your environment.
 +
* Install the native LESS engine by the following command:
 +
<source lang=bash>
 +
npm install -g less
 +
</source>
 
* Add maven dependency if you are using maven.
 
* Add maven dependency if you are using maven.
 
<source lang="xml">
 
<source lang="xml">
Line 113: Line 154:
 
         <groupId>org.zkoss.maven</groupId>
 
         <groupId>org.zkoss.maven</groupId>
 
         <artifactId>zkless-servlet</artifactId>
 
         <artifactId>zkless-servlet</artifactId>
         <version>0.8.2</version>
+
         <version>0.9.6</version>
 
     </dependency>
 
     </dependency>
 
     <!-- omitted other dependency -->
 
     <!-- omitted other dependency -->
Line 123: Line 164:
 
<source lang="xml">
 
<source lang="xml">
 
<web-app>
 
<web-app>
    <!-- omitted other servlets -->
+
    <!-- omitted other servlets -->
    <servlet>
+
    <servlet>
        <servlet-name>zkLess</servlet-name>
+
        <servlet-name>zkLess</servlet-name>
        <servlet-class>org.zkoss.less.ZKLessServlet</servlet-class>
+
        <servlet-class>org.zkoss.less.ZKLessServlet</servlet-class>
        <init-param>
+
        <init-param>
            <param-name>org.zkoss.less.LessResource</param-name>
+
            <param-name>org.zkoss.less.LessResource</param-name>
            <param-value>/less</param-value><!-- specify to the folder that contains *.less -->
+
            <!-- specify to the folder that contains *.less -->
        </init-param>
+
            <param-value>/less</param-value>
        <init-param>
+
        </init-param>
            <param-name>org.zkoss.less.OutputFormat</param-name>
+
        <init-param>
            <param-value>.css.dsp</param-value><!-- specify output file suffix, default .css.dsp -->
+
            <param-name>org.zkoss.less.OutputFormat</param-name>
        </init-param>
+
            <!-- specify output file suffix, default .css.dsp -->
        <init-param>
+
            <param-value>.css.dsp</param-value>
            <param-name>org.zkoss.less.CompressOutput</param-name>
+
        </init-param>
            <param-value>true</param-value><!-- compress output, default true -->
+
        <init-param>
        </init-param>
+
            <param-name>org.zkoss.less.CompressOutput</param-name>
        <load-on-startup>1</load-on-startup>
+
            <!-- compress output, default true -->
    </servlet>
+
            <param-value>true</param-value>
    <servlet-mapping>
+
        </init-param>
        <servlet-name>zkLess</servlet-name>
+
        <load-on-startup>1</load-on-startup>
        <url-pattern>/less/*</url-pattern><!-- specify to folder that contains *.less -->
+
    </servlet>
    </servlet-mapping>
+
    <servlet-mapping>
 +
        <servlet-name>zkLess</servlet-name>
 +
        <!-- specify to folder that contains *.less -->
 +
        <url-pattern>/less/*</url-pattern>
 +
    </servlet-mapping>
 
</web-app>
 
</web-app>
 
</source>
 
</source>
  
 
* Project structure and use LESS directly in zul page.
 
* Project structure and use LESS directly in zul page.
 +
: Remember to put all your LESS files and import resources under '''webapp/less''' which is the folder specified in web.xml
 
: Project structure should look like the following:  
 
: Project structure should look like the following:  
  
Line 162: Line 208:
 
</source>
 
</source>
  
: Use test.less inside test.zul as follows
+
* Use test.less inside test.zul as follows
  
 
<source lang="xml" high="2">
 
<source lang="xml" high="2">

Latest revision as of 01:50, 13 January 2015


Install Node.js

Node.js is now required for our ZK-Less Engine since ZK 7.0.3.

If you don't have Node.js installed yet, please follow the steps given by the official-website to install Node.js.

Node version 0.10.30 or above is required. To check your Node.js version, simply run:

node --version

Compile LESS to DSP

Here we introduce two ways to compile LESS files to DSP files. One is by Ant and the other is by Maven.

You could choose either one of them depends on your preferences.

Compile LESS by Ant

  • Have Node.js installed in your environment.
  • Follow the online document to install Apache Ant.
  • Download the following necessary jars into your project/lib folder from zk repository (right click -> save as).
    • yuicompressor.jar
    • zkjszips.jar
    • commons-io.jar
    • CssCompressor.jar
    • zul.jar (extract zk-bin-7.0.X.zip file from sourceforge)
  • Execute the following command under your project folder to install ZK-Less' node package.
cd project_folder
npm install zkless-engine
  • Write an ant script build.xml file like the following sample under project folder, and change the input folder and output folder as needed.
<?xml version="1.0"?>
<project name="less.compile" default="lessc" basedir=".">
    <target name="lessc">
        <exec executable="node">
            <!-- location of the engine's core file -->
            <arg value="${basedir}/node_modules/zkless-engine/lib/CpLess.js"/>
            <!-- input folder that contains less files-->
            <arg value="${basedir}/src/main/webapp"/>
            <!-- output folder -->
            <arg value="${basedir}/src/main/webapp"/>
            <!-- path of zul.jar -->
            <arg value="${basedir}/lib/zul.jar"/>
        </exec>
        <!-- compress the result using zk's Css Compressor -->
        <java classname="CompressCss" fork="true">
            <!-- input folder (same as above) -->
            <arg value="${basedir}/src/main/webapp"/>
            <!-- output folder (same as above) -->
            <arg value="${basedir}/src/main/webapp"/>
            <classpath>
                <!-- required jars -->
                <pathelement location="${basedir}/lib/zkjszips.jar"/>
                <pathelement location="${basedir}/lib/yuicompressor.jar"/>
                <pathelement location="${basedir}/lib/commons-io.jar"/>
                <pathelement location="${basedir}/lib/CssCompressor.jar"/>
            </classpath>
        </java>
    </target>
</project>
  • Execute ant command in command line tool, i.e:
cd project_folder
ant lessc

Compile LESS by Maven

  • Have Node.js installed in your environment.
  • Execute the following command to install the native LESS engine by Less.js
npm install -g less
  • Modify pom.xml in maven project
<!-- Add Plugin Repository -->
<pluginRepositories>
    <pluginRepository>
        <id>zkmaven</id>
        <name>ZK Maven Plugin Repository</name>
        <url>http://mavensync.zkoss.org/maven2/</url>
    </pluginRepository>
</pluginRepositories>
<dependencies>
    <!-- only needed if using _zkmixins.less provided by ZK -->
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zul</artifactId>
        <version>7.0.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!-- Add zkless-engine-maven-plugin -->
        <plugin>
            <groupId>org.zkoss.maven</groupId>
            <artifactId>zkless-engine-maven-plugin</artifactId>
            <version>0.9.6</version>
            <executions>
                <execution>
                    <id>compile-less</id>
                    <goals>
                        <goal>lessc</goal>
                    </goals>
                    <configuration>
                        <!-- LESS source folder -->
                        <sourceDirectory>
                            ${project.basedir}/src/main/resources
                        </sourceDirectory>
                        <!-- *.CSS.DSP output folder -->
                        <outputDirectory>
                            ${project.basedir}/src/main/resources
                        </outputDirectory>
                        <!-- Compress Option, default is true -->
                        <!--  <compress>false</compress> -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Execute mvn install command to trigger LESS compilation.

Compile LESS to DSP during Development phase

It is not friendly to debug LESS during development by running Ant or Maven each time you modify your LESS files.

Therefore, we provide a servlet named ZKLessServlet that can be used in web project to develop LESS by simply refreshing the browser page.

Note: This is only recommended in development environment.

Steps to use ZKLessServlet within a Maven Project:

  • Have Node.js installed in your environment.
  • Install the native LESS engine by the following command:
npm install -g less
  • Add maven dependency if you are using maven.
<repositories>
    <repository>
        <id>ZK CE</id>
        <name>ZK CE Repository</name>
        <url>http://mavensync.zkoss.org/maven2</url>
    </repository>
    <!-- omitted other repository -->
</repositories>
<dependencies>
    <dependency>
        <groupId>org.zkoss.maven</groupId>
        <artifactId>zkless-servlet</artifactId>
        <version>0.9.6</version>
    </dependency>
    <!-- omitted other dependency -->
</dependencies>
  • Add servlet settings in web.xml
<web-app>
    <!-- omitted other servlets -->
    <servlet>
        <servlet-name>zkLess</servlet-name>
        <servlet-class>org.zkoss.less.ZKLessServlet</servlet-class>
        <init-param>
            <param-name>org.zkoss.less.LessResource</param-name>
            <!-- specify to the folder that contains *.less -->
            <param-value>/less</param-value>
        </init-param>
        <init-param>
            <param-name>org.zkoss.less.OutputFormat</param-name>
            <!-- specify output file suffix, default .css.dsp -->
            <param-value>.css.dsp</param-value>
        </init-param>
        <init-param>
            <param-name>org.zkoss.less.CompressOutput</param-name>
            <!-- compress output, default true -->
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLess</servlet-name>
        <!-- specify to folder that contains *.less -->
        <url-pattern>/less/*</url-pattern>
    </servlet-mapping>
</web-app>
  • Project structure and use LESS directly in zul page.
Remember to put all your LESS files and import resources under webapp/less which is the folder specified in web.xml
Project structure should look like the following:
zkMavenWebProject - src/main/webapp
    WEB-INF
        web.xml
        zk.xml
    less
        test.less
    pages
        test.zul
  • Use test.less inside test.zul as follows
<!-- test.zul -->
<?link rel="stylesheet" href="../less/test.less"?>
<zk>
    <button label="test" />
</zk>
  • Now you can modify LESS and see the result by refreshing your browser page.

Version History

Last Update : 2015/01/13


Version Date Content
     


Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.