1.创建maven工程springboot-jar
2.在pom.xml指定打包方式为jar
<groupId>com.test</groupId> <artifactId>springboot-jar</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>
2.在pom.xml引入springboot依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.8.RELEASE</version> </dependency> </dependencies>
4.指定maven打包插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.8.RELEASE</version>
<configuration>
<includeSystemScope>true</includeSystemScope> //解决scope为system的jar包无法被打包进jar文件
<mainClass>com.test.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
5.controller测试类
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author c * @date 2020/5/14 9:59 */ @RestController public class TestController { @RequestMapping("/helloworld") public String hello(String name) { return "name" + name; } }
6.启动类Application
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author c * @date 2020/5/14 10:00 */ @SpringBootApplication public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); LOG.info("**************** Startup Success ****************"); } }
7.application.yml配置文件
server:
port: 7070
session-timeout: 0
context-path: /
8.打包测试运行
idea右上角Maven--Lifecycle--package,在项目target目录下生成两个jar文件,一个是加载maven依赖另一个没有加载,
我们选择springboot-jar-1.0-SNAPSHOT.jar上传到linux目录下.
启动项目:java -jar springboot-jar-1.0-SNAPSHOT.jar 但是关闭窗口项目就停止了
后台启动项目:nohup java -jar springboot-jar-1.0-SNAPSHOT.jar & 关闭窗口不会停止项目,还会在当前目录生成nohup.out日志文件
nohup java -jar springboot-jar-1.0-SNAPSHOT.jar > 日志文件名.log 2>&1 & 指定生成的日志文件名
nohup java -jar springboot-jar-1.0-SNAPSHOT.jar >/dev/null 2>&1 & 标准输出和错误输出都丢弃