Register Custom Gogoshell Commands In Liferay

Customizations are the essence of Liferay, and Gogoshell can likewise be tailored to your specifications.

28 Mar 2024

Felix Gogo shell basic commands and Liferay instructions are executed by the Gogo shell. Both the command line and the Control Panel provide access to the Gogo shell. There are numerous built-in Liferay commands that we can utilize in the Gogo shell. Have you considered using a custom Gogoshell command on Liferay, though?

Yes, you can create one component class in Liferay to define custom Gogo shell commands.

Here you go.

  • Include the dependency listed below in your module project.

    compileOnly group: "com.liferay", name: "org.apache.felix.gogo.runtime"

  • For the custom Gogo shell command, create a component class.

  • Add the component declaration below.

    @Component( property = {"osgi.command.function = reloadPickListGogoShellCommand", "osgi.command.scope=picklist"}, service = OSGiCommands.class )
    • where "osgi.command.scope" denotes the command's scope and "osgi.command.function" is the name of the custom Gogo shell command.
  • Make sure the "OSGiCommands" class is implemented in your class.

    public class ReloadPickListGogoShellCommand implements OSGiCommands {
  • For your function, now construct a method. We'll create the "reloadPickListGogoShellCommand" method in our example

    @Descriptor("Custom Gogo shell command")

    public String reloadPickListGogoShellCommand(long bundleId) throws Exception {

    _log.info("Command reloadPickListGogoShellCommand executed");

    return "SUCCESS";

    }

  • Your class should now appear as follows:

    import org.apache.felix.service.command.Descriptor;

    import org.osgi.service.component.annotations.Component;

    import com.liferay.osgi.util.osgi.commands.OSGiCommands;

    import com.liferay.portal.kernel.log.Log;

    import com.liferay.portal.kernel.log.LogFactoryUtil;

    @Component(

    property = {"osgi.command.function=reloadPickListGogoShellCommand", "osgi.command.scope=picklist"},

    service = OSGiCommands.class

    )

    public class ReloadPickListGogoShellCommand implements OSGiCommands {

    private static final Log _log = LogFactoryUtil.getLog(ReloadPickListGogoShellCommand.class);

    @Descriptor("Update database indexes for a specific module via bundle ID")

    public String reloadPickListGogoShellCommand(long bundleId) throws Exception {

    _log.info("Command reloadPickListGogoShellCommand executed");

    return "SUCCESS";

    }

    }

  • Great, go ahead and deploy this module. Use the gogo shell to carry out the command.

  • It is now ready for you to use by writing your logic into a method.