How to Create a Command for Spring Shell

1) Create the command class:

package br.eti.fernandoribeiro.sample;

import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.stereotype.Component;

@Component
public final class SampleCommands implements CommandMarker {

  @CliCommand(value = "sample do-something", help = "Does something")
  public void doSomething() {
    ...
  }

}

2) Create the META-INF/spring/spring-shell-plugin.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  <context:component-scan base-package="br.eti.fernandoribeiro.sample" />
</beans>

3) Build the archive.

4) Drop the archive in the class path.

5) Run the command:

$ sample do-something

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s