Cookies help us deliver our services.

By using our services, you agree to our use of cookies. Learn more

I understand

In a previous article I explained How to implement a custom Gogo shell command for Liferay 7; in this new article I will explain some other useful information to refine your custom command.

So let's start from the same project we have seen in the other article but first of all, make sure you have the following dependencies in the build.gradle file:

dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    compileOnly group: "org.apache.felix", name: "org.apache.felix.gogo.runtime", version: "1.0.6"
    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
}

And the following import in the bnd.bnd file:

[...]
Import-Package: \
org.apache.felix.service.command;status=provisional;version='[0.10,1)',\
*

We have just seen how to declare the custom command through Java annotations:

package it.marconapolitano.liferay.gogo.command;

@Component(property = {
    "osgi.command.function=company", "osgi.command.scope=liferay"
}, service = Object.class)
public class CompanyCommand {
}

The first thing I'll show is how you can specify more than one command, simply by repeating the osgi.command.function property and implementing the Java methods as we have already seen:

package it.marconapolitano.liferay.gogo.command;

@Component(property = {
    "osgi.command.function=company", "osgi.command.function=company2",
    "osgi.command.scope=liferay"
}, service = Object.class)
public class CompanyCommand {
public void company() {
// ...
}

public void company(long companyId) {
// ...
}

public void company2() {
// ...
}
}

Now if you connect to the Gogo shell and type the help company command you will get something not too clear like this:

company
   scope: liferay
parameters:
long

company
scope: liferay

If you want to better describe your methods and their parameters, you need to use the Descriptor annotation as shown below:

package it.marconapolitano.liferay.gogo.command;

@Component(property = {
    "osgi.command.function=company", "osgi.command.function=company2",
    "osgi.command.scope=liferay"
}, service = Object.class)
public class CompanyCommand {

@Descriptor("Show the instances list")
public void company() {
// ...
}

@Descriptor("Show the instance details")
public void company(
@Descriptor("Instance ID") long companyId) {
// ...
}
}

Se adesso ci colleghiamo alla Gogo shell e digitiamo il comando help company otterremo qualcosa di molto più parlante come questo:

company - Show the instance details
scope: liferay
parameters:
long Instance ID

company - Show the instances list
scope: liferay