首页 > 其他分享 >SpringBoot 09: SpringBoot项目打包 (war包 + jar包)

SpringBoot 09: SpringBoot项目打包 (war包 + jar包)

时间:2022-11-15 23:12:46浏览次数:39  
标签:SpringBoot boot 09 jar springframework import org war

打成war包

项目配置

  • 创建一个springboot的jsp应用

  • pom.xml

	<!-- 执行打包是war包-->
	<packaging>war</packaging>

	<build>
		<!--打包后的文件名称-->
		<finalName>myboot</finalName>

		<!--resources插件, 把jsp编译到指定的目录-->
		<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>
  • controller
package com.example.web.controller;

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

@Controller
public class UserController {

    @RequestMapping("/user/toLogin")
    public String toLogin(Model model){
        model.addAttribute("data", "need to login page");
        return "toLogin";
    }
}
  • application.properties中配置视图解析器
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
  • webapp目录下的toLogin.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>toLogin page</title>
</head>
<body>
从后台获取的数据: ${data}
</body>
</html>
  • 主启动类继承SpringBootServletInitializer, 重写configure方法
package com.example;

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;

@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);
	}
}
  • 部署war:把war放到tomcat等服务器的发布目录中。以tomcat为例,myboot.war放到tomcat/webapps目录中

打成jar包

  • 去掉打成war包的标签
<packaging>war</packaging>
  • 指定springboot-maven-plugin版本
<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>
  • 最后执行maven的clean插件,清除之前打的包,然后再次打包
  • 在target目录中,生成jar 文件,例子是myboot.jar
  • 可以执行独立的springboot项目(已经配置了java环境):在cmd中执行java -jar myboot.jar

标签:SpringBoot,boot,09,jar,springframework,import,org,war
From: https://www.cnblogs.com/nefu-wangxun/p/16894402.html

相关文章

  • SpringBoot 08: SpringBoot综合使用Mybatis + Dubbo + Redis
    业务背景Student表CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(255)COLLATEutf8_binDEFAULTNULL,`phone`varchar(11)......
  • Springboot单元测试Junit的坑及解决方案
    最近做springboot项目,写单元测试导入junit的时候,org.junit.jupiter.api.Test和org.junit.Test傻傻分不清,因为习惯了用junit4,所以导入的都是org.junit.Test,普通的测试是没......
  • Installing harbor-2.6.2 on openEuler-22.09
    一、Installingharbor-2.6.2onopenEuler1地址https://goharbor.iohttps://github.com/goharbor/harbor2Harbor安装条件部署Harbor的最低硬件和软件配置......
  • 洛谷 P8097
    考虑时光倒流。由于A操作只会给两个活跃的点连边,所以可以忽略,倒过来相当于没有删边操作。然后只剩下加边,加活跃点,两种操作。每次暴力DFS即可。时间复杂度\(\mathca......
  • P5309 [Ynoi2011] 初始化
    P5309[Ynoi2011]初始化考虑暴力,模拟题意,时间复杂度竟是\(O(\frac{n^2}{x})\),那么对于\(x\ge\sqrt{n}\)的修改就可以暴力了,这不是根号分治吗。再去考虑\(x<\sqrt{n}......
  • SpringBoot+@Validated实现参数验证(非空、类型、范围、格式等)-若依前后端导入Excel
    场景若依管理系统前后端分离版基于ElementUI和SpringBoot怎样实现Excel导入和导出:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108278834SpringBoot+Vu......
  • jar包的下载方法及地址
    引言:一般来说我们所需要的jar包都是在官网上下载的,但是对于一些大的项目可能需要很多的jar包,于是我们得去不同的官网去下载,不仅难找,难下载,有时下载的版本与所需的不一致。......
  • 阿里云服务器上SpringBoot单体项目的部署
    1、注意阿里云需要安全组端口设置,不然无法访问。不仅linux虚拟机里需要开放指定端口,阿里云ecs安全组也需要添加开放对应的端口;2、linux虚拟机中安装docker,参照阿里云服务......
  • SpringBoot 07: springboot中使用dubbo
    公共接口项目独立的maven项目:定义了接口和数据类数据类kagecom.example.dubbo.model;importjava.io.Serializable;publicclassStudentimplementsSeria......
  • 009网页状态码302和307的区别
    状态码307与302之间的唯一区别在于,当发送重定向请求的时候,307状态码可以确保请求方法和消息主体不会发生变化 ......