Created
May 14, 2018 10:42
-
-
Save skrymets/2977a05d415801b7656cd9e7c6303764 to your computer and use it in GitHub Desktop.
Enable or disable Spring shell when application is starting up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<dependencies> | |
... | |
<dependency> | |
<groupId>commons-cli</groupId> | |
<artifactId>commons-cli</artifactId> | |
<version>1.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.shell</groupId> | |
<artifactId>spring-shell-starter</artifactId> | |
<version>2.0.0.RELEASE</version> | |
</dependency> | |
... | |
</dependencies> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.commons.cli.CommandLine; | |
import org.apache.commons.cli.DefaultParser; | |
import org.apache.commons.cli.Option; | |
import org.apache.commons.cli.Options; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.shell.jline.InteractiveShellApplicationRunner; | |
import org.springframework.shell.jline.ScriptShellApplicationRunner; | |
@SpringBootApplication | |
public class ShellApplication { | |
private static final Logger LOG = LoggerFactory.getLogger(EqueueApplication.class); | |
public static void main(String[] args) { | |
Option shellOption = Option.builder("shell").desc("Run interactive shell").build(); | |
Options options = new Options().addOption(shellOption); | |
CommandLine cmd; | |
try { | |
cmd = new DefaultParser().parse(options, args); | |
} catch (Exception e) { | |
LOG.error(e.getMessage()); | |
return; | |
} | |
SpringApplication app = new SpringApplication(ShellApplication.class); | |
Map<String, Object> properties = new HashMap<>(); | |
final boolean runShell = cmd.hasOption(shellOption.getOpt()); | |
properties.put(InteractiveShellApplicationRunner.SPRING_SHELL_INTERACTIVE_ENABLED, runShell); | |
properties.put(ScriptShellApplicationRunner.SPRING_SHELL_SCRIPT_ENABLED, runShell); | |
if (runShell) { | |
// All @ShellComponent (s) may be marked with this @Profile(CDI.Profiles.SHELL) | |
app.setAdditionalProfiles(CDI.Profiles.SHELL); | |
} | |
app.setDefaultProperties(properties); | |
app.run(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment