Les projets de printemps traditionnels peuvent être principalement déployés dans des conteneurs Web, tels que Tomcat. Spring Boot fournit une méthode de déploiement super simple, qui doit combiner directement l'application dans un package JAR, et ne doit exécuter Java -Jar en production à exécuter.
Cet article décrit comment créer un package JAR exécutable et comment déployer, exécuter et arrêter.
Dans la première étape, nous devons ajouter Spring-Boot-Maven-Plugin à pom.xml et l'ajouter dans la section Dépendances ci-dessous:
<fuild> <Glugins> <Glugin> <GroupId> org.springframework.boot </prôdId> <Artifactid> Spring-Boot-Maven-Plugin </ ArfactId> </Glugin> </Glugins> </Duild>
La deuxième étape consiste à enregistrer le pom.xml et à exécuter la commande package mvn vers le package:
LocalHost: Spring-Boot-tutorial-Exécutable Majunwei $ mvn package -dmaven.test.skip = true [info] scanning pour les projets ... [avertissement] [Avertissement] Certains problèmes ont été rencontrés lors de la construction du modèle efficace pour com.majunwei: Spring-boot-tutorial-eccutable: Jar: 0.0.1-snapshot [Warning] 'RELATPE com.majunwei: printemps-boot-tutorial-execuable: 0.0.1-snapshot (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/pom.xml) pointe à com.majunwei: printemps-boot-tutorial au lieu de org.springframework.boot: printemps-boot-starter-parent, veuillez vérifier votre structure de projet @ ligne 6, colonne 10 [avertissement] [avertissement] Il est fortement recommandé de résoudre ces problèmes car ils menacent la stabilité de votre bâtiment. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- / Utilisateurs / majunwei / documents / work / spring-boot-tutorial / printemp-boot-tutorial-excutable / src / main / ressources [info] skip non existant ressourcesdirectory / users / majunwei / documents / work / printemp-boot-tutorial / spring-boot-tutorial-excutable / src / main / ressources [info] [info] ----- Maven-Compiler-Plugin: 3.1: Compile (par défaut-Compile) @ Spring-Boot-tutorial-Exécutable --- [Info] Rien à compiler - Toutes les classes sont à jour [Info] [Info] --- Maven-Resources-Plugin: 2.6: TestResources (Info-TestResources) - Info] Maven-Compiler-Plugin: 3.1: TestCompile (Default-TestCompile) @ Spring-Boot-tutorial-Excutable --- [Info] Not Compilation Sources de test [Info] [Info] --- Maven-Surefire-Plugin: 2.18.1: Test (Test de défaut) @ Spring-boot-tutorial-execuable --- [Info] Maven-Jar-Plugin: 2.6: Jar (par défaut-jar) @ printemps-boot-tutorial-exécutable --- [info] [info] --- printemps-boot-maven-Plugin: 1.5.6.release: repackage (par défaut) @ Spring-boot-tutorial-eCucuable --- [info] ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Cela complète l'opération d'emballage et enregistre les grands packages dans le répertoire cible. Il devrait être d'environ 10 Mo.
LocalHost: Target Majunwei $ ls -lhtotal 28232drwxr-xr-x 4 majunwei personnel 136b 8 4 11:12 Classesdrwxr-xr-x 3 Majunwei Staff 102b 8 4 11:14 généré-Sourcedrwxr-xr-x 3 Majunwei Staff 102b 8 4 11:14 Maven-a Majunwei Personnel 102B 8 4 11:14 Maven-Status-RW-R --- 1 Majunwei Personnel 14m 8 4 11:14 printemps-boot-tutorial-exécutable-0.0.1-snapshot.jar-rw-r --- 1 Majunwei Staff 3.2k 8 4 11:14 printemps-boot-tutorial-exécutable-0.0.1-snapshot.jar.originaldrwxr-xr-x 3 majunwei personnel 102b 8 4 11:12 Classes de test
Ce package contient des packages JAR dépendants, des classes et d'autres informations. Si vous souhaitez afficher soigneusement le contenu de ce package JAR, vous pouvez utiliser la commande JAR TVF ou le décompresser pour lire:
$ jar tvf printemps-boot-tutorial-exécutable-0.0.1-snapshot.jar
Il existe également un très petit fichier pour MyProject-0.0.1-Snapshot.jar.original dans le répertoire cible. Il s'agit du fichier JAR d'origine créé par Maven avant le bac à printemps.
La troisième étape consiste à utiliser la commande java -jar pour exécuter l'application:
LocalHost: Target Majunwei $ java -jar printemps-boot-tutorial-exécutable-0.0.1-snapshot.jar. ____ __ _ /// / ___'_ __ _ _(_)_ __ __ _ / / / / / ( ( )/___ | '_ | '_ | '_ // _` | / / / / / / / / / / / / / | | | | | | | | | | | | | | | | | | | (_| | ) ) ) ) ' |____| .__ | | _ | | _ | | ============= | _ | ========================. V0.0.1-Snapshot sur localhost avec PID 909 (/Users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-execuable/target/spring-boot-tutorial-execuable-0.0.1-snapshot.jar a commencé par majunwei dans dans dans dans le maju / Utilisateurs / majunwei / documents / work / printemp-boot-tutorial / printemp-boot-tutorial-exécutable / cible) 2017-08-04 12: 05: 58.926 info 909 --- [Main] Osbtutorial.Execuable.Application: pas de profil actif Set, Back to Defaul Main] ationConfigeMedDedWebApplicationContext: Rafraîchissement org.springframework.boot.context.embedded.annotationConfigembeddedwebapplicationContext@27f8302d: date de démarrage [ven 04 août 12:05:59 CST 2017]; Root of Context Hierarchy2017-08-04 12: 06: 01.030 Info 909 --- [Main] SBCETTOMCATEMEDDEDSERVletContainer: Tomcat initialisé avec le 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.162017-08-04 12:06:01.224 INFO 909 --- [Ost-StartStop-1] OCCC [Tomcat]. [LocalHost]. [/]: Initialisation de Spring Embedded WebApplicationContext2017-08-04 12: 06: 01.225 Info 909 --- [OST-StartStop-1] Ostweb.Context.Contextloader: Root WebApplication Context: Initialisation terminée dans 2199 MS2017-08-04 12: 06: 01.430 Info 909 --- [OST-StartStop-1] OSBWServlet.ServletRegrationBean: Mapping Servlet: 'DispatcherServlet' à [/] 2017-08-04 12: 06: 01.437 Info 909 --- [Ost-StartStop-1] «CharacterEncodingFilter» à: [/ *] 2017-08-04 12: 06: 01.437 Info 909 --- [Ost-StartStop-1] OSBWServlet.FilterRegistrationBean: Mapping Filter: `` HiddenhttpMethodFilter 'à: [/ *] 2017-08-04 12: 06: 01.438 Info 909-08-04 12: 06: 01.438 IFFO 909 [OST-StartStop-1] OSBWServlet.FilterRegistrationBean: Filtre de mappage: «httpputformContentFilter» à: [/ *] 2017-08-04 12: 06: 01.439 Info 909 --- [/ *] 2017-08-04 12: 06: 01.439 Info 909 --- [OST-StartStop-1] OSBWServlet.FilterRegistrationBean: Mapping Filter: 'requestContextFilter' to: [/ *] 2017-08-04 12: 06: 01.890 Info 909 --- [Main] @ControllerAdvice: org.springframework.boot.context.embedded.annotationConfigembeddedwebapplicationContext@27f8302d: date de démarrage [ven 04 12:05:59 CST 2017]; Root of Context Hiérarchy2017-08-04 12: 06: 02.019 Info 909 --- [Main] swsmmarequestmappinghandlermapping: maphed "{[/]}" sur java.lang.string org.spring.boot.tutorial.execuable.application.home () 2017-08-04 12: 02.024 909 --- [Main] swsmmarequestmappinghandlermapping: mapted "{[/ error]}" sur public org.springframework.http.responseentity <java.util.map <java.lang.string, java.lang.object >> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error (javax.servlet.http.httSservletRequest) 2017-08-04 12: 06: 02.024 info 909 --- [main] swsmmarequestmapinghandlermappingPapping: mAPED. "{[/ error], produit = [text / html]}" sur public org.springframework.web.servlet.modelandView org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml (javax.servlet.http.httpservletRequest, javax.servlet.http.httpservletRessu) 2017-08-04 12: 06: 02 Oswshandler.simpleurlHandlermapping: chemin d'url mappé [/ webjars / **] sur le gestionnaire de type [classe org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.062 Info 909 - [Main] Oswshandler.Simpleurl. Chemin d'url [/ **] sur le gestionnaire de type [classe org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.129 info 909 --- [Main] Oswshandler.SimpleurlLermler: Mapped Url Path [/**/favicon.ico] org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.344 Info 909 --- [Main] OsjeaannotationMbeanExporteur: Enregistrement des haricots pour l'exposition JMX sur startup2017-08-04 12: 06: 02.48 Info 909 - PRINCE] SBCETTOMCATEMBEDDEDSERVletContainer: Tomcat Démarré sur les ports (s): 8080 (HTTP) 2017-08-04 12: 06: 02.458 Info 909 --- [Main] Osbtutorial.Execuable.Application: Application démarrée en 4.054 secondes (JVM en fonction de 4.622)Étape 4: Tout comme avant, si vous souhaitez quitter l'application, appuyez sur Ctrl-C.
Téléchargez le code source de ce tutoriel
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.