首页 > 其他分享 >打包

打包

时间:2023-06-07 15:05:58浏览次数:48  
标签:jar boot springframework spring org main 打包

1. 打包  85

Spring Boot 可以打包为 war 或 jar 文件。 以两种方式发布应用

2. Spring Boot 打包为 war

创建 Spring Boot web 项目: course13

2.1  pom.xml  85

在 pom.xml 文件中配置内嵌 Tomcat 对 jsp 的解析包

<?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>

            <!--使用了mybatis ,而且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

打包_打包操作

指定 webapp 是 web 应用目录

打包_xml_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 创建 jsp 文件  85

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

2.4 创建 JspWarController  85

JspController

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//演示打包方式打包为war文件  85
@Controller
public class JspController {

    @RequestMapping("/main")
    public String main(Model model){
        model.addAttribute("data","SpringBoot打包为war文件");
        return "index";
    }
}

2.5 设置视图解析器  85

applicationg.properties

server.port=9001
server.servlet.context-path=/myjsp

#指定视图解析器  85
http://spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

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

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

打包_maven_03

2.7 主启动类继承 SpringBootServletInitializer  86-87

继承 SpringBootServletInitializer 可以使用外部 tomcat。SpringBootServletInitializer 就是原有的 web.xml 文件的替代。使用了嵌入式 Servlet,默认是不支持 jsp。

 继承这个类SpringBootServletInitializer

 才能使用独立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
 */
@SpringBootApplication
public 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);
    }
}

指定项目 package 是 war

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

maven package 打包

打包_xml_04

发布打包后的 war 到 tomcat

在浏览器访问 web 应用

把打包结果放到tomcat的webapps目录下E:\java\dev2\apache-tomcat-9.0.65\webapps

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

打包_xml_05

3. 打包jar包  88

pom.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_1</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>
        <!--加入resources插件 -->
        <!--指定编译jsp到META-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/resources中的所有文件编译到classpath目录中-->
            <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 目录  88

打包_xml_06

指定 webapp 是 web 应用目录

打包_spring_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
@Controller
public 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=9002
server.servlet.context-path=/myboot

#配置视图解析器  88
http://spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

启动主类  88

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

打包_maven_08

打包  89

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

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

打包_spring_09

打包_xml_10

打包_spring_11

打包_spring_12

4. Spring Boot 部署和运行方式总结  89-91

➢ 在 IDEA 中直接运行 Spring Boot 程序的 main 方法(开发阶段)

➢ 用 maven 将 Spring Boot 安装为一个 jar 包,使用 Java 命令运行

java -jar springboot-xxx.jar 

可以将该命令封装到一个 Linux 的一个 shell 脚本中(上线部署)

◼ 写一个 shell 脚本:

#!/bin/sh

java -jar xxx.jar

◼ 赋权限 chmod 777 run.sh

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

标签:jar,boot,springframework,spring,org,main,打包
From: https://blog.51cto.com/u_15784725/6431583

相关文章

  • uniapp离线打包总结
    uniapp离线打包总结一、准备好AndroidStudio的项目外壳,这里采用的是https://nativesupport.dcloud.net.cn/AppDocs/download/android.html#下载后选用HBbuilder-Integrate-AS作为外壳,如下图所示二、Android模块配置按项目所用到的模块进行配置,详情参考官网https://nativesu......
  • tar打包文件排除项使用
    1、exclude排除参数使用tar-Pczf/tmp/data.tar.gz/data--exclude*.jar--exclude*.war--exclude=/data/jenkins2、注意项a、排除目录需要使用绝对路径b、如果在crontab中使用需要加上转义斜杠,不然排除不生效。如下所示:tar-Pczf/tmp/data.tar.gz/data--excl......
  • 读取FTP文件,并打包成压缩包下载
    importjava.io.*;importjava.net.SocketException;importjava.net.URLEncoder;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipOutputStream;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPF......
  • python打包后,执行报错:NameError: name ‘exit‘ is not defined
    try:file_name=os.path.basename(src)file_size=os.stat(src).st_sizeexceptException:print("源文件不存在:",src)exit()在ide使用中没有问题,但是封装成应用程序时就出现问题: NameError:name'exit'isnotdef......
  • 手机app打包发布
    今天app已经打包成apk,  ......
  • PyInstaller 完美打包 Python 脚本,输出结构清晰、便于二次编辑的打包程序
    引入问题如果我要写一个Python项目,打包成exe运行(方便在没有Python的电脑上使用),我需要打包出的根目录结构美观,没有多余的、杂乱的依赖文件在那里碍眼,而且需要在发现bug时,我还需要能够修改里面的代码后,无需再次打包,就能正常运行,该怎么做呢?就以一个Hello项目为例,记一下我......
  • 打包Jar后的文件读取
    对于org.springframework.core.io.ClassPathResource本地环境时,使用方法 getFile(),可以正常读取文件,但打成Jar后,读取失败,其主要原因是:在jar里,返回的是一个Jar协议地址:jar:file:/xxx/xx.jar!/xxxx而getFile方法的实现为publicstaticFilegetFile(URLresourceUrl,St......
  • 【python】打包py文件
    pyinstaller安装方法步骤:①安装打包用的模块pipinstallPyInstaller②创建虚拟环境pipinstallpipenv#用于搭建虚拟环境pipenvinstall#创建一个新的虚拟环境pipenvshell#进入这个虚拟环境pipinstall--#通过pip安装程序所需要的模块PyInstaller-i1.i......
  • SpringBoot打包成WAR包的时候把第三方jar包打到LIB文件夹下和把第三方jar包打入到Spri
    SpringBoot打包成WAR包的时候把第三方jar包打到LIB文件夹下和把第三方jar包打入到SpringBootjar包中转载首先我们应该知道我们把SPRINGBOOT项目打包成WAR包和JAR包的时候分别需要何种插件我们最常用的把springBoot打成jar包的插件是下面这样的一个插件,这是把我们的springBoot......
  • JavaFX系列---【新建JavaFx项目和打包】
    新建JavaFx项目和打包1.安装jdk17,并配置环境变量下载地址:https://www.oracle.com/java/technologies/downloads/#java172.安装wix3和启用.NETFREAMEWORK3.5下载地址:https://github.com/wixtoolset/wix3/releases/tag/wix3112rtm3.安装scencebuilder下载地址:https://ope......