Spring Boot创建可执行jar包的实例教程
传统的spring项目,可能大部分都要部署到web容器中,如Tomcat。SpringBoot提供了一种超级简单的部署方式,就是直接将应用打成jar包,在生产上只需要执行java-jar就可以运行了。
本篇文章就介绍如何创建可执行jar包,以及如何部署、运行和停止。
第一步,我们需要在pom.xml添加spring-boot-maven-plugin,添加在dependecies部分下面:
org.springframework.boot spring-boot-maven-plugin
第二步,保存pom.xml并运行mvnpackage命令打包:
localhost:spring-boot-tutorial-executablemajunwei$mvnpackage-Dmaven.test.skip=true [INFO]Scanningforprojects... [WARNING] [WARNING]Someproblemswereencounteredwhilebuildingtheeffectivemodelforcom.majunwei:spring-boot-tutorial-executable:jar:0.0.1-SNAPSHOT [WARNING]'parent.relativePath'ofPOMcom.majunwei:spring-boot-tutorial-executable:0.0.1-SNAPSHOT(/Users/majunwei/Documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/pom.xml)pointsatcom.majunwei:spring-boot-tutorialinsteadoforg.springframework.boot:spring-boot-starter-parent,pleaseverifyyourprojectstructure@line6,column10 [WARNING] [WARNING]Itishighlyrecommendedtofixtheseproblemsbecausetheythreatenthestabilityofyourbuild. [WARNING] [WARNING]Forthisreason,futureMavenversionsmightnolongersupportbuildingsuchmalformedprojects. [WARNING] [INFO] [INFO]------------------------------------------------------------------------ [INFO]Buildingspring-boot-tutorial-executable0.0.1-SNAPSHOT [INFO]------------------------------------------------------------------------ [INFO] [INFO]---maven-resources-plugin:2.6:resources(default-resources)@spring-boot-tutorial-executable--- [INFO]Using'UTF-8'encodingtocopyfilteredresources. [INFO]skipnonexistingresourceDirectory/Users/majunwei/Documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/src/main/resources [INFO]skipnonexistingresourceDirectory/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]Nothingtocompile-allclassesareuptodate [INFO] [INFO]---maven-resources-plugin:2.6:testResources(default-testResources)@spring-boot-tutorial-executable--- [INFO]Notcopyingtestresources [INFO] [INFO]---maven-compiler-plugin:3.1:testCompile(default-testCompile)@spring-boot-tutorial-executable--- [INFO]Notcompilingtestsources [INFO] [INFO]---maven-surefire-plugin:2.18.1:test(default-test)@spring-boot-tutorial-executable--- [INFO]Testsareskipped. [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]BUILDSUCCESS [INFO]------------------------------------------------------------------------ [INFO]Totaltime:2.380s [INFO]Finishedat:2017-08-04T12:01:52+08:00 [INFO]FinalMemory:21M/227M [INFO]------------------------------------------------------------------------
这样就完成了打包操作,大好的包保存在target目录里。应该在10MB左右。
localhost:targetmajunwei$ls-lh total28232 drwxr-xr-x4majunweistaff136B8411:12classes drwxr-xr-x3majunweistaff102B8411:14generated-sources drwxr-xr-x3majunweistaff102B8411:14maven-archiver drwxr-xr-x3majunweistaff102B8411:14maven-status -rw-r--r--1majunweistaff14M8411:14spring-boot-tutorial-executable-0.0.1-SNAPSHOT.jar -rw-r--r--1majunweistaff3.2K8411:14spring-boot-tutorial-executable-0.0.1-SNAPSHOT.jar.original drwxr-xr-x3majunweistaff102B8411:12test-classes
这个包里包含了依赖的jar包、classes等信息,如果你想仔细查看这个jar包等内容,你可以使用jartvf命令,或解压出来看:
$jartvfspring-boot-tutorial-executable-0.0.1-SNAPSHOT.jar
target目录里还有个非常小的myproject-0.0.1-SNAPSHOT.jar.original文件。这个是SpringBoot打包之前Maven创建的原始jar文件。
第三步,使用java-jar命令,运行应用:
localhost:targetmajunwei$java-jarspring-boot-tutorial-executable-0.0.1-SNAPSHOT.jar ._________ /\\/___'_____(_)______\\\\ (()\___|'_|'_||'_\/_`|\\\\ \\/___)||_)|||||||(_||)))) '|____|.__|_||_|_||_\__,|//// =========|_|==============|___/=/_/_/_/ ::SpringBoot::(v1.5.6.RELEASE) 2017-08-0412:05:58.917INFO909---[main]o.s.b.tutorial.executable.Application:StartingApplicationv0.0.1-SNAPSHOTonlocalhostwithPID909(/Users/majunwei/Documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target/spring-boot-tutorial-executable-0.0.1-SNAPSHOT.jarstartedbymajunweiin/Users/majunwei/Documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target) 2017-08-0412:05:58.926INFO909---[main]o.s.b.tutorial.executable.Application:Noactiveprofileset,fallingbacktodefaultprofiles:default 2017-08-0412:05:59.039INFO909---[main]ationConfigEmbeddedWebApplicationContext:Refreshingorg.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27f8302d:startupdate[FriAug0412:05:59CST2017];rootofcontexthierarchy 2017-08-0412:06:01.030INFO909---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcatinitializedwithport(s):8080(http) 2017-08-0412:06:01.050INFO909---[main]o.apache.catalina.core.StandardService:Startingservice[Tomcat] 2017-08-0412:06:01.053INFO909---[main]org.apache.catalina.core.StandardEngine:StartingServletEngine:ApacheTomcat/8.5.16 2017-08-0412:06:01.224INFO909---[ost-startStop-1]o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringembeddedWebApplicationContext 2017-08-0412:06:01.225INFO909---[ost-startStop-1]o.s.web.context.ContextLoader:RootWebApplicationContext:initializationcompletedin2199ms 2017-08-0412:06:01.430INFO909---[ost-startStop-1]o.s.b.w.servlet.ServletRegistrationBean:Mappingservlet:'dispatcherServlet'to[/] 2017-08-0412:06:01.437INFO909---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'characterEncodingFilter'to:[/*] 2017-08-0412:06:01.437INFO909---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'hiddenHttpMethodFilter'to:[/*] 2017-08-0412:06:01.438INFO909---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'httpPutFormContentFilter'to:[/*] 2017-08-0412:06:01.439INFO909---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'requestContextFilter'to:[/*] 2017-08-0412:06:01.890INFO909---[main]s.w.s.m.m.a.RequestMappingHandlerAdapter:Lookingfor@ControllerAdvice:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27f8302d:startupdate[FriAug0412:05:59CST2017];rootofcontexthierarchy 2017-08-0412:06:02.019INFO909---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/]}"ontojava.lang.Stringorg.spring.boot.tutorial.executable.Application.home() 2017-08-0412:06:02.024INFO909---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error]}"ontopublicorg.springframework.http.ResponseEntity>org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-08-0412:06:02.024INFO909---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error],produces=[text/html]}"ontopublicorg.springframework.web.servlet.ModelAndVieworg.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-08-0412:06:02.062INFO909---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/webjars/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-0412:06:02.062INFO909---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-0412:06:02.129INFO909---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**/favicon.ico]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-0412:06:02.344INFO909---[main]o.s.j.e.a.AnnotationMBeanExporter:RegisteringbeansforJMXexposureonstartup 2017-08-0412:06:02.448INFO909---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcatstartedonport(s):8080(http) 2017-08-0412:06:02.458INFO909---[main]o.s.b.tutorial.executable.Application:StartedApplicationin4.054seconds(JVMrunningfor4.622)
第四步,跟之前一样,想要退出应用,按ctrl-c就可以了。
下载本教程的源代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。