zoukankan      html  css  js  c++  java
  • 将一个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

  • 相关阅读:
    Leetcode 从前序与中序遍历序列构造二叉树
    python基础一 day6 序列操作集合
    python基础一 day6 文件操作
    python基础一 day5 集合
    python基础一 day5 复习
    python基础一 day5 知识点
    python基础一 day4 字典
    python基础一day4 元组
    python基础一 day3 列表
    python基础一 day2 字符串操作
  • 原文地址:https://www.cnblogs.com/heyang78/p/11596706.html
Copyright © 2011-2022 走看看