Spring Boot创建可执行jar包的实例教程
程序员文章站
2022-10-10 17:58:38
传统的spring项目,可能大部分都要部署到web容器中,如tomcat。spring boot提供了一种超级简单的部署方式,就是直接将应用打成jar包,在生产上只需要执行...
传统的spring项目,可能大部分都要部署到web容器中,如tomcat。spring boot提供了一种超级简单的部署方式,就是直接将应用打成jar包,在生产上只需要执行java -jar就可以运行了。
本篇文章就介绍如何创建可执行jar包,以及如何部署、运行和停止。
第一步,我们需要在pom.xml添加spring-boot-maven-plugin,添加在dependecies部分下面:
<build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
第二步,保存pom.xml并运行mvn package命令打包:
localhost:spring-boot-tutorial-executable majunwei$ mvn package -dmaven.test.skip=true [info] scanning for projects... [warning] [warning] some problems were encountered while building the effective model for com.majunwei:spring-boot-tutorial-executable:jar:0.0.1-snapshot [warning] 'parent.relativepath' of pom com.majunwei:spring-boot-tutorial-executable:0.0.1-snapshot (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/pom.xml) points at com.majunwei:spring-boot-tutorial instead of org.springframework.boot:spring-boot-starter-parent, please verify your project structure @ line 6, column 10 [warning] [warning] it is highly recommended to fix these problems because they threaten the stability of your build. [warning] [warning] for this reason, future maven versions might no longer support building such malformed projects. [warning] [info] [info] ------------------------------------------------------------------------ [info] building spring-boot-tutorial-executable 0.0.1-snapshot [info] ------------------------------------------------------------------------ [info] [info] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-tutorial-executable --- [info] using 'utf-8' encoding to copy filtered resources. [info] skip non existing resourcedirectory /users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/src/main/resources [info] skip non existing resourcedirectory /users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/src/main/resources [info] [info] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-tutorial-executable --- [info] nothing to compile - all classes are up to date [info] [info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ spring-boot-tutorial-executable --- [info] not copying test resources [info] [info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ spring-boot-tutorial-executable --- [info] not compiling test sources [info] [info] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-tutorial-executable --- [info] tests are skipped. [info] [info] --- maven-jar-plugin:2.6:jar (default-jar) @ spring-boot-tutorial-executable --- [info] [info] --- spring-boot-maven-plugin:1.5.6.release:repackage (default) @ spring-boot-tutorial-executable --- [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 2.380 s [info] finished at: 2017-08-04t12:01:52+08:00 [info] final memory: 21m/227m [info] ------------------------------------------------------------------------
这样就完成了打包操作,大好的包保存在target目录里。应该在10mb左右。
localhost:target majunwei$ ls -lh total 28232 drwxr-xr-x 4 majunwei staff 136b 8 4 11:12 classes drwxr-xr-x 3 majunwei staff 102b 8 4 11:14 generated-sources drwxr-xr-x 3 majunwei staff 102b 8 4 11:14 maven-archiver drwxr-xr-x 3 majunwei staff 102b 8 4 11:14 maven-status -rw-r--r-- 1 majunwei staff 14m 8 4 11:14 spring-boot-tutorial-executable-0.0.1-snapshot.jar -rw-r--r-- 1 majunwei staff 3.2k 8 4 11:14 spring-boot-tutorial-executable-0.0.1-snapshot.jar.original drwxr-xr-x 3 majunwei staff 102b 8 4 11:12 test-classes
这个包里包含了依赖的jar包、classes等信息,如果你想仔细查看这个jar包等内容,你可以使用jar tvf命令,或解压出来看:
$ jar tvf spring-boot-tutorial-executable-0.0.1-snapshot.jar
target目录里还有个非常小的myproject-0.0.1-snapshot.jar.original文件。这个是spring boot打包之前maven创建的原始jar文件。
第三步,使用java -jar命令,运行应用:
localhost:target majunwei$ java -jar spring-boot-tutorial-executable-0.0.1-snapshot.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v1.5.6.release) 2017-08-04 12:05:58.917 info 909 --- [ main] o.s.b.tutorial.executable.application : starting application v0.0.1-snapshot on localhost with pid 909 (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target/spring-boot-tutorial-executable-0.0.1-snapshot.jar started by majunwei in /users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target) 2017-08-04 12:05:58.926 info 909 --- [ main] o.s.b.tutorial.executable.application : no active profile set, falling back to default profiles: default 2017-08-04 12:05:59.039 info 909 --- [ main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@27f8302d: startup date [fri aug 04 12:05:59 cst 2017]; root of context hierarchy 2017-08-04 12:06:01.030 info 909 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized with port(s): 8080 (http) 2017-08-04 12:06:01.050 info 909 --- [ main] o.apache.catalina.core.standardservice : starting service [tomcat] 2017-08-04 12:06:01.053 info 909 --- [ main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/8.5.16 2017-08-04 12:06:01.224 info 909 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2017-08-04 12:06:01.225 info 909 --- [ost-startstop-1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 2199 ms 2017-08-04 12:06:01.430 info 909 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/] 2017-08-04 12:06:01.437 info 909 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2017-08-04 12:06:01.437 info 909 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*] 2017-08-04 12:06:01.438 info 909 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*] 2017-08-04 12:06:01.439 info 909 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2017-08-04 12:06:01.890 info 909 --- [ main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@27f8302d: startup date [fri aug 04 12:05:59 cst 2017]; root of context hierarchy 2017-08-04 12:06:02.019 info 909 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/]}" onto java.lang.string org.spring.boot.tutorial.executable.application.home() 2017-08-04 12:06:02.024 info 909 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest) 2017-08-04 12:06:02.024 info 909 --- [ main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2017-08-04 12:06:02.062 info 909 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12:06:02.062 info 909 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12:06:02.129 info 909 --- [ main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12:06:02.344 info 909 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup 2017-08-04 12:06:02.448 info 909 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http) 2017-08-04 12:06:02.458 info 909 --- [ main] o.s.b.tutorial.executable.application : started application in 4.054 seconds (jvm running for 4.622)
第四步,跟之前一样,想要退出应用,按ctrl-c就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: *凤梨的营养价值和功效
推荐阅读
-
Spring Boot项目添加外部Jar包以及配置多数据源的完整步骤
-
Spring Boot创建可执行jar包的实例教程
-
Spring Boot Maven 打包可执行Jar文件的实现方法
-
Spring Boot打jar包后配置文件的外部优化配置方法
-
Spring boot 打jar包分离lib的正确配置方式
-
Spring Boot打war包的实例教程
-
Spring Boot Jar 包部署脚本的实例讲解
-
Spring Boot项目添加外部Jar包以及配置多数据源的完整步骤
-
Spring Boot工程打完jar包读取工程内的模板文件
-
spring boot 打成jar包,部署到公司Linux系统的服务器