Gradle workspace: dependencies and modules management

Gradle workspace: dependencies and modules management

Overview

This article explains how to manage dependencies in your Gradle workspace project, use global dependencies, define configuration, and filter sub-modules.

Dependencies management

If you have a multi-module project - you probably have a lot of common dependencies in your modules, and you want to reuse them from a single place. This way you'll not need to update a single dependency version in tens of modules, but just update it in one place.
The dependencies in your modules can be different:
  • Liferay dependencies - those ones, exposed by Liferay;
  • Third-party dependencies - not defined in Liferay.
And approach is different for those types.
For Liferay dependencies - you can enable The Target Platform.
For third-party ones - you may use global dependencies.

Global dependencies

You may define global dependencies in the root of your workspace, and then reuse them in different modules.
Create file dependencies.gradle in the root folder of workspace, sample:
ext {
    versions = [
        javax  : [
                portletApi                          : "2.0",
                servletApi                          : "3.0.1"
        ],
        jstl                                        : "1.2"
    ]
    globalDependency = [
            jstl                                    : "jstl:jstl:${versions.jstl}",
            portletApi                              : "javax.portlet:portlet-api:${versions.javax.portletApi}",
            servletApi                              : "javax.servlet:javax.servlet-api:${versions.javax.servletApi}"
    ]
}
Here we defined global dependencies for JSTL, Portlet API and Servlet API.
Everything under ext is available for the Gradle context. That's why you can use syntax like ${versions.jstl}.
Add the following line:
apply from: "dependencies.gradle"
to the root's build.gradle file to import the global dependencies.
Now you may re-use defined global dependencies in yout modules.
For example, you can replace
compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
with
compileOnly globalDependency.portletApi

Modules management

Define module roots

You can define folders, which are the module roots for your modules, themes and wars in root gradle.properties file. Default values are:
liferay.workspace.modules.dir=modules
liferay.workspace.themes.dir=themes
liferay.workspace.wars.dir=wars
but you can override them, if needed.
These folders and all sub-folders (recursively) will be imported as Gradle modules.
You can also specify several folders, for example:
liferay.workspace.modules.dir=modules,modules-bkp

Exclude Gradle submodules

You may need to exclude some modules from Gradle deployment: during development you may have some demo modules, which you don't want to build with the main Gradle tasks.
With Maven you can specify the explicit modules set for deployment. But there is no way to include/exclude specific modules in Gradle: by default all folders inside modules/themes/wars folders are imported as Gradle modules.
However, you can define sub-modules filtering in parent build.gradle file, sample:
subprojects { subproject ->
    def project = subproject.project
    def projectName = project.name
    if (projectName.contains("demo")) {
        project.tasks.all { task -> task.enabled = false }
    }
}
In this example for all modules containing "demo" in the name - all tasks will be disabled. This way you may exclude specific modules from deployment.
Enjoy 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration