Spring官網看到SpringShell,本身最近探索了一下,寫個簡單的HelloWorldjava
注:我參考的是官方的sample,地址:https://github.com/spring-projects/spring-shell/tree/master/samples/helloworld,能夠clone到本地本身運行一下它的HelloWorld。git
個人開發環境: windows7 64bitgithub
開發工具: eclipse luna、mavenspring
廢話很少說:亮代碼,以下:shell
總體結構:apache
pom.xml:windows
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mjduan</groupId> <artifactId>LearnSpringShell2</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <name>LearnSpringShell2 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.shell.version>1.2.0.RELEASE</spring.shell.version> <jar.mainclass>org.springframework.shell.Bootstrap</jar.mainclass> <log4j.version>1.2.17</log4j.version> <junit.version>4.10</junit.version> </properties> <dependencies> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell</artifactId> <version>${spring.shell.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>LearnSpringShell2</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>true</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVersions>false</useUniqueVersions> <classpathPrefix>lib/</classpathPrefix> <mainClass>${jar.mainclass}</mainClass> </manifest> <manifestEntries> <version>${project.version}</version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> </project>
log4j.properties:app
#讓log4j不輸出日誌,否則屏幕上全是日誌信息 log4j.rootCategory=OFF
spring-shell-plugin.xml:eclipse
<?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.xsd"> <context:component-scan base-package="com.mjduan" /> </beans>
Main.java:maven
package com.mjduan.learnSpringShell; import java.io.IOException; import org.springframework.shell.Bootstrap; public class Main { public static void main(String[] args) throws IOException { Bootstrap.main(args); } }
MyPromptProvider.java:
package com.mjduan.learnSpringShell.commands; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.shell.plugin.support.DefaultPromptProvider; import org.springframework.stereotype.Component; @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class MyPromptProvider extends DefaultPromptProvider{ @Override public String getPrompt() { return "mingJun@duan>"; } @Override public String getProviderName() { return "my prompt provider"; } }
MyHistoryFileNameProvider.java:
package com.mjduan.learnSpringShell.commands; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider; import org.springframework.stereotype.Component; @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class MyHistoryFileNameProvider extends DefaultHistoryFileNameProvider{ //會在當面目錄下新建日誌文件 @Override public String getHistoryFileName() { return "myLearnSpringShell.log"; } @Override public String getProviderName() { return "My History file name provider"; } }
MyBannerProvider.java:
package com.mjduan.learnSpringShell.commands; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.shell.plugin.support.DefaultBannerProvider; import org.springframework.shell.support.util.OsUtils; import org.springframework.stereotype.Component; @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class MyBannerProvider extends DefaultBannerProvider{ @Override public String getBanner() { StringBuffer buf = new StringBuffer(); buf.append("=======================================" + OsUtils.LINE_SEPARATOR); buf.append("* *"+ OsUtils.LINE_SEPARATOR); buf.append("* mjDuan *" +OsUtils.LINE_SEPARATOR); buf.append("* *"+ OsUtils.LINE_SEPARATOR); buf.append("=======================================" + OsUtils.LINE_SEPARATOR); buf.append("Version:" + this.getVersion()); return buf.toString(); } @Override public String getVersion() { return "1.1.0"; } @Override public String getWelcomeMessage() { return "Welcome to mjDuan CLI"; } @Override public String getProviderName() { return "My promt provider"; } }
HelloWorldCommands.java:
package com.mjduan.learnSpringShell.commands; import org.springframework.shell.core.CommandMarker; import org.springframework.shell.core.annotation.CliAvailabilityIndicator; import org.springframework.shell.core.annotation.CliCommand; import org.springframework.shell.core.annotation.CliOption; import org.springframework.stereotype.Component; @Component public class HelloWorldCommands implements CommandMarker{ private boolean simpleCommandExecuted = false; @CliAvailabilityIndicator({"hw simple"}) public boolean isSimpleCommandExecuted() { return true; } //返回爲true時才能夠執行hw complex和hw enum命令 @CliAvailabilityIndicator({"hw complex","hw enum"}) private boolean isComplexAvailable() { if(simpleCommandExecuted){ return true; }else{ return false; } } @CliCommand(value = "hw simple", help = "Print a simple hello world message") public String simple( @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message, @CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) { simpleCommandExecuted = true; return "Message = [" + message + "] Location = [" + location + "]"; } @CliCommand(value = "hw complex", help = "Print a complex hello world message (run 'hw simple' once first)") public String hello( @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message, @CliOption(key = { "name1"}, mandatory = true, help = "Say hello to the first name") final String name1, @CliOption(key = { "name2" }, mandatory = true, help = "Say hello to a second name") final String name2, @CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "When you are saying hello") final String time, @CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello") final String location) { return "Hello " + name1 + " and " + name2 + ". Your special message is " + message + ". time=[" + time + "] location=[" + location + "]"; } @CliCommand(value = "hw enum", help = "Print a simple hello world message from an enumerated value (run 'hw simple' once first)") public String eenum( @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final MessageType message){ return "Hello. Your special enumerated message is " + message; } enum MessageType { Type1("type1"), Type2("type2"), Type3("type3"); private String type; private MessageType(String type){ this.type = type; } public String getType(){ return type; } } }
打jar包:
到這,能夠打jar包了(不是war),能夠用命令行執行mvn package或者在eclipse中Run as -> maven build...以後打jar包。
運行:
用windows的cmd命令行切換到項目的源碼目錄下,執行java -jar target/LearnSpringShell2.jar,以下圖所示:
注:在下也只是本身探索了一下,沒有深刻的研究。
源碼url: https://github.com/MingJunDuan/SpringShell1/tree/master/LearnSpringShell2
Enjoy it!