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:
Name | Depends On | Type | Description |
---|---|---|---|
buildSoy | - | BuildSoyTask | Compiles 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 | WrapSoyAlloyTemplateTask | Wraps the JavaScript functions compiled from Closure Templates into AlloyUI modules. |
The plugin also adds the following dependencies to tasks defined by the java
plugin:
Name | Depends On |
---|---|
classes | wrapSoyAlloyTemplate |
The buildSoy
task is automatically configured with sensible defaults,
depending on whether the java
plugin is applied:
Property Name | Default Value |
---|---|
includes | ["**/*.soy"] |
source | If the 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 Name | Default Value |
---|---|
enabled | false |
includes | ["**/*.soy.js"] |
source | If the 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:
Name | Depends On | Type | Description |
---|---|---|---|
replaceSoyTranslation | - configJSModules if com.liferay.js.module.config.generator is applied - processResources if java is applied - transpileJS if com.liferay.js.transpiler is applied | ReplaceSoyTranslationTask | Replaces goog.getMsg definitions with Liferay.Language.get calls. |
The plugin also adds the following dependencies to tasks defined by the java
plugin:
Name | Depends On |
---|---|
classes | replaceSoyTranslation |
The replaceSoyTranslation
task is automatically configured with sensible
defaults, depending on whether the java
plugin is applied:
Property Name | Default Value |
---|---|
includes | ["**/*.soy.js"] |
replacementClosure | Replaces goog.getMsg definitions with Liferay.Language.get calls. |
source | If the 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 Name | Type | Default Value | Description |
---|---|---|---|
classpath | FileCollection | project.configurations.soy | The 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 Name | Type | Default Value | Description |
---|---|---|---|
moduleName | String | null | The name of the AlloyUI module. |
namespace | String | null | The 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.