How to Use Custom Module from One Workspace to Another Workspace's Custom Module in Liferay 7.4

blog-banner

Moving custom modules between workspaces in Liferay 7.4 simplifies the development process by allowing for better code reuse and organization. This blog post will walk you through creating, exporting, and integrating a custom module from one workspace to another, making your projects more efficient and manageable.

What are Liferay workspace and modules?

A Liferay workspace is an organized environment that Liferay provides to make it easier to build, launch, and manage Liferay projects. It divides projects into predetermined folders for modules, themes, and other components, making the development process easier. Liferay modules are small, self-contained blocks of functionality that may be independently built, tested, and deployed. They use the OSGi (Open Services Gateway Initiative) framework, which allows for dynamic module management and improves the modularity and flexibility of applications on the Liferay platform.

What are the prerequisites?

  • Liferay DXP/Portal 7.4
  • Basic knowledge of Liferay
  • Liferay Developer Studio or Blade CLI
  • Understanding of Liferay workspaces
  • Experience with creating and deploying Liferay modules

How to Use Custom Module from One Workspace to Another Workspace's Custom Module?

We can use our custom module in other workspace modules using the following two methods:

  • Export the module jar and include it with the build gradle.
  • Using the maven repository.

Export the module jar and include it with the build gradle

We have an original workspace module that we want to utilize in another workspace module. For example, I have the lr-training workspace module lr-common-service that I want to use in the training-workspace doctor-web.

workspace-1 workspace-2

You must first build the original module (lr-common-service jar) and create the jar, then create a libs folder in the doctor-web module home path, as indicated in the image below, and add the lr-common-service jar.

module-libs

Now, as indicated in the image below, use build gradle to add the external module jar.

build-gradle-external-jar

We have added all of the external jars indicated in the doctor-web module's libs folder, allowing us to use the lr-common-service module's dependencies in the same way as we use the LanguageCommonUtil class, as shown in the image below.

external-module-class

Using the maven repository

We must set up the Liferay plugins workspace to use the Maven repository.

To build a Liferay plugins workspace, we must first construct a default Liferay workspace and delete several unnecessary Liferay folders and files such as configs, modules, themes, gradle.properties, and gradle-local.properties, as illustrated in the figure below.

plugins-workspace

We must upgrade the build.gradle and its settings.gradle files for creating a Liferay plugin workspace.

In the settings.gradle file, define rootProject.name and which modules you wish to include in the Liferay plugin workspace, as seen in the image below.

settings-gradle

In the build.gradle file, define the buildscript and subprojects as seen in the photos below.

build-script sub-projects

To use the maven repository, specify it in the workspace build.gradle file, as shown in the image above. I used a local Maven repository to demonstrate repositories and publishing, but you must use an external Maven repository in your project workspace, as seen in the image below.

external-maven-repository

Once your workspace is set up, you can develop modules, and the modules you create must be included in the settings.gradle file for workspace.

In your module build.gradle, you must define publishing so that when you build the module, it will be added to the maven repository that you have defined, as seen in the image below.

module-build-gradle

You can publish your module to a remote maven repository with the gradlew publish command, or to a local maven repository with the gradlew publishToMavenLocal command.

I used the above-mentioned command to publish the lr-common-service module for the local maven repository, and its jar was added to m2, the local maven repository, as shown in the image below.

local-maven-repository

As indicated in the image below, I added the lr-common-service from the local maven repository to the doctor-web module build.gradle file.

include-local-maven-repository-jar

We can now use the dependencies of the lr-common-service module in the doctor-web module, as seen in the image below.

external-maven-class.

Note: We can add external modules to our workspace using either of the techniques listed above, however we recommend using the Maven repository method rather than manually adding jars to the project.

Conclusion

Using custom modules between Liferay workspaces is a useful way to improve development productivity and code management. Developers can speed project workflows, minimize redundancy, and improve dependency organization by integrating modules directly via Gradle or the Maven repository. This technique not only makes module reuse easier, but it also supports scalable and maintainable solutions in Liferay 7.4, which increases overall Liferay development productivity and project consistency.

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium