传统的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打成普通的jar包,不包含配置文件,供其他程序应用 本文介绍如何使用Maven将Spring Boot应用打成普通的非可执行jar包。 配置maven-jar-plugin 执行m
问题内容: 我使用eclipse创建可执行jar。它依靠外部其他罐子。 在Eclipse中,只需选择即可。 您可以创建一个可执行jar。它可以在安装了jre的任何地方执行。 但是如果我使用命令行来编译jar。 它可以生成jar。但是jar只能在产生它的目录中执行。如果我将其放入另一个目录或计算机,则会抱怨。 因此,我的问题是,如何使用命令行作为Eclipse生成可执行jar。 问题答案: jar文
我提取了一个包含xml、java类等的可执行jar文件。实际上,这个可执行jar文件是一个具有依赖关系的库。我需要修改这个库中一个类文件中的一行代码。我已经成功地编辑了类文件,现在我想把它重新打包到可执行的jar中。如何做到这一点。
11.5 创建可执行的jar文件 让我们通过创建一个能够运行在生产环境中、完全独立且可执行的jar文件来完成我们的示例。可执行jar(有时称为“fat jar”)是包含有被编译的类以及代码运行所依赖的所有jar的归档文件。 可执行的jar和Java Java没有提供任何标准的方法来加载嵌套的jar文件(即包含在一个jar文件中的jar文件)。如果您想发布一个独立的应用程序,这可能会导致问题。 为了
我想做的是: 使用gradle和eclipse作为IDE创建可执行Jar。Jar应该包含运行所需的所有库,并且可以在docker容器中使用。 迄今为止的问题: org.springframework.boot不包含在Jar中。 代码: src/main/java/SampleController。JAVA 建筑格拉德尔 使用的命令: 在工作区/项目/构建/库中 java-jar项目。罐子 错误:
我需要从我的应用程序中制作一个可执行的jar包。 这是我的pom.xml: 我用mvn clean package-DskipTests=true构建包,并通过java-jar运行它,我得到了以下结果: mvn--version Apache Maven 3.6.0(NON-CANONICAL_2018-11-06T03:14:22 01:00_root;2018-11-06T03:14:22 0