Los proyectos de primavera tradicionales pueden implementarse principalmente en contenedores web, como Tomcat. Spring Boot proporciona un método de implementación súper simple, que es combinar directamente la aplicación en un paquete JAR, y solo necesita ejecutar Java -jar en la producción para ejecutarse.
Este artículo describe cómo crear un paquete JAR ejecutable y cómo implementar, ejecutar y detener.
En el primer paso, necesitamos agregar Spring-Boot-Maven-Plugin a pom.xml y agregarlo en la sección de dependencias a continuación:
<DIARD> <GULTINS> <LEMPLEMIN> <MOUMPOMID> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactid> </glugin> </glugins> </struct>
El segundo paso es guardar el pom.xml y ejecutar el comando del paquete MVN para empaquetar:
localhost: spring-boot-tutorial-ejecutable majunwei $ mvn paquete -dmaven.test.skip = true [info] escaneo para proyectos ... [advertencia] [advertencia] Se encontraron algunos problemas al construir el modelo efectivo para com.majunwei: spring-boot-tutorial-eCutable: jar: 0.0.1-snapshot com.majunwei: Spring-Boot-Tutorial-Executable: 0.0.1-snapshot (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/pom.xml) en com.majunwei: primavera-boot-tutorial en lugar de de org.springframework.Boot: Spring-Boot-Starter-Parent, verifique su estructura de proyecto @ Line 6, Columna 10 [Advertencia] [Advertencia] Se recomienda encarecidamente solucionar estos problemas porque amenazan la estabilidad de su edificio. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- /Usuarios/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-eexecutable/src/main/recursos [info] omitir no existente recursos de recursos/usuarios/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-eexecutable/src/src/main/recursos [info] [info] --------tutorial maven-compiler-plugin: 3.1: compile (default-compile) @ spring-boot-tutorial-eexecutable --- [info] nada para compilar: todas las clases están actualizadas [info] [info] --- maven-recesces-plugin: 2.6: testresources (testesurces predeterminados) --- maven-compiler-plugin: 3.1: testCompile (default-testcompile) @ spring-boot-tutorial-eexecutable --- [info] no compilando fuentes de prueba [info] [info] --- maven-seguro-plugin: 2.18.1: test (test-test) @ spring-tutorial-eCutable --- [info] tests tests we test se tests weped. maven-jar-plugin: 2.6: jar (default-jar) @ spring-boot-tutorial-ejecutable --- [info] [info] --- spring-boot-maven-plugin: 1.5.6.release: reempackage (predeterminado) @ spring-boot-tutorial-eexecutable --- [info] --------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------
Esto completa la operación de embalaje y guarda los paquetes grandes en el directorio de destino. Debería ser alrededor de 10 MB.
localhost:target majunwei$ ls -lhtotal 28232drwxr-xr-x 4 majunwei staff 136B 8 4 11:12 classesdrwxr-xr-x 3 majunwei staff 102B 8 4 11:14 generated-sourcesdrwxr-xr-x 3 majunwei staff 102B 8 4 11:14 maven-archiverdrwxr-xr-x 3 Majunwei Staff 102b 8 4 11:14 Maven-status-rw-r --- 1 Majunwei Staff 14m 8 4 11:14 Spring-Boot-Tutorial-Ejecutable-0.0.1-Snapshot.jar-Rw-R --- 1 Majunwei Staff 3.2k 8 4 11:14 Spring-Boot-Tutorial-Ejecutable-0.0.1-snapshot.jar.originaldrwxr-xr-x 3 Majunwei Staff 102b 8 4 11:12 Clases de prueba
Este paquete contiene paquetes de JAR dependientes, clases y otra información. Si desea ver cuidadosamente el contenido de este paquete JAR, puede usar el comando jar tvf o descomponerlo para leer:
$ jar tvf spring-boot-tutorial-ejecutable-0.0.1-snapshot.jar
También hay un archivo muy pequeño para myproject-0.0.1-snapshot.jar.original en el directorio de destino. Este es el archivo JAR original creado por Maven antes de que el arranque de primavera esté empaquetado.
El tercer paso es usar el comando java -jar para ejecutar la aplicación:
Localhost: Target Majunwei $ Java -jar Spring-Boot-Tutorial-Ejecutable-0.0.1-Snapshot.jar. ____ __ _ /// / ___'_ __ _ _ (_) _ __ __ _ / / / / ((() / ____ | '_ |' _ | '_ // _` | / / / / / / / / / / / / / | | | | | | | | | | | | | | | (_ |>)) .__ | | _ | | _ | | ============== | _ | =========================== ____/=/_/_/:: Spring Boot :: (v1.5.6.release) 2017-08-04 12: 05: 58.917 Información 909 --- [Main] v0.0.1-snapshot en localhost con PID 909 (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target/spring-boot-tutorial-executable-0.0.1-snapshot.jar /Usuarios/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-eexecutable/target) 2017-08-04 12: 05: 58.926 info 909 --- [principal] OsBtutorial.Executable.application: No se establece un perfil activo, recaudado a los perfiles predeterminados: predeterminado2017-08-04 12: 05: 59.099999----[--RECHOLO-RECUPTADO DE PROFILECIDAD: DEFORACIÓN: PRECUENTA A2017-08-04 12: 05. Main] ationConfigembeddedwebapplicationContext: Refreshing org.springframework.boot.context.embedded.annotationConfigembeddedwebapplicationcontext@27f8302d: fecha de inicio [FRI 04 12:05:59 CST 2017]; Root of Context Jerarchy2017-08-04 12: 06: 01.030 Información 909 --- [principal] SBCETTOMCATEMBEDDEDServletContainer: Tomcat inicializado con puerto (s): 8080 (HTTP) 2017-08-04 12: 06: 01.050 Información 909 ----- [principal] o.apache.catalina.core.StandardService: Servicio inicial [Tomcat] 2017-08-04 12: 06: 01.053 Información 909 --- [Main] org.apache.catalina.core.Standardengine: Infoin Motor: Apache Tomcat/8.5.162017-08-04 12: 06: 01.224 Info 909-APACH [OST-STARTSTOP-1] OCCC [TOMCAT]. [Localhost]. [/]: Inicialización de la aplicación web incrustada SpringContext2017-08-04 12: 06: 01.225 Información 909 --- [OST-STARTSTOP-1] OSTWEB.CONTEXT. 12: 06: 01.430 Información 909 --- [OST-STARTSTOP-1] OSBWServlet.ServletRegistrationBean: Mapeo Servlet: 'DispatcherServlet' a [/] 2017-08-04 12: 06: 01.437 Info 909 --- [OST-STARTSTOP-1] OSBWServlet. 'caracterSencodingFilter' a: [/*] 2017-08-04 12: 06: 01.437 INFO 909 --- [OST-STARTSTOP-1] OSBWServlet.FilterRegistrationBean: Mapeo del filtro: 'Hiddenhtttpmethodfilter' a: [/*] 2017-08-04 12: 06: 01. [OST-STARTSTOP-1] OSBWServlet.FilterRegistrationBean: Filtro de mapeo: 'httpputFormContentFilter' a: [/*] 2017-08-04 12: 06: 01.439 Info 909 --- [OSTStop-1] OsbWServlet.FilterRegistrationBean: Filtro MapePing: 'Mapeador:' [/*] 2017-08-04 12: 06: 01.439 INFO 909 --- [OST-STARTSTOP-1] OSBWServlet.FilterRegistrationBean: Mapeo de filtro: 'requestContextFilter' a: [/*] 2017-08-04 12: 06: 01.890 info 909 --- [main] swsmmmmermemmer @ControllerAdvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@27f8302d: fecha de inicio [viernes 04 12:05:59 CST 2017]; root of context hierarchy2017-08-04 12:06:02.019 INFO 909 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String org.spring.boot.tutorial.executable.Application.home()2017-08-04 12:06:02.024 INFO 909 --- [Main] SWSMMAREQUESTMAPPingHandLermapping: Mapped "{[/error]}" en público 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 --- [principal] swsmmarequestmappinghandlerping: mapeado "{[[/error], produce = [text/html]}" en público 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] [Main] OSWSHANDLER.SimpleUrlHandlermapping: ruta de URL asignada [/webjars/**] en el controlador de type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.062 info 909 ---- [Main] Oswshandler.simapt Ruta de URL asignada [/**] en el controlador de tipo [clase org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.129 Info 909 --- [Main] Oswshandler.Simpleurlhandlermapped: Mapped Urler de tipo [clase org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.344 info 909 --- [principal] OsJeaannotationMBeanExporter: registrar frijoles para la exposición JMX en startup2017-08-04 12: 06: 06. [Principal] SbcettomCatembeddedServletContainer: Tomcat comenzó en puerto (s): 8080 (http) 2017-08-04 12: 06: 02.458 Información 909 --- [principal] OsbTutorial.Executable.Application: Aplicación comenzó en 4.054 segundos (JVM en ejecución para 4.622)Paso 4: Al igual que antes, si desea salir de la aplicación, presione CTRL-C.
Descargue el código fuente de este tutorial
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.