当前位置: 首页 > 图灵资讯 > 技术篇> 打包

打包

来源:图灵教育
时间:2023-06-08 09:21:38

1.打包85

SpringBoot可以打包为war或jar文件。以两种方式发布应用程序

2.SpringBoot包装warrr

创建Springbootweb项目:course13

2.1pom.xml85

在pom.tomcat对jsp的分析包配置在xml文件中

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.11</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bjpowernode</groupId>    <artifactId>course13</artifactId>    <version>1.0.0</version>    <!--打包类型-->    <packaging>war</packaging>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!--加入处理jsp的依赖  85-->        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId>tomcat-embed-jasper</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <!--包装后的文件名称-->        <finalName>myboot</finalName>        <!--resources插件, 将jsp编译到指定的目录  85-->        <resources>        <resource>            <directory>src/main/webapp</directory>            <targetPath>META-INF/resources</targetPath>            <includes>                <include>**/*.*</include>            </includes>        </resource>            <!--使用mybatiss ,而且mapper文件放在src/main/java目录-->            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>            <!--把src/main/resources下面的所有文件,都包括classes目录-->            <resource>                <directory>src/main/resources</directory>                <includes>                    <include>**/*.*</include>                </includes>            </resource>        </resources>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

2.2创建webapp目录85.2

打包_spring

web应用目录指定webapp

打包_spring_02

main.jsp

<%--为jar打包  88--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>     main.jsp ,显示数据 ${data}</body></html>

2.3创建855jsp文件

<%--演示打包方式  85--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>index.jsp</title></head><body>    index.jsp , 在controler中显示数据  ${data}</body></html>

2.4创建JspWarcontroller85

JspController

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;///演示打包方式打包war文件  85@Controllerpublic class JspController {    @RequestMapping("/main")    public String main(Model model){        model.addAttribute("data",“SpringBoot打包为war文件”;        return "index";    }}

2.5视图解析器85设置视图解析器

applicationg.properties

server.port=9001server.servlet.context-path=/myjsp#指定视图分析器  85http://spring.mvc.view.prefix=/spring.mvc.view.suffix=.jsp

2.6启动主类,浏览器访问地址index

访问浏览器http://localhost:9001/myjsp/main

打包_打包操作_03

2.7主启动类继承Springbotservletinitializer86-87

继承Springbotservletinitializer可以使用外部tomcat。Springbotservletinitializer是原始web。.替换xml文件。采用嵌入式Servlet,默认不支持jsp。

继承这类SpringBotServletinitilizer

独立tomcat服务器可以使用

JspApplication

package com.bjpowernode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;/** * SpringBootServletInitializer: 继承这一类, 独立tomcat服务器可以使用  86 */@SpringBootApplicationpublic class JspApplication extends SpringBootServletInitializer {    public static void main(String[] args) {        SpringApplication.run(JspApplication.class, args);    }    @Override    protected SpringApplicationBuilder configure            (SpringApplicationBuilder builder) {       return builder.sources(JspApplication.class);    }}

指定项目packagewarr

<!--打包类型--><packaging>war</packaging>

mavenpackag

打包_maven_04

war到tomcatt后发布打包的war

访问web在浏览器中的应用程序

将包装结果放在tomcatwebaps目录下:\java\dev2\apache-tomcat-9.0.65\webapps

启动tomcat,浏览器访问http://localhost:8080/myboots/main

打包_spring_05

3.包装jar包88pomm.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.12</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bjpowernode</groupId>    <artifactId>course13_</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <!--tomcat依赖,处理jsp-->        <dependency>            <groupId>org.apache.tomcat.embed</groupId>            <artifactId>tomcat-embed-jasper</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <!--包装后的文件名称  88-->        <finalName>myboot</finalName>        <!--包装后的文件名称  88-->        <finalName>myboot</finalName>        <!--加入resources插件 -->        <!--指定编译jsp到METAA-INF/resources-->        <resources>            <resource>                <directory>src/main/webapp</directory>                <targetPath>META-INF/resources</targetPath>                <includes>                    <include>**/*.*</include>                </includes>            </resource>            <!--若使用mybatis,同时将xml文件放在src//main/java目录-->            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>            <!--把src/main在classpath目录中编译所有文件/resources-->            <resource>                <directory>src/main/resources</directory>                <includes>                    <include>**/*.*</include>                </includes>            </resource>        </resources>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <!--打包jar, 当有jsp文件时,maven-plugin插件的版本必须指定 1.4.2.RELEASE-->                <version>1.4.2.RELEASE</version>            </plugin>        </plugins>    </build></project>

创建webapp目录888

打包_spring_06

web应用目录指定webapp

打包_打包操作_07

HelloController

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;///打包放肆jar  88@Controllerpublic class HelloController {    @RequestMapping("/hello")    public ModelAndView hello(){        ModelAndView mv  = new ModelAndView();        mv.addObject("data",“SpringBoot打包为jar”;        mv.setViewName("main");        return mv;    }}

设置视图解析器

applicationg.properties

#端口server.port=9002server.servlet.context-path=/myboot#视图分析器的配置  88http://spring.mvc.view.prefix=/spring.mvc.view.suffix=.jsp

启动主类88

浏览器输入http://localhost:9002/myboot/hello

打包_spring_08

打包89

在myboot.jar--在Explorer显示----cmd---输入java-jarmyboot.jar运行

---输入http的浏览器://localhost:9002/myboot/hello

打包_打包操作_09

打包_xml_10

打包_打包操作_11

打包_xml_12

4.SpringBoot部署和运行模式总结89-91

➢Main方法(开发阶段)直接在IDEA中运行SpringBoot程序

➢用maven将SpringBoot安装成jar包,用Java命令运行

java-jarspringboot-xxx.jar

这个命令可以包装在Linux的shell脚本中(在线部署)

◼写一个shell脚本:

#!/bin/sh

java-jarxxx.jar

◼授权chmod777runrun.sh

◼启动shell脚本:./run.sh