将一个SpringBoot工程打成jar包并在控制台执行起来

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

posted @ 2019-09-27 11:15  逆火狂飙  阅读(829)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东