使用Eclipse對SpringBoot項目如何進行打包部署


1,打包概要介紹:

  自己做了個小demo,突然想練一下如何打包發布,期間出現了兩個錯誤,第一個是加載不到主類,第二個是加載不到jsp文件,一會會把這兩個問題一一陳述,以及解決方法。

1.1,先檢查pom文件

  因為springboot的項目打包的話他是根據pom.xml文件進行打包的,注意:(仔細觀看我寫的注釋,加載不到jsp文件那你還要回頭再看)

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6     <groupId>com.example.demo</groupId>
  7     <artifactId>stores-erp</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <!-- 這邊要改成我們jar  因為我們要大jar包 假如是war包的話就寫成war -->
 10     <packaging>jar</packaging>
 11     <name>stores-erp</name>
 12     <description>stores-erp</description>
 13     
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>1.5.18.RELEASE</version>
 18         <relativePath /> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25     </properties>
 26 
 27     <dependencies>
 28         <dependency>
 29             <groupId>cglib</groupId>
 30             <artifactId>cglib</artifactId>
 31             <version>3.2.5</version>
 32         </dependency>
 33         <dependency>
 34             <groupId>com.github.pagehelper</groupId>
 35             <artifactId>pagehelper-spring-boot-starter</artifactId>
 36             <version>1.2.5</version>
 37         </dependency>
 38         <dependency>
 39             <groupId>jstl</groupId>
 40             <artifactId>jstl</artifactId>
 41             <version>1.2</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework.boot</groupId>
 45             <artifactId>spring-boot-starter-jdbc</artifactId>
 46         </dependency>
 47         <dependency>
 48             <groupId>org.springframework.boot</groupId>
 49             <artifactId>spring-boot-starter-web</artifactId>
 50         </dependency>
 51         <dependency>
 52             <groupId>org.apache.tomcat.embed</groupId>
 53             <artifactId>tomcat-embed-jasper</artifactId>
 54         </dependency>
 55         <dependency>
 56             <groupId>org.mybatis.spring.boot</groupId>
 57             <artifactId>mybatis-spring-boot-starter</artifactId>
 58             <version>1.3.2</version>
 59         </dependency>
 60 
 61         <dependency>
 62             <groupId>org.springframework.boot</groupId>
 63             <artifactId>spring-boot-devtools</artifactId>
 64             <scope>runtime</scope>
 65         </dependency>
 66         <dependency>
 67             <groupId>mysql</groupId>
 68             <artifactId>mysql-connector-java</artifactId>
 69             <scope>runtime</scope>
 70         </dependency>
 71         <dependency>
 72             <groupId>org.springframework.boot</groupId>
 73             <artifactId>spring-boot-starter-test</artifactId>
 74             <scope>test</scope>
 75         </dependency>
 76         <!-- springboot工具 修改代碼后不需重啟即生效 -->
 77         <dependency>
 78             <groupId>org.springframework</groupId>
 79             <artifactId>springloaded</artifactId>
 80         </dependency>
 81         <!-- 文件上傳 -->
 82         <dependency>
 83             <groupId>commons-fileupload</groupId>
 84             <artifactId>commons-fileupload</artifactId>
 85             <version>1.3.2</version>
 86         </dependency>
 87     </dependencies>
 88     <build>
 89         <!-- 注意最下面的build這塊一定要配置否則打jar的時候會說找不 到主類 -->
 90         <plugins>
 91             <plugin>
 92                 <groupId>org.springframework.boot</groupId>
 93                 <artifactId>spring-boot-maven-plugin</artifactId>
 94                 <!-- 注意:記住啊 下面<version>1.4.2.RELEASE</version>
 95                           這行代碼是必須要加的不加的話就全GG了,因為springboot打包有個bug
 96                           反正這行代碼別忘了加
 97                      -->
 98                 <version>1.4.2.RELEASE</version>
 99                 <configuration>
100                     <mainClass>com.example.demo.SpringBootDay02Application</mainClass>
101                 </configuration>
102             </plugin>
103         </plugins>
104         <!--這里進行配置,后會自動的加載src/main/webapp下的所有文件 :配置Maven 對resource文件 過濾 -->
105         <!-- 我之前遇見的jsp找不到的問題 就是因為沒有配置好以下的文件所以才會加載不到jsp配置文件 -->
106         <resources>
107             <resource>
108                 <directory>src/main/webapp</directory>
109                 <targetPath>META-INF/resources</targetPath>
110                 <includes>
111                     <include>**/**</include>
112                 </includes>
113             </resource>
114             <resource>
115                 <directory>src/main/resources</directory>
116                 <filtering>true</filtering>
117             </resource>
118         </resources>
119     </build>
120 </project>

1.2,pom.xml中需要注意的地方

注意pom.xml最下面的build這塊一定要配置否則打jar的時候會說找不到主類(我標紅的地方則是Springboot的啟動類路徑,咱們的路徑肯定不一樣修改一下哈

1.3,啟動類代碼修改

  在啟動類當中加上繼承 SpringBootServletInitializer類並重寫configure方法,這是為了打包springboot項目用的(不是單單實現就完事了,有些代碼還需要修改一下,仔細點)

 1 package com.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.builder.SpringApplicationBuilder;
 6 import org.springframework.boot.web.support.SpringBootServletInitializer;
 7 
 8 @SpringBootApplication
 9 public class SpringBootDay02Application extends SpringBootServletInitializer{
10 
11     public static void main(String[] args) {
12         SpringApplication.run(SpringBootDay02Application.class, args);
13     }
14     //為springboot打包項目用的
15     @Override
16     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
17         // TODO Auto-generated method stub
18         return builder.sources(this.getClass());
19     }
20 }

1.4,application.properties文件檢查

    代碼大致不需要修改,就是檢查各個應用連接之類的,假如在虛擬機上部署的話則需要進行一些修改,端口號啊,環境配置連接等等

 1 server.port=8080
 2 server.servlet.context-path=/
 3 debug=false
 4 
 5 spring.datasource.url=jdbc:mysql://localhost:3306/stores-erp?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
 6 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 7 spring.datasource.username=root
 8 spring.datasource.password=321
 9 
10 spring.mvc.view.prefix=/WEB-INF/jsp/
11 spring.mvc.view.suffix=.jsp
12 
13 mybatis.config-location=classpath:mybatis/mybatis-config.xml
14 mybatis.mapper-locations=classpath:mybatis/mappers/*/*.xml
15 mybatis.type-aliases-package=com.example.demo.pojo

2.1,第一步,

  點擊項目右鍵,Run As=》Maven Clean進行項目刷新

 

2.2,第二步,

  Run As=》Maven insert進行項目打包,假如成功的話則出現以下頁面信息:

2.3,第三步,

  我們找到項目中的target文件就可以看到我們的jar包文件等等了。

2.4,第四步,

  你可以選擇直接把stores-erp-0.01-SNAPSHOT.jar拷貝出去,也可以直接去他目錄下找它,我選擇的是找到項目中的jar文件的所在地址,在導航欄輸入cmd。

 

 

2.5,第五步,

  啟動jar包項目,注意:我之前做的時候這邊出了個錯誤,就是加載不到主類等等,是個非常操蛋的錯誤就是因為少加了個 -  這個坑我提你們踩了- -.(這個執行方式windows和linux上都一樣)

3,然后就可以進行訪問了效果如下:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM