How to Create a Command for Spring Shell

1) Create the command class:

package com.wordpress.simplesassim.shell;

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

@Component
public 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="com.wordpress.simplesassim.shell" />
</beans>

3) Build the archive.

4) Drop the archive in the class path.

5) Run the command:

$ sample do-something

Published by:

Fernando Ribeiro

Experienced tech executive with a 24-year track record in enterprise computing. Leading AWS's professional services application modernization division in Brazil. He's held diverse roles including management, solutions architecture, sales consultancy, and full-stack development at major players like Oracle, Red Hat, and IBM. Fernando also contributes to open source and writes about emerging technologies. The views expressed here are his own and do not necessarily reflect the views of AWS.

Categories SoftwareTags , , Leave a comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.