Soy Gradle Plugin

The Soy Gradle plugin lets you compile Closure Templates into JavaScript functions. It also lets you use a custom localization mechanism in the generated .soy.js files by replacing goog.getMsg definitions with a different function call (e.g., Liferay.Language.get).

The plugin has been successfully tested with Gradle 4.10.2.

Usage

To use the plugin, include it in your build script:

buildscript {
    dependencies {
        classpath group: "com.liferay", name: "com.liferay.gradle.plugins.soy", version: "3.1.8"
    }

    repositories {
        maven {
            url "https://repository-cdn.liferay.com/nexus/content/groups/public"
        }
    }
}

There are two Soy Gradle plugins you can apply to your project:

  • Apply the Soy Plugin to compile Closure Templates into JavaScript functions:

    apply plugin: "com.liferay.soy"
    
  • Apply the Soy Translation Plugin to use a custom localization mechanism in the generated .soy.js files:

    apply plugin: "com.liferay.soy.translation"
    

Since the Soy Gradle plugin automatically resolves the Soy library as a dependency, you have to configure a repository that hosts the library and its transitive dependencies. The Liferay CDN repository hosts them all:

repositories {
    maven {
        url "https://repository-cdn.liferay.com/nexus/content/groups/public"
    }
}

Soy Plugin

The Soy plugin adds two tasks to your project:

NameDepends OnTypeDescription
buildSoy-BuildSoyTaskCompiles Closure Templates into JavaScript functions.
wrapSoyAlloyTemplate- configJSModules if com.liferay.js.module.config.generator is applied
- processResources if java is applied
- transpileJS if com.liferay.js.transpiler is applied
WrapSoyAlloyTemplateTaskWraps the JavaScript functions compiled from Closure Templates into AlloyUI modules.

The plugin also adds the following dependencies to tasks defined by the java plugin:

NameDepends On
classeswrapSoyAlloyTemplate

The buildSoy task is automatically configured with sensible defaults, depending on whether the java plugin is applied:

Property NameDefault Value
includes["**/*.soy"]
source

If the java plugin is applied: The first resources directory of the main source set (by default, src/main/resources).

Otherwise: []

The wrapSoyAlloyTemplate task is disabled by default, and it is automatically configured with sensible defaults, depending on whether the java plugin is applied:

Property NameDefault Value
enabledfalse
includes["**/*.soy.js"]
source

If the java plugin is applied: project.sourceSets.main.output.resourcesDir

Otherwise: []

Additional Configuration

There are additional configurations that can help you use the Soy library.

Soy Dependency

By default, the plugin creates a configuration called soy and adds a dependency to the 2015-04-10 version of the Soy library. It is possible to override this setting and use a specific version of the tool by manually adding a dependency to the soy configuration:

dependencies {
    soy group: "com.google.template", name: "soy", version: "2015-04-10"
}

Soy Translation Plugin

The Soy Translation plugin adds one task to your project:

NameDepends OnTypeDescription
replaceSoyTranslation- configJSModules if com.liferay.js.module.config.generator is applied
- processResources if java is applied
- transpileJS if com.liferay.js.transpiler is applied
ReplaceSoyTranslationTaskReplaces goog.getMsg definitions with Liferay.Language.get calls.

The plugin also adds the following dependencies to tasks defined by the java plugin:

NameDepends On
classesreplaceSoyTranslation

The replaceSoyTranslation task is automatically configured with sensible defaults, depending on whether the java plugin is applied:

Property NameDefault Value
includes["**/*.soy.js"]
replacementClosureReplaces goog.getMsg definitions with Liferay.Language.get calls.
source

If the java plugin is applied: project.sourceSets.main.output.resourcesDir

Otherwise: []

Tasks

BuildSoyTask

Tasks of type BuildSoyTask extend SourceTask, so all its properties and methods, such as include and exclude, are available.

Task Properties

Property NameTypeDefault ValueDescription
classpathFileCollectionproject.configurations.soyThe classpath for executing the Liferay Portal Tools Soy Builder.

WrapSoyAlloyTemplateTask

Tasks of type WrapSoyAlloyTemplateTask extend SourceTask, so all its properties and methods, such as include and exclude, are available.

Task Properties

Property NameTypeDefault ValueDescription
moduleNameStringnullThe name of the AlloyUI module.
namespaceStringnullThe namespace of the Closure Templates of the project.

It is possible to use Closures and Callables as values for the String properties to defer evaluation until task execution.

ReplaceSoyTranslationTask

The ReplaceSoyTranslationTask task type finds all the goog.getMsg definitions in the project’s files and replaces them with a custom function call.

var MSG_EXTERNAL_123 = goog.getMsg('welcome-to-{$releaseInfo}', { 'releaseInfo': opt_data.releaseInfo });

A goog.getMsg definition looks like the example above, and it has the following components:

  • variable name: MSG_EXTERNAL_123
  • language key: welcome-to-{$releaseInfo}
  • arguments object: { 'releaseInfo': opt_data.releaseInfo }

Tasks of type ReplaceSoyTranslationTask extend SourceTask, so all its properties and methods, such as include and exclude, are available.

Task Properties

Property NameTypeDefault ValueDescription
replacementClosureClosure<String>nullThe Closure invoked in order to get the replacement for goog.getMsg definitions. The given Closure is passed the variable name, language key, and arguments object as its parameters.
« Source Formatter Gradle PluginTarget Platform Gradle Plugin »
Was this article helpful?
0 out of 0 found this helpful