【名词解释】
CLI:Command Line Interface 命令行界面 典型如Linux控制台
GUI:Graphical User Interface 图形界面 典型如BS/CS程序
【工具】
Idea和一台能联网的电脑
【主要步骤】
1.用Idea创建一个Maven项目。
2.在pom里加入以下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.6.4</version> </dependency>
注意2.6.4不是必须的,您可以根据自己m2目录中的spring-boot-starter版本来定。当然能联网就省却很多没必要的烦恼了。
3.制作一个启动类如下:
package com.hy; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Hello cli"); } }
这个类一要加入SpringBootApplication注解,二要实现CommandLineRunner接口。
4.此时就可以启动项目了,让我们先看看效果:
C:\Java17\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=55819:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\ufo\IdeaProjects\SpringbootCli\target\classes;C:\Users\ufo\.m2\repository\org\springframework\boot\spring-boot-starter\2.6.4\spring-boot-starter-2.6.4.jar;C:\Users\ufo\.m2\repository\org\springframework\boot\spring-boot\2.6.4\spring-boot-2.6.4.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-context\5.3.16\spring-context-5.3.16.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-aop\5.3.16\spring-aop-5.3.16.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-beans\5.3.16\spring-beans-5.3.16.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-expression\5.3.16\spring-expression-5.3.16.jar;C:\Users\ufo\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.4\spring-boot-autoconfigure-2.6.4.jar;C:\Users\ufo\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.4\spring-boot-starter-logging-2.6.4.jar;C:\Users\ufo\.m2\repository\ch\qos\logback\logback-classic\1.2.10\logback-classic-1.2.10.jar;C:\Users\ufo\.m2\repository\ch\qos\logback\logback-core\1.2.10\logback-core-1.2.10.jar;C:\Users\ufo\.m2\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;C:\Users\ufo\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.1\log4j-to-slf4j-2.17.1.jar;C:\Users\ufo\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;C:\Users\ufo\.m2\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\Users\ufo\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-core\5.3.16\spring-core-5.3.16.jar;C:\Users\ufo\.m2\repository\org\springframework\spring-jcl\5.3.16\spring-jcl-5.3.16.jar;C:\Users\ufo\.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar com.hy.MyApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.4) 2022-03-01 20:20:13.578 INFO 9952 --- [ main] com.hy.MyApplication : Starting MyApplication using Java 17.0.2 on DESKTOP-OOO5MRB with PID 9952 (C:\Users\ufo\IdeaProjects\SpringbootCli\target\classes started by ufo in C:\Users\ufo\IdeaProjects\SpringbootCli) 2022-03-01 20:20:13.582 INFO 9952 --- [ main] com.hy.MyApplication : No active profile set, falling back to 1 default profile: "default" 2022-03-01 20:20:14.362 INFO 9952 --- [ main] com.hy.MyApplication : Started MyApplication in 1.57 seconds (JVM running for 2.306) Hello cli Process finished with exit code 0
很不错,确如预期。
5.制作执行包。
程序出来后,我们不可能让客户带着Idea里运行,像这种cli程序一般是给他打个jar包,然后用java -jar的方式运行。
打包很简单,只要把下面代码放到pom里:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
刷新maven后,打开maven窗口,先执行clean再执行package,在项目的target目录里就会出现一个jar包,我的是SpringbootCli-1.0-SNAPSHOT
之后把这个jar拿出来,放到任意目录下,打开cmd窗口,输入java -jar SpringbootCli-1.0-SNAPSHOT.jar就执行起来了。
C:\hy>java -jar SpringbootCli-1.0-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.4) 2022-03-01 20:12:34.097 INFO 7628 --- [ main] com.hy.MyApplication : Starting MyApplication using Java 17.0.2 on DESKTOP-OOO5MRB with PID 7628 (C:\hy\SpringbootCli-1.0-SNAPSHOT.jar started by ufo in C:\hy) 2022-03-01 20:12:34.103 INFO 7628 --- [ main] com.hy.MyApplication : No active profile set, falling back to 1 default profile: "default" 2022-03-01 20:12:34.858 INFO 7628 --- [ main] com.hy.MyApplication : Started MyApplication in 1.294 seconds (JVM running for 1.803) Hello cli C:\hy>
【完整POM.XML】
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SpringbootCli</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.6.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
最后把完整的工程链接分享给大家
【完整工程链接】
https://files.cnblogs.com/files/heyang78/SpringbootCli220301.zip?t=1646137849
END
PS:特别提醒
如果非公网,您可能需要指定spring-boot-maven-plugin的版本,如下
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.13.RELEASE</version>
<executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
以上蓝色粗体字部分是我在单位机器上必须添加的内容,至于版本是从我的m2仓库里找到的最高版本,而本地仓库是连的单位maven私服。
没有使用这句之前,打包时总是报1.5.9.RELAEASE出错,然后执行报没有主清单属性。
加上这句后打包执行就都好了。
2022年3月2日10点03分