JDK:1.8.0_212
IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE)
工程下載:https://files.cnblogs.com/files/xiandedanteng/SpringBootSample03_20190927_01.rar
如果不清楚如何做一個SringBoot工程請參考:https://www.cnblogs.com/xiandedanteng/p/11593880.html
SringBoot工程搭建完畢后,只需要再改動一下pom.xml,其目的是添加 executions節點,完整文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hy</groupId> <artifactId>SpringBootSample03</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootSample03</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
上面兩行綠色漢字注釋中間就是新增的節點。
之后,就是在工程中右鍵點擊pom.xml,然后選擇Run as ->Maven build...,點完了一個對話框會跳出來。
在Goals一項中填入package,然后點Run就行,STS4一頓忙活后,你要打的包在工程下的target目錄下就出來了。
打開控制台,進去這個Jar文件所在的目錄,然后輸入 >java -jar SpringBootSample03-0.0.1-SNAPSHOT.jar
下面是執行效果
D:\>java -jar SpringBootSample03-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) 2019-09-27 10:53:42.009 INFO 4500 --- [ main] c.e.demo.SpringBootSample03Application : Starting SpringBootSample03Application v0.0.1-SNAPSHOT on DESKTOP-8IDBHPK with PID 4500 (D:\SpringBootSample03-0.0.1-SNAPSHOT.jar started by horn1 in D:\) 2019-09-27 10:53:42.014 INFO 4500 --- [ main] c.e.demo.SpringBootSample03Application : No active profile set, falling back to default profiles: default 2019-09-27 10:53:43.985 INFO 4500 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-09-27 10:53:44.032 INFO 4500 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-09-27 10:53:44.032 INFO 4500 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24] 2019-09-27 10:53:44.195 INFO 4500 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-09-27 10:53:44.196 INFO 4500 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2093 ms 2019-09-27 10:53:44.538 INFO 4500 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-09-27 10:53:44.826 INFO 4500 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-09-27 10:53:44.831 INFO 4500 --- [ main] c.e.demo.SpringBootSample03Application : Started SpringBootSample03Application in 3.467 seconds (JVM running for 4.014) 2019-09-27 10:53:55.285 INFO 4500 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-09-27 10:53:55.287 INFO 4500 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-09-27 10:53:55.302 INFO 4500 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 11 ms
然后,在瀏覽器輸入:
http://localhost:8080/test
反饋會是:
欲戴王冠,必承其重
到這里,SpringBoot工程的Jar部署方式就ok了,它的效果和在STS4里Run java application一樣。
網上同類文章對這種方式的具體操作方式五花八門,有繁瑣有簡單,還有些已經失去了時效。所以在參考時還得花點時間尋思鑒別一下,同時做實驗驗證,不能盲目照搬。
-- END -- 2019年9月27日11:15:12