IDEA中springboot項目打包成jar
阿新 • • 發佈:2019-04-27
dea override tin desc pid html prope 虛擬機 getc
image.png
image.png
image.png
springboot的打包方式有很多種。有打成war的,有打成jar的,也有直接提交到github,通過jekins進行打包部署的。這裏主要介紹如何打成jar進行部署。不推薦用war,因為springboot適合前後端分離,打成jar進行部署更合適。
首先需要在application.properties當中配置端口
server.port=8080
# http://localhost:8088/swagger-ui.html
marven的配置文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.weixin</groupId>
<artifactId> smallsystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>smallsystem</name>
<description>smallsystem</description>
<parent>
<groupId>org.springframework.boot</groupId >
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.weixin.SmallsystemApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
註意最下面的build這塊一定要配置否則打jar的時候會說找不 到主類
image.png
在啟動類當中加上extends SpringBootServletInitializer並重寫configure方法,這是為了打包springboot項目用的。
package com.weixin;
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 SmallsystemApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SmallsystemApplication.class, args);
}
@Override//為了打包springboot項目
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
然後按照順序運行mvn clean再mvn install,我是用idea執行的
image.png
然後就會出來我們需要的jar
image.png
然後到這個jar的根目錄下執行java -jar smallsystem-0.0.1-SNAPSHOT.jar
這個執行方式windows和linux上都一樣
如果是阿裏雲上的,需要通過阿裏雲把你指定的端口開放,如果是虛擬機上的,需要把防火墻什麽的關掉,開放端口即可。
IDEA中springboot項目打包成jar