Spring Boot作成jarパッケージ実行可能な実例教程
伝統的なspringプロジェクトは、ほとんどがウェブコンテナに展開されるかもしれません。Spring Bootは、アプリケーションを直接jarパッケージにするという超簡単な配置を提供しています。生産上はjava-jarだけで実行できます。
この記事では、Jarパッケージをどのように作成し、どのように展開し、実行し、停止するかを紹介します。
第一歩は、私たちはpom.xmlにspring-boot-maven-pluginを追加し、dependeciesの部分の下に追加する必要があります。
第3ステップは、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ディレクトリにあります。10 MBぐらいです。
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カバン、clasesなどの情報が含まれています。このjarカバンなどを詳しく調べたいなら、jar tvfコマンドを使ってもいいです。
$ jar tvf spring-boot-tutorial-executable-0.0.1-SNAPSHOT.jar
targetディレクトリには非常に小さいmyproject-00.0.1-NAPSHOT.jar.originalファイルがあります。これはSpring Boot包装前にMavenが作成したオリジナルJarファイルです。第3ステップは、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を押せばいいです。本教程のソースコードをダウンロードします。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。