Compiling a Client-Side Module

A client-side module, either a Vaadin widget set or a pure client-side module, needs to be compiled to JavaScript using the Vaadin Client Compiler.

Widget set compilation is most often needed when using add-ons. In that case, the widget sets from different add-ons are compiled into an application widget set, as described in “Using Vaadin Add-ons”.

When doing client-side development, you need to compile the widget set every time you modify the client-side code.

Vaadin Compiler Overview

The Vaadin Client Compiler compiles Java to JavaScript. It is provided as the executable vaadin-client-compiler JAR. It requires the vaadin-client JAR, which contains the DefaultWidgetSet, Vaadin client-side framework.

The compiler compiles a client module, which can either be a pure client-side module or a Vaadin widget set, that is, the Vaadin Client-Side Engine that includes the widgets used in the application. The client module is defined with a module descriptor, which was described in “Client-Side Module Descriptor”. The module descriptor for application widget sets is automatically generated.

While you can compile client modules individually, in Vaadin applications you normally combine them in one application widget set. The application widget set includes any add-on and custom widgets. The compiler scans the class path for any widget sets to include in the application widget set.

Compilation Result

The compiler writes the compilation result to a target folder that will include the compiled JavaScript with any static resources included in the module.

Compiling in Maven Projects

The Vaadin Maven Plugin, which is enabled in all Vaadin archetypes, makes Maven to automatically compile the widget set when necessary.

Plugin Configuration

XML

  1. <plugin>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-maven-plugin</artifactId>
  4. <version>${vaadin.plugin.version}</version>
  5. <configuration>
  6. <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
  7. <webappDirectory>${basedir}/target/classes/VAADIN/widgetsets</webappDirectory>
  8. <draftCompile>false</draftCompile>
  9. <compileReport>false</compileReport>
  10. <style>OBF</style>
  11. <strict>true</strict>
  12. </configuration>
  13. <executions>
  14. <execution>
  15. <goals>
  16. <goal>update-theme</goal>
  17. <goal>update-widgetset</goal>
  18. <goal>compile</goal>
  19. <goal>compile-theme</goal>
  20. </goals>
  21. </execution>
  22. </executions>
  23. </plugin>

Compilation Modes

The Vaadin Maven Plugin can compile widget sets either locally or online by using a cloud service. The online compilation requires that all the widget sets are available in certain public Maven repositories. As this is not the case when developing custom widgets, you must use the local mode.

Local compilation is the default mode, so you only need to enable it if you have changed the mode to use the online service.

See “Widget Set Modes” for more information.

Compiling

You can explicitly compile the widget set with the vaadin:compile goal.

On command-line:

  1. $ mvn vaadin:compile

If there is no widget set defined, but you have add-on dependencies that need a custom widget set, the Vaadin Maven plugin will automatically generate a widget set definition for you.

Compiling in Eclipse

When you have the Vaadin Plugin installed in Eclipse, you can simply click the Compile Vaadin Widgetset button in the toolbar.

widgetset compiling toolbar

Widget set compilation button in Eclipse

It will compile the widget set it finds from the project. If the project has multiple widget sets, such as one for custom widgets and another one for the project, you need to select the module descriptor of the widget set to compile before clicking the button.

Compiling with the Vaadin Plugin for Eclipse currently requires that the module descriptor has suffix Widgetset.gwt.xml, although you can use it to compile also other client-side modules than widget sets.

The result is written under WebContent/VAADIN/widgetsets folder.

Compiling with Ant

Consider the following configuration:

XML

  1. <target name="configure">
  2. <!-- Where project source files are located -→
  3. <property name="sources" value="src" />
  4. <!-- Path to root of web application folder -→
  5. <property name="webroot" value="WebContent" />
  6. <!-- Compilation work directory -→
  7. <property name="workdir" value="build/work"/>
  8. </target>

The script assumes the Eclipse project layout with WebContent folder.

The compile-widgetset target invokes the Vaadin Compiler to compile the widget set. The class path includes source folder in case there are custom widgets, compiled server-side classes, and the dependencies resolved with Ivy.

XML

  1. <target name="compile-widgetset" depends="init,resolve">
  2. <java classname="com.google.gwt.dev.Compiler"
  3. failonerror="yes" fork="yes">
  4. <arg value="-war" />
  5. <arg value="${webroot}/VAADIN/widgetsets" />
  6. <arg value="${widgetset}" />
  7. <arg value="-logLevel"/>
  8. <arg value="INFO"/>
  9. <!-- <arg value="-strict"/> -->
  10. <jvmarg value="-Xmx1024M"/>
  11. <jvmarg value="-Xss512M"/>
  12. <jvmarg value="-Djava.awt.headless=true"/>
  13. <classpath>
  14. <!-- Location of source code -->
  15. <pathelement path="${sources}" />
  16. <!-- Compiled server-side classes -->
  17. <pathelement path="${workdir}/WEB-INF/classes" />
  18. <!-- Dependencies retrieved with Ivy -->
  19. <path refid="ivy.deps.widgetset"/>
  20. </classpath>
  21. <sysproperty key="vFailIfNotSerializable" value="${failifnotserializable}" />
  22. </java>
  23. <!-- Cleanup -->
  24. <delete dir="${webroot}/VAADIN/gwt-unitCache"/>
  25. <delete dir="${webroot}/VAADIN/widgetsets/WEB-INF"/>
  26. </target>

You can find the complete example build script for compiling widget sets with Ant and Ivy in the build.xml for the Book Examples. You can copy the build script to your project and, once configured, run it with Ant. You may need to do some configuration in the build targets, such as to exclude or include source or target paths.