作者:prescott1972 | 来源:互联网 | 2023-09-25 13:27
JDK:1.8.0_212IDE:STS4(SpringToolSuit4Version:4.3.2.RELEASE)工程下载:https:files.cnblogs.comfil
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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.8.RELEASEversion>
<relativePath/>
parent>
<groupId>com.hygroupId>
<artifactId>SpringBootSample03artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>SpringBootSample03name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
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