springboot--打jar包,war包,添加logback⽇志springboot–打jar包,war包
打war包的过程
springboot内置了Tomcat服务器,可以使⽤maven的打包⼯具进打包。
1.配置⼀下pom⽂件,选择打包⽅式,加⼊jsp依赖,还有war包的名称
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /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.5.0</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<!--修改打包⽅式,打war包-->
<packaging>war</packaging>
<groupId>com</groupId>
<artifactId>037-springboot-jar</artifactId>
<version>1.0.0</version>
<name>037-springboot-jar</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot内嵌Tomcat解析jsp的依赖-->
<dependency>
<groupId>at.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--指定打war包的名称-->
<finalName>SpringbootWar</finalName>
<resources>
<!--需要对webapp下⾯的jsp⽂件进⾏编译-->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resource>
<!--
加这⾏代码是为了将resources⽂件夹下⾯的主配置⽂件
application.properties编译到target⽂件夹⾥⾯,
主配置⽂件夹⾥⾯有视图解析器,如果没有这⾏代码,就不到
设置好的jsp页⾯
-->
<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.创建⼀个控制器类(UserController)
package;
import Controller;
import Model;
import RequestMapping; import ResponseBody; import ModelAndView;
import HashMap;
@Controller
public class UserController {
@RequestMapping(value ="/user/detail")
@ResponseBody
public Object userDetail(){
HashMap<String, Object> hashMap =new HashMap<>();
hashMap.put("id",1001);
hashMap.put("username","lisi");
return hashMap;maven打包本地jar包
}
@RequestMapping(value ="/user/page/detail")
public ModelAndView userPageDetail(){
ModelAndView mv =new ModelAndView();
mv.addObject("id",1001);
mv.addObject("username","wangwu");
mv.setViewName("userDetail");
return mv;
}
}
3. 需要⼊⼝⽅法application继承SpringBootServletInitializer:
package;
import SpringApplication;
import SpringBootApplication;
import SpringApplicationBuilder;
import SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
//参数为当前springboot启动类
//构建新资源
return builder.sources(Application.class);
}
}
4.使⽤maven⾥⾯的package功能,会在target⽂件⾥⾯⽣成war包,⽣成的war包放⼊tomcat⽂件夹⾥的webapp⽂件夹下⾯,在运⾏tomcat服务器,需要在url地址⾥⾯加⼊资源路径名,也就是war包的名字,就可以运⾏了
打Jar包的过程
打jar包的过程⽐较简单,不需要在pom⽂件中声明打jar包,因为默认的就是打jar包,程序⼊⼝main⽅法也不需要的继承其他类。下⾯是代码⽰例:
1.配置pom⽂件,和war包的配置过程类似
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /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.5.0</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>037-springboot-jar</artifactId>
<version>1.0.0</version>
<name>037-springboot-jar</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot内嵌Tomcat解析jsp的依赖-->
<dependency>
<groupId>at.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--指定打jar包的名称-->
<finalName>springbootjar</finalName>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
<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>
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
2.使⽤maven的package打包⼯具,target⽂件夹会出现jar包,将jar包放⼊到的任意⼀个⽂件夹,并在地址栏⾥⾯输⼊cmd,可以打开cmd命令界⾯,并进⼊到相应的⽂件夹,输⼊java -jar springbootjar.jar指令可以运⾏jar包,⾃动打开tomcat服务器。之后再浏览器输⼊url地址,不需要项⽬名称,和使⽤开发⼯具⼀样的⽤法。
war包需要放到tomcat服务器中运⾏,也就是放到tomcat的webapp⽬录下,war包的⼯程以本地的tomcat服务器的端⼝号为准;jar包使⽤的是内嵌的tomcat服务器,以核⼼配置⽂件的端⼝号的,根路径为准。
我们还可以在同级⽬录下写⼀个bat后缀的⽂件,⾥⾯写⼊java -jar springbootjar.jar指令,之后我们点
击这个bat⽂件就可以直接启动这个jar包
springboot集成logback⽇志
springboot⼯程在创建好的项⽬⾥⾯已经集成了logback⽇志,所以不需要添加的额外的依赖。
1.需要在资源⽂件夹resources⾥⾯加⼊⽇志配置⽂件,这个配置⽂件的名字有格式要求,必须是-spri
ng结尾的(l): <?xml version="1.0" encoding="utf-8" ?>
<!-- 从⾼到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL -->
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论