โครงการฤดูใบไม้ผลิแบบดั้งเดิมอาจถูกนำไปใช้กับเว็บคอนเทนเนอร์ส่วนใหญ่เช่น Tomcat Spring Boot ให้วิธีการปรับใช้อย่างง่ายสุด ๆ ซึ่งจะรวมแอปพลิเคชันเข้ากับแพ็คเกจ JAR โดยตรงและจำเป็นต้องดำเนินการ Java -Jar ในการผลิตเพื่อเรียกใช้
บทความนี้อธิบายถึงวิธีการสร้างแพ็คเกจ JAR ที่ใช้งานได้และวิธีการปรับใช้เรียกใช้และหยุด
ในขั้นตอนแรกเราต้องเพิ่ม Spring-Boot-Maven-Plugin ไปยัง Pom.xml และเพิ่มในส่วนการอ้างอิงด้านล่าง:
<สร้าง> <plugins> <plugin> <loupid> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid>
ขั้นตอนที่สองคือการบันทึก pom.xml และเรียกใช้คำสั่งแพ็คเกจ MVN ไปยังแพ็คเกจ:
Localhost: Majunwei $ majunwei $ majunwei $ mvn แพคเกจ -dmaven.test.skip = true [ข้อมูล] การสแกนสำหรับโครงการ ... [คำเตือน] [คำเตือน] พบปัญหาบางอย่างในขณะที่สร้างแบบจำลองที่มีประสิทธิภาพสำหรับ com.majunwei com.majunwei: Spring-boot-tutorial-executable: 0.0.1-snapshot (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/pom.xml) org.springframework.boot: Spring-Boot-Starter-Parent โปรดตรวจสอบโครงสร้างโครงการของคุณ @ บรรทัดที่ 6, คอลัมน์ 10 [คำเตือน] [คำเตือน] ขอแนะนำให้แก้ไขปัญหาเหล่านี้เพราะพวกเขาคุกคามความมั่นคงของอาคาร [คำเตือน] [คำเตือน] ด้วยเหตุผลนี้ - - - /ผู้ใช้/majunwei/เอกสาร/work/spring-boot-tutorial/Spring-Boot-Boot-Tutorial-Executable/SRC/Main/Resources [ข้อมูล] ข้าม ResourceDirectory/ผู้ใช้ที่ไม่ได้มีอยู่/Majunwei/Documents/Work/Spring maven-compiler-plugin: 3.1: คอมไพล์ (คอมไพล์เริ่มต้น) @ Spring-Boot-Tutorial-Executable --- [ข้อมูล] ไม่มีอะไรที่จะรวบรวม-คลาสทั้งหมดนั้นทันสมัย [ข้อมูล] [ข้อมูล] --- แหล่งข้อมูล-ข้อมูล-ข้อมูล-การทดสอบ-ข้อมูล --- maven-compiler-plugin: 3.1: testcompile (testcompile เริ่มต้น) @ spring-boot-tutorial-executable --- [info] ไม่รวบรวมแหล่งทดสอบ [ข้อมูล] [ข้อมูล] --- maven-surefire-plugin: 2.18.1: การทดสอบ maven-jar-plugin: 2.6: jar (default-jar) @ spring-boot-tutorial-executable --- [info] [ข้อมูล] --- ฤดูใบไม้ผลิ-boot-boot-maven-plugin: 1.5.6.release: Repackage (ค่าเริ่มต้น) @ Spring-Boot-Tutorial - -
สิ่งนี้ทำให้การดำเนินการบรรจุภัณฑ์เสร็จสิ้นและบันทึกแพ็คเกจขนาดใหญ่ในไดเรกทอรีเป้าหมาย มันควรจะอยู่ที่ประมาณ 10MB
Localhost: เป้าหมาย Majunwei $ ls -lhtotal 28232drwxr-xr-x 4 Majunwei พนักงาน 136b 8 4 11:12 Classesdrwxr-xr-x 3 Majunwei พนักงาน 102b 8 4 11:14 Majunwei Staff 102b 8 4 11:14 Maven-Status-RW-R --- 1 Majunwei Staff 14m 8 4 11:14 Spring-Boot-Tutorial-Executable-0.0.1-Snapshot.jar-rw-r --- 1 พนักงาน Majunwei 3.2K 8 4 11:14 Spring-Boot-Tutorial-Executable-0.0.1-snapshot.jar.originaldrwxr-Xr-X 3 Majunwei Staff 102b 8 4 11:12 Test-Classes
แพ็คเกจนี้มีแพ็คเกจ JAR ที่ขึ้นอยู่กับคลาสและข้อมูลอื่น ๆ หากคุณต้องการดูเนื้อหาของแพ็คเกจ JAR นี้อย่างระมัดระวังคุณสามารถใช้คำสั่ง JAR TVF หรือเปิดซิปเพื่ออ่าน:
$ jar tvf spring-boot-tutorial- executable-0.0.1-snapshot.jar
นอกจากนี้ยังมีไฟล์ขนาดเล็กมากสำหรับ MyProject-0.0.1-snapshot.jar.original ในไดเรกทอรีเป้าหมาย นี่คือไฟล์ JAR ดั้งเดิมที่สร้างโดย Maven ก่อนที่จะบรรจุสปริงบูต
ขั้นตอนที่สามคือการใช้คำสั่ง java -jar เพื่อเรียกใช้แอปพลิเคชัน:
Localhost: เป้าหมาย Majunwei $ java -jar Spring-Boot-Tutorial-Executable-0.0.1-snapshot.jar - - - - - ============= | _ | ============================ ____/=/_/_/:: spring boot :: (v1.5.6.6.6.6.6.6.6.6.6.6.6 v0.0.1-snapshot บน Localhost ด้วย PID 909 (/users/majunwei/documents/work/spring-boot-tutorial/spring-boot-tutorial-executable/target/spring-boot-tutorial-executable-0.0.1-snapshot /ผู้ใช้/majunwei/เอกสาร/งาน/ฤดูใบไม้ผลิ-บูท-แนะนำ/ฤดูใบไม้ผลิ-บูท-แนะนำ-สามารถใช้งานได้/เป้าหมาย) 2017-08-04 12: 05: 58.926 ข้อมูล 909 --- [หลัก] osbtutorial AtionConfigembeddedWebapplicationContext: รีเฟรช org.springframework.boot.context.embedded.annotationConfigembeddedWebapplicationContext@27F8302D: วันที่เริ่มต้น รูทของบริบท Hierarchy2017-08-04 12: 06: 01.030 Info 909 --- [Main] SbcettomcatembeddedservletContainer: Tomcat เริ่มต้นด้วยพอร์ต: 8080 (HTTP) 2017-08-04 12: 06: 01.050 o.apache.catalina.core.standardservice: เริ่มต้นบริการ [Tomcat] 2017-08-04 12: 06: 01.053 ข้อมูล 909 --- [Main] org.apache.catalina.core.standardengine: เริ่มต้น [OST-StartStop-1] OCCC [Tomcat]. [localhost]. [/]: การเริ่มต้นการเริ่มต้นสปริงแบบฝัง webapplicationContext2017-08-04 12: 06: 01.225 ข้อมูล 909 --- [ost-startstop-1] Ostweb.context 12: 06: 01.430 ข้อมูล 909 --- [OST-StartStop-1] OsbwServlet.ServletRegistrationBean: การทำแผนที่ servlet: 'dispatcherservlet' ถึง [/] 2017-08-04 12: 06: 01.437 ข้อมูล 909 --- 'ตัวละครฯลา' ถึง: [/*] 2017-08-04 12: 06: 01.437 ข้อมูล 909 --- [ost-startstop-1] Osbwservlet.FilterRegistrationBean: ตัวกรองการทำแผนที่: 'Hiddenhttpmethodfilter' [OST-StartStop-1] OSBWSERVLET.FilterRegistrationBean: การทำแผนที่ตัวกรอง: 'HTTPPUTFORMCONTENTENTFILTER' ถึง: [/*] 2017-08-04 12: 06: 01.439 ข้อมูล 909 --- [/*] 2017-08-04 12: 06: 01.439 ข้อมูล 909 --- [ost-startstop-1] osbwservlet.filterregistrationBean: การทำแผนที่ตัวกรอง: 'requestcontextfilter' ถึง: [/*] 2017-08-04 12: 06: 06: 06: 06: 06: 06: 06 @ControllerAdvice: org.springframework.boot.context.embedded.annotationconfigembeddeddedweBapplicationContext@27f8302d: วันที่เริ่มต้น [FRI 04 ส.ค. 12:05:59 CST 2017]; รูทของบริบท Hierarchy2017-08-04 12: 06: 02.019 ข้อมูล 909 --- [หลัก] SWSMMAREQUESTMAPPINGHANDLERMAPPAPP: Mapped "{[/]}" ลงบน Java.lang.string org.spring.boot.tutorial 909 --- [Main] SWSMMAREQUESTMAPPIONDHANDLERMAPPANC: MAPPED "{[/ข้อผิดพลาด]}" เข้าสู่ 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 ข้อมูล 909 --- "{[/ข้อผิดพลาด], ผลิต = [text/html]}" ลงบน org.springframework.web.servlet.modelandview สาธารณะ org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml (javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse) 2017-08-08-08 Oswshandler.simpleurlhandlermapping: เส้นทาง URL ที่แมป [/webjars/**] ไปยังตัวจัดการประเภท [คลาส org.springframework.web.servlet.resource.resourcehttprequesthandler] 2017-08-04 12: 06: 02.062 เส้นทาง URL ที่แม็พ [/**] ไปยังตัวจัดการประเภท [คลาส org.springframework.web.servlet.resource.resourcehtttprequesthandler] 2017-08-04 12: 06: 02.129 ข้อมูล 909 --- ตัวจัดการประเภท [คลาส org.springframework.web.servlet.resource.resourcehtttprequesthandler] 2017-08-04 12: 06: 02.344 ข้อมูล 909 --- [หลัก] osjeaannotationmbeanexporter: การลงทะเบียน Beans 909 --- [Main] SbcettomcatembeddedservletContainer: Tomcat เริ่มต้นที่พอร์ต: 8080 (http) 2017-08-04 12: 06: 02.458 ข้อมูล 909 --- [main] osbtutorial.executableขั้นตอนที่ 4: เหมือนก่อนถ้าคุณต้องการออกจากแอปพลิเคชันให้กด CTRL-C
ดาวน์โหลดซอร์สโค้ดของบทช่วยสอนนี้
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น